0

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

<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> 

不会对准备好的语句造成任何问题,但是:

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

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

4

2 回答 2

0

设置空闲超时=480 分钟?

于 2009-07-17T14:45:10.627 回答
0

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

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();

看起来, aCallableStatementPreparedStatement您的要求更相关。

在此处探索 jdbc 中的 CallableStatement

于 2019-05-28T02:05:28.307 回答