1.在工程目录的META-INF 下创建context.xml文件,并设置一下内容:
auth=\"Container\" type=\"javax.sql.DataSource\"password=\"eric\"
driverClassName=\"com.mysql.jdbc.Driver\"
maxIdle=\"2\" maxWait=\"5000\"
username=\"root\"
url=\"jdbc:mysql://localhost:3306/test?autoReconnect=true\"
maxActive=\"4\"/>
测试代码:
DataSource ds = null;
if (ds == null) {
try {
Context initContext = new InitialContext();
if (initContext == null)
System.out.println(\"............\");
Context envContext = (Context) initContext
.lookup(\"java:comp/env\");
ds = (DataSource) envContext.lookup(\"jdbc/mysql\");
System.out.println(ds);
} catch (NamingException e) {
e.printStackTrace();
}
}
2.在tomcat/config/server.xml中添加一下代码(在host)之间
name=\"jdbc/test\"type=\"javax.sql.DataSource\"
password=\"eric\"
driverClassName=\"com.mysql.jdbc.Driver\"
maxIdle=\"2\"
maxWait=\"5000\"
username=\"root\"
url=\"mysql://localhost:3306/test\"
maxActive=\"4\" />
removeAbandoned
true
removeAbandonedTimeout
60
logAbandoned
false
factory
org.apache.commons.dbcp.BasicDataSourceFactory
并在工程目录的web.xml中添加一下代码
jdbc/test
javax.sql.DataSource
Container
3.在\\conf\\Catalina\\localhost目录下创建跟工程同名的xml文件,并在web.xml中添加一下代码
jdbc/test
javax.sql.DataSource
Container
另外,看到网上有如下关系tomcat连接池的说法:
1.用户释放连接后,连接被回收到连接池,这个连接仍然是可用的。
如果一个连接等待了一段时间(连接池的最大空闲时间,可设置)仍然没有
人来请求连接,那么它将被彻底释放。当然,如果连接池中的连接个数已经
等于连接池的最小连接数(可设置),那么它不会被释放。