Tomcat 7 JDBC Connection Pool

原文:http://ramonblog.cloudfoundry.com/blog/4fd34be35e8e781871000001

commons-dbcp

Before introduce tomcat 7 jdbc connection pool, we have a basic and simple introduction aboutcommons-dbcp. To use dbcp, as below steps:

  1. download dbcp from here and its dependency commons-pool.
  2. put those two libs into WEB-INF/lib
  3. You can use BasicDataSource as these sample code Note: use jdbc directly, it's boring and tedious. I suggest use spring jdbc template.

Tomcat Built in Connection Pool

Tomcat provides a replacement or alternative for commons-dbcp since version 6. Old connection pool is also based commons-dbcp. You can find detail information about tomcat new connection pool from here: The Tomcat JDBC Connection Pool. From that we know, it's much better than commons-dbcp.

To use it, we need do below things. For detail see The Tomcat JDBC Connection Pool. 1. put jdbc driver into {catalina}/lib. Usually we put jdbc library into WEB-INF/lib as app library. Because we need add resource in context which is initialized by tomcat, not considering your apps library in WEB-INF/lib. Therefore it would throw exception 'cannot find jdbc driver' when tomcat startup. 2. configure resource in context.xml. You can find detail information from here, note you can find more information about Context from here. In my app, I add context.xml in META-INF 3. configure resource ref in web.xml 4. get resource from java code.

Note for step 2, you need set password/username/url to make sure connect to database. If your app need read it from other configuration, such as Amazon Beanstalk read connection from Container parameter, you cannot use JNDI to find pool settings, just use programatic way PoolProperties likehere

Connection pool in Amazon Beanstalk

To use tomcat built in connection pool, we need put jdbc driver into {catalina}/lib. To do that, we need customize our environment, see here. Therefore it's better use commons-dbcp for your simple application.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值