简体   繁体   English

Spring JDBCTemplate除了Apache Commons之外还有其他MySQL数据源吗?

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

I am using Spring JDBCTemplate to perform SQL operations on an apache commons datasource (org.apache.commons.dbcp.BasicDataSource) and when the service is up and running to long, i end up getting this exception: 我正在使用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)

I have tried adding the 'autoReconnect=true' to the connection string, but this problem still occurs. 我尝试将'autoReconnect = true'添加到连接字符串,但是仍然会出现此问题。 Is there another datasource that can be used that will manage the reconnecting for me? 是否可以使用另一个数据源为我管理重新连接?

BasicDataSource can manage keeping the connections alive for you. BasicDataSource可以管理使连接保持活动状态。 You need to set the following properties : 您需要设置以下属性:

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

These will configure the data source to keep continually test your connections, and expire and remove them if they become stale. 这些将配置数据源以继续不断测试您的连接,并在连接过期时过期并删除它们。 There's a number of other properties on the basic data source that you may want to consider checking into as well to tweak your connection pooling performance. 您可能还需要考虑一下基本数据源上的许多其他属性,以调整连接池的性能。 I've run into some strange problems in the past where I was having issues with my database access and it all came down to how the connection pool was configured. 过去,我在遇到数据库访问问题时遇到了一些奇怪的问题,而这全都取决于连接池的配置方式。

You can try to C3PO: 您可以尝试C3PO:

http://sourceforge.net/projects/c3p0/ 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>

grettings pacovr 格雷廷斯·帕科夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring jdbctemplate,数据源,transactionManager - Spring jdbctemplate, datasource, transactionManager 使用 Spring JdbcTemplate - 注入数据源 vs jdbcTemplate - using Spring JdbcTemplate - injecting datasource vs jdbcTemplate Spring Boot 找不到 JdbcTemplate 的数据源 - Spring Boot can't find DataSource for JdbcTemplate Spring Framework IllegalArgumentException&#39;dataSource&#39;或&#39;jdbcTemplate&#39;是必需的JAVA - Spring Framework IllegalArgumentException 'dataSource' or 'jdbcTemplate' is required JAVA Spring Boot中的多个DataSource和JdbcTemplate(> 1.1.0) - Multiple DataSource and JdbcTemplate in Spring Boot (> 1.1.0) Spring-配置Apache Commons电子邮件 - Spring - config apache commons email 如何在Spring JDBCTemplate中为数据源设置区分大小写``关闭&#39;&#39; - How do i set case sensitivity 'off' for a Datasource in spring jdbctemplate Spring 工厂方法“jdbcTemplate”抛出异常; 属性“数据源”是必需的 - Spring Factory method 'jdbcTemplate' threw exception; Property 'dataSource' is required Spring JDBCTemplate 与 Hikari 数据源批量更新行为异步 - Spring JDBCTemplate with Hikari datasource batch update behaving asynchronously Kubernetes for MySQL 中的 Spring DataSource - Spring DataSource in Kubernetes for MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM