我有一个Java Spring应用程序连接到SQL Server数据库。
连接设置为:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:jtds:sqlserver://${db.host}:1433/TestDB" />
<property name="user" value="${db.user}" />
<property name="password" value="${db.pass}" />
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="acquireIncrement" value="5"/>
<property name="maxIdleTime" value="30000" />
</bean>
一切正常但有时我收到以下错误:
Could not open JDBC Connection for the transaction; nested exception is java.sql.SQLException: I/O Error: Read timed out
我搜索了很多,但找不到任何线索,任何想法或帮助?
我正在使用
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
在我的spring-config xml
中获取我的sqlSession
,并在DAO中使用:
@Autowired
SqlSession sqlSession;
然后我执行我想要的查询。是否可能因为连接未关闭而存在此错误?
答案 0 :(得分:0)
在我的情况下,当夜间数据库备份作业被触发时,连接被删除。我也使用jtds / sql-server。以下是我要解决的问题:
select from
。每10分钟左右调用一次并记录结果。它将为您提供有关何时以及为何发生这种情况的一些反馈。请记住,如果您不更改maxIdleTime并且保持多个连接处于打开状态,即使您使用运行状况检查功能,其中一些连接仍可能处于错误状态。引自c3p0 documentation:
默认情况下,池永远不会过期。如果您希望Connections随着时间的推移而过期以保持&#34; fresh&#34;,请设置maxIdleTime和/或maxConnectionAge。 maxIdleTime定义在从池中剔除之前应允许Connection未使用的秒数。 maxConnectionAge强制池剔除从数据库中获取的任何连接超过过去设置的秒数。
设置健康检查呼叫的另一种方法是使用idleConnectionTestPeriod参数。另请查看answer,这可以为您提供有关如何设置的更多信息。