我有一个与Prepared Steatement 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分钟没有流量,会导致删除所有连接并重新创建。由于现有的准备语句已从缓存的准备语句池中删除。如何克服这个问题?我必须使用idle-timeout-minutes,因为MySQL服务器会在8h后关闭连接
答案 0 :(得分:0)
设置空闲超时= 480分钟?
答案 1 :(得分: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();
看起来CallableStatement
比PreparedStatement
更符合您的要求。
在jdbc here中浏览CallableStatement