Tomcat之内置数据源(DBCP)的配置(for JDBC):
转载请注明出处!!
通常使用的开源数据源主要有以下3中:
1)DBCP
2)C3p0
3)Tomcat 内置数据源
至于DBCP与c3p0两个数据源的使用配置,以及tomcat内置数据源的简单应用,在之前的额文章中已有介绍(CSDN网址:http://blog.csdn.net/u012802702/article/details/51158764)
本文主要记录Tomcat的内置数据源的其他配置,包括:
1)如何配置数据源使得整个Tomcat中的所有web项目均可以使用该数据源。
2)如何配置数据源使得当前虚拟主机下的所有web应用均可以使用该数据源
3)如何配置数据源使得只有但前web应用可以使用该数据源。
以方便日后查看、参考。
一、Tomcat内置数据源的配置说明
阅读本文前需先了解一些关于Tomcat虚拟主机以及Web应用的相关配置。即对Tomcat的相关配置需要有一定的了解。
Tomcat中内置有数据源,而DBCP与tomcat同属于Apache的项目,因此不难猜到Tomcat中内置的数据源其实就是DBCP数据源。
官方对于JDBC使用其内置数据源的配置说明如下:
4. Configure Tomcat's Resource Factory
To configure Tomcat's resource factory, add an element like this to the <Context> element for this web application.
<Context ...>
...
<Resource name="jdbc/EmployeeDB"
auth="Container"
type="javax.sql.DataSource"
username="dbusername"
password="dbpassword"
driverClassName="org.hsql.jdbcDriver"
url="jdbc:HypersonicSQL:database"
maxActive="8"
maxIdle="4"/>
...
</Context>
Note that the resource name (here,
jdbc/EmployeeDB) must match the value specified in the web application deployment descriptor.
This example assumes that you are using the HypersonicSQL database JDBC driver. Customize the driverClassName and driverName parameters to match your actual database's JDBC driver and connection URL.
The configuration properties for Tomcat's standard data source resource factory (org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory) are as follows:
driverClassName - Fully qualified Java class name of the JDBC driver to be used.
username - Database username to be passed to our JDBC driver.
password - Database password to be passed to our JDBC driver.
url - Connection URL to be passed to our JDBC driver. (For backwards compatibility, the property driverName is also recognized.)
initialSize - The initial number of connections that will be created in the pool during pool initialization. Default: 0
maxActive - The maximum number of connections that can be allocated from this pool at the same time. Default: 8
minIdle - The minimum number of connections that will sit idle in this pool at the same time. Default: 0
maxIdle - The maximum number of connections that can sit idle in this pool at the same time. Default: 8
maxWait - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception. Default: -1 (infinite)
如上按照tomcat官方的说明对于JDBC使用其内置数据源只需要配置一个.<context>标签元素(其实一个context标签对应一个web应用)在<context>标签中添加<Resource>标签元素,内容如图中所示(前面文章中也有详细说明),
但问题是<context>标签在哪里配置呢?
tomcat官方给出的相应的额配置文档说明中有详细的使用说明:详情可以查看:http://127.0.0.1:8080/docs/config/context.html
打开该网址的前提是你在本地已启动Tomcat,方法如下:
启动本地Tomcat方法:找到Tomcat安装目录下/bin/startup.bat并双击运行即可启动本地的Tomcat,
关闭的方法:双击Tomcat安装目录下的/bin/shutdown.bat运行即可关闭Tomcat。
本文只在此进行简单总结,内容如下:
文件位置 |
应用场景 |
tomcat安装目录/conf/context.xml |
在该文件中配置<Context>,且配置在这个位置的信息将会被所有的web应用所共享 |
Tomcat安装目录/conf/[engin]/[Host]/context.xml |
文件中可以配置<Context>标签,这里配置的信息将会被这台虚拟主机中的所有web应用所共享 |
Tomcat安装目录/conf/server.xml |
文件中的<Host>标签中配置<Context>标签,这是web应用的第一种配置方式,在这个标签中配置的信息将只对当前web应用起作用 |
Tomcat安装目录/conf/[engin]/[Host]/自己创建一个.xml文件 |
在这个文件中使用<Context>标签配置一个web应用,这是web应用第二种配置方式,在这个<Context>标签中配置的信息将只会对当前web应用起作用 |
当前web应用目录/META-INF/context.xml |
在其中可以写<Context>标签进行配置,这种配置信息将只会对当前web应用起作用,详情见前面文章:《三种数据库连接池的配置及使用》 http://blog.csdn.net/u012802702/article/details/51158764 |