我在 ORMLite 文档的示例 7 中配置了 ORMLite,但过了一会儿我收到连接关闭错误
java.sql.SQLException: Connection has already been closed
at com.j256.ormlite.jdbc.JdbcConnectionSource.getReadWriteConnection (JdbcConnectionSource.java:177)
at com.j256.ormlite.jdbc.JdbcConnectionSource.getReadOnlyConnection(JdbcConnectionSource.java:168)
at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:228)
at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:181)
at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:263)
at com.tube.auction.db.DbFacade.createOrUpdateUser(DbFacade.java:265)
我在代码中只使用 dao bean。
<bean id="databaseUrl" class="java.lang.String">
<constructor-arg index="0" value="jdbc:mysql://localhost:3306/auction" />
</bean>
<bean id="connectionSource" class="com.j256.ormlite.jdbc.JdbcConnectionSource" init-method="initialize">
<property name="url" ref="databaseUrl" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="userDao" class="com.j256.ormlite.spring.DaoFactory" factory-method="createDao">
<constructor-arg index="0" ref="connectionSource" />
<constructor-arg index="1" value="com.tube.auction.dto.User" />
</bean>
如何处理连接关闭问题?我看不到任何强制连接重启的方法例如注意 isOpen() for dao.connection返回 true
-- 1. Can i test connection here and restart it? How? Code snippet?
try {
userDao.queryForId(someUserId);
} catch(SQLException ex) {
if(is this connection closed?) {
-- 2. What should I do here to restart connection?
}
}