【问题标题】:jboss connection poolingjboss连接池
【发布时间】:2010-11-11 17:30:45
【问题描述】:

我有一个与 Prepared Steement 池(跨所有连接)有关的问题。这是配置文件

<datasources>
  <local-tx-datasource>
    <jndi-name>JNDI-NAME</jndi-name>
    <connection-url>jdbc:mysql://<server_name>/<database_name>?useServerPrepStmts=true</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>xxx</user-name>
    <password>xxxxx</password>
    <min-pool-size>10</min-pool-size>
    <max-pool-size>20</max-pool-size>
    <idle-timeout-minutes>20</idle-timeout-minutes>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
    <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
    <background-validation>true</background-validation>
    <background-validation-minutes>5</background-validation-minutes>
    <prepared-statement-cache-size>100</prepared-statement-cache-size>
    <share-prepared-statements>true</share-prepared-statements>
    <!-- sql to call when connection is created
    <new-connection-sql>some arbitrary sql</new-connection-sql>
      -->
    <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
    <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
      -->
    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasources>

好像这行:

<background-validation-minutes>5</background-validation-minutes> 

不会导致Prepared Statements出现任何问题,但是:

<idle-timeout-minutes>20</idle-timeout-minutes>

如果过去 20 分钟没有流量,所有连接都会被删除并重新创建。因此,现有的 Prepared Statements 将从缓存的 Prepared Statements 池中删除。如何克服这个问题?我必须使用 idle-timeout-minutes 因为 MySQL 服务器在 8 小时后关闭连接

【问题讨论】:

    标签: jboss connection-pooling


    【解决方案1】:

    设置空闲超时=480 分钟?

    【讨论】:

      【解决方案2】:

      您一定是出于错误的目的使用了PreparedStatementPreparedStament 主要用于在单个流中使用不同的参数多次执行相同的查询。一旦需要完成,它应该如下所示关闭。

      PreparedStatement pstmt = con.prepareStatement("insert into Emp(name) values(?)");
      pstmt.setString(1, "foo");
      int i1=stmt.executeUpdate();  // inserted one record
      pstmt.setString(1, "bar");
      int i2=stmt.executeUpdate(); // inserted 2nd record
      pstmt.close(); // close prepared statement 
      con.close();
      

      看起来,CallableStatementPreparedStatement 更符合您的要求。

      探索 jdbc 中的 CallableStatement here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-23
        • 1970-01-01
        • 2015-11-07
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多