我有一个与Prepared Steatement池(跨所有连接)相关的问题。
<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服务器会在8小时后关闭连接
2条答案
按热度按时间zengzsys1#
是否将空闲超时设置为480分钟?
wj8zmpe12#
您一定是将
PreparedStatement
用于错误的目的。PreparedStament
主要用于在单个流中使用不同的参数多次执行相同的查询。一旦完成了它的需要,它应该关闭,如下所示。看起来,
CallableStatement
比PreparedStatement
更符合您的要求。探索jdbc here中的可调用语句