gpt4 book ai didi

mysql - 关闭与 ORMLite 和 MySql 的连接

转载 作者:行者123 更新时间:2023-11-29 00:28:39 27 4
gpt4 key购买 nike

我像 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>

如何处理连接关闭问题?我看不到任何强制重新启动连接的方法例如请注意 dao.connection returns true 的 isOpen()

-- 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?
}
}

最佳答案

JdbcConnectionSource 没有内部保持事件机制。 JdbcPooledConnectionSource 产生一个内部线程来执行保持事件测试。这为我解决了问题。

尝试将 bean 连接源更改为使用 JdbcPooledConnectionSource,如下所示:

<bean id="connectionSource" class="com.j256.ormlite.jdbc.JdbcPooledConnectionSource" init-method="initialize">

关于mysql - 关闭与 ORMLite 和 MySql 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17668538/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com