如何配置Tomcat数据库连接池

GD Star Rating
loading...

仅需修改context.xml:

  1. <context path="/chinatrial">
  2.     <resource
  3.        name="jdbc/DBPool"
  4.        type="javax.sql.DataSource"
  5.        driverClassName="com.mysql.jdbc.Driver"
  6.        maxIdle="16"
  7.        minIdle="8"
  8.        maxActive="64"
  9.        initialSize="16"
  10.        username="root"
  11.        password="root"
  12.        poolPreparedStatements="true"
  13.        removeAbandoned="true"
  14.        removeAbandonedTimeout="60"
  15.        testOnBorrow="false "
  16.        url="jdbc:mysql://localhost/chinatrial"
  17.        connectionProperties="useUnicode=true;characterEncoding=utf-8;autoReconnect=true">
  18. </context>
<context path="/chinatrial">
    <resource
        name="jdbc/DBPool"
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        maxIdle="16"
        minIdle="8"
        maxActive="64"
        initialSize="16"
        username="root"
        password="root"
        poolPreparedStatements="true"
        removeAbandoned="true"
        removeAbandonedTimeout="60"
        testOnBorrow="false "
        url="jdbc:mysql://localhost/chinatrial"
        connectionProperties="useUnicode=true;characterEncoding=utf-8;autoReconnect=true">
</context>

各属性的含义以及完整属性列表可参考:DBCP官方文档,以下是一些建议:

  • 将连接属性配置在“ connectionProperties”中,这样可以避免对“&”符号进行转义
  • 配置“poolPreparedStatements”以缓存PreparedStatement对象提升性能
  • 配置“removeAbandoned”和“removeAbandonedTimeout”以打开“Remove Abandoned Connection”功能。该功能会定期检查并关闭没有被close的connection,避免连接泄
  • 将“testOnBorrow”设为“false”以关闭从连接池获取连接时的有效性检查

原创内容,转载请注明: 转载自拈花微笑

本文链接地址: 如何配置Tomcat数据库连接池

685

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!:

使用新浪微博登陆

无觅相关文章插件,快速提升流量