tomcat 连接池

13 篇文章 0 订阅

连接池,一个很神秘又犀利的话题。

 

在java里面有一个接口,javax.sql.Datasource,这是一个接口,它里面有个方法getConnection(),即通过它就可以取出connection.

 

许多框架都实现了这个接口,如spring,hibernate,c3p0,proxool,甚至是tomcat也实现了。

 

因为tomcat中实现的,即通过JNDI实现的,

最好的学习资料当然是官方文档:http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html#JDBC_Data_Sources

 

从官网复制下来的例子:

 

<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="dbusername"
            password="dbpassword"
            driverClassName="org.hsql.jdbcDriver"
            url="jdbc:HypersonicSQL:database"
            maxActive="8"
            maxIdle="4"/>
  ...
</Context>

 

 

照着配置就行了。

 

  • 疑问1,一直比较困扰我,就是在context.xml中配置了后,是否要在web.xml中进行如下配置?

 

<resource-ref>
  <description>
    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the <Context>
    configurartion for the web application.
  </description>
  <res-ref-name>
    jdbc/EmployeeDB
  </res-ref-name>
  <res-type>
    javax.sql.DataSource
  </res-type>
  <res-auth>
    Container
  </res-auth>
</resource-ref>

 

 

 

官网中有这样一句话:

 

If a resource has been defined in a <Context> element it is not necessary for that resource to be defined in /WEB-INF/web.xml. However, it is recommended to keep the entry in /WEB-INF/web.xml to document the resource requirements for the web application.

 

它的意思是说如果在context中配置了resource后,就没有必须再在web.xml中配置了,但是推荐在web.xml中保持一个入口来证明resource的需求,即为了说明下,既然是说明,那么就是可加可不加了,因为从规范的角度来说最好是加,但是我看了网上好多人的例子都没有加,而且我不加,在tomcat中测试也能正常工作。

 

 疑问2:resource配置中是否要配置factory这个属性,因为官网在global resource中配置的时候加了的,但是在一般的resource配置的时候却又不加,我没加在tomcat中也能正常工作,所以我就暂时先不加吧。

 

 

下面是一些框架中实现连接池的代码:

 

Hibernate中连接池的写法:http://developer.51cto.com/art/200906/129914.htm

作者得出tomcat中的DBCP有BUG,hibernate中实现的连接池有BUG,c3p0有BUG,最好的方法是用proxool。

 

Spring中连接池的写法:http://www.blogjava.net/lzj520/archive/2008/03/19/187211.html

其实Spring的连接池也是用DBCP来实现的;

这篇文章非常全,亮点是竟然融入了C3P0的写法,赞!

还有一篇文章也是讲spring jndi的配置的:http://karidyang.iteye.com/blog/216125

 

 

tomcat中连接池的配置1:http://stackoverflow.com/questions/10137828/jndi-resource-definition-in-apache-tomcat-6

 

tomcat中连接池的配置2:http://stackoverflow.com/questions/9183321/how-to-use-jndi-datasource-provided-by-tomcat-in-spring

 

 

tomcat5.5中连接池的写法3:http://www.iteye.com/topic/25161?page=2

它提到了要在resource中加入factory的概念

 

 ====================================================

 

refurl:http://bbs.csdn.net/topics/300211091

最古老的DBCP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值