簡體   English   中英

Spring JDBCTemplate除了Apache Commons之外還有其他MySQL數據源嗎?

[英]Spring JDBCTemplate other MySQL datasource than apache commons?

我正在使用Spring JDBCTemplate在apache commons數據源(org.apache.commons.dbcp.BasicDataSource)上執行SQL操作,並且當該服務啟動並運行了很長時間時,我最終收到此異常:

org.springframework.dao.RecoverableDataAccessException: StatementCallback; SQL [SELECT * FROM vendor ORDER BY name]; The last packet successfully received from the server was 64,206,061 milliseconds ago.  The last packet sent successfully to the server was 64,206,062 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 64,206,061 milliseconds ago.  The last packet sent successfully to the server was 64,206,062 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
    at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:406)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:455)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:463)
    at com.cable.comcast.neto.nse.taac.dao.VendorDao.getAllVendor(VendorDao.java:25)
    at com.cable.comcast.neto.nse.taac.controller.RemoteVendorAccessController.requestAccess(RemoteVendorAccessController.java:78)

我嘗試將'autoReconnect = true'添加到連接字符串,但是仍然會出現此問題。 是否可以使用另一個數據源為我管理重新連接?

BasicDataSource可以管理使連接保持活動狀態。 您需要設置以下屬性:

minEvictableIdleTimeMillis = 120000 // Two minutes
testOnBorrow = true
timeBetweenEvictionRunsMillis = 120000 // Two minutes
minIdle = (some acceptable number of idle connections for your server)

這些將配置數據源以繼續不斷測試您的連接,並在連接過期時過期並刪除它們。 您可能還需要考慮一下基本數據源上的許多其他屬性,以調整連接池的性能。 過去,我在遇到數據庫訪問問題時遇到了一些奇怪的問題,而這全都取決於連接池的配置方式。

您可以嘗試C3PO:

http://sourceforge.net/projects/c3p0/

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close">
   <property name="user" value="${db.username}"/>
   <property name="password" value="${db.password}"/>
   <property name="driverClass" value="${db.driverClassName}"/>
   <property name="jdbcUrl" value="${db.url}"/>
   <property name="initialPoolSize" value="0"/>
   <property name="maxPoolSize" value="1"/>
   <property name="minPoolSize" value="1"/>
   <property name="acquireIncrement" value="1"/>
   <property name="acquireRetryAttempts" value="0"/>
   <property name="idleConnectionTestPeriod" value="600"/> <!--in seconds-->
</bean>

格雷廷斯·帕科夫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM