1.在工程目录的META-INF 下创建context.xml文件,并设置一下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context debug= "99" reloadable="true" crossContext="true" verbosity="debug">
<Resource name="jdbc/mysql"
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"/>
</Context>
测试代码:
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)之间
<Context path="/jndi" docBase="D:\workspace\jndi\WebRoot" debug="0" reloadable="true" crossContext="true"/>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_quality_log." suffix=".txt" timestamp="true"/>
<Resource
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" />
<ResourceParams name="jdbc/test">
<parameter>
<name>removeAbandoned</name>
<!-- Abandoned DB connections are removed and recycled -->
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. -->
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<!-- Log a stack trace of the code which abandoned -->
<value>false</value>
</parameter>
<parameter>
<name>factory</name>
<!--DBCP Basic Datasource Factory -->
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
</ResourceParams>
并在工程目录的web.xml中添加一下代码
<resource-ref>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.在\conf\Catalina\localhost目录下创建跟工程同名的xml文件,并在web.xml中添加一下代码
<resource-ref>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
另外,看到网上有如下关系tomcat连接池的说法:
1.用户释放连接后,连接被回收到连接池,这个连接仍然是可用的。
如果一个连接等待了一段时间(连接池的最大空闲时间,可设置)仍然没有
人来请求连接,那么它将被彻底释放。当然,如果连接池中的连接个数已经
等于连接池的最小连接数(可设置),那么它不会被释放。
2.以tomcat连接池为例,tomcat启动后,假设我设置的最小连接数为5,那么这时候(此Tomcat下的程序没有启动)Tomcat是否和后台地数据库建立5条连线(session)呢?答案:不是.
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com