1. Tomcat连接池配置
1. 1 Tomcat默认连接池配置
(1)修改conf/server.xml,添加如下内容
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name=" jdbc/connPool"
auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
</GlobalNamingResources>
(2)修改conf/context.xml,添加如下内容
<ResourceLink name=" jdbc/connPool " global=" jdbc/connPool " type="javax.sql.DataSource"/>
(3)修改web应用的web.xml,添加如下内容
<resource-ref>
<description>DB Connection</description>
<res-ref-name> jdbc/connPool </res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
1.2 Tomcat c3p0连接池配置
(1)修改conf/server.xml,添加如下内容
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="jdbc/connPool"
auth="Container"
description="DB Connection"
driverClass="com.mysql.jdbc.Driver"
maxPoolSize="10"
minPoolSize="2"
acquireIncrement="2"
user="root"
password="root"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:mysql://localhost:3306/db?autoReconnect=true" />
</GlobalNamingResources>
(2)修改conf/context.xml,添加如下内容
<ResourceLink name=" jdbc/connPool " global=" jdbc/connPool " type="javax.sql.DataSource"/>
(3)修改web应用的web.xml,添加如下内容
<resource-ref>
<description>DB Connection</description>
<res-ref-name> jdbc/connPool </res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
2. Tomcat连接池调用方式
(1) Java直接调用
Java代码// 初始化查找命名空间 Context initContext = new InitialContext(); Context envContext = (Context)initContext.lookup("java:/comp/env"); // 找到DataSource DataSource ds = (DataSource)envContext.lookup("jdbc/connPool"); 或
Java代码Context initContext = new InitialContext(); DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/pooledDS");
(2) 通过Spring方式调用
Xml代码<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:/comp/env/jdbc/connPool</value>
</property></bean>
3. Resources配置其它说明
Resources的属性可自由添加,其作为type指定的类运行时所需的参数。