Tomcat 7 JDBC Connection Pool

来源:互联网 发布:ajax发送json数据 编辑:程序博客网 时间:2024/06/05 10:47

原文: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.

原创粉丝点击