gpt4 book ai didi

java - MySql 数据库连接错误 Managed ConnectionFactory is null

转载 作者:行者123 更新时间:2023-11-30 22:51:52 24 4
gpt4 key购买 nike

在我的应用程序中,我使用 Hibernate 和 mysql 连接到数据库。只要有来自 GUI 的请求,此应用程序就会连接到数据库。在尝试再次连接到数据库后,如果长时间没有来自 GUI 的请求。我收到以下错误。可能的解决方案是什么?

 org.jboss.util.NestedSQLException: You are trying to use a connection factory that has been shut down: ManagedConnectionFactory is null.; - nested throwable: 
(javax.resource.ResourceException: You are trying to use a connection factory that has been shut down: ManagedConnectionFactory is null.)
at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:95)
at org.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:92)`

这是我连接到数据库的代码。

SessionFactory factory;
factory = new Configuration().configure().buildSessionFactory();
session = factory.openSession();
SessionFactoryImplementor impl = (SessionFactoryImplementor)session.getSessionFactory();
ConnectionProvider cp = impl.getConnectionProvider();
conn = cp.getConnection();//This conn is used for prepared statement and so on..

这是我的mysql ds文件

<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<use-java-context>true</use-java-context>
<url-delimiter>|</url-delimiter>
<connection-url>jdbc:mysql://localhost:3306/TEST_DB?zeroDateTimeBehavior=convertToNull</connection-url>

<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>user</user-name>
<password>****</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<new-connection-sql>select current_date()</new-connection-sql>
<check-valid-connection-sql>select current_date()</check-valid-connection-sql>
<min-pool-size>1</min-pool-size>
<max-pool-size>5</max-pool-size>
<set-tx-query-timeout/>
<query-timeout>300</query-timeout>

<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>

</datasources>

最佳答案

你做错了。 Hibernate session 应该只在单个事务的生命周期内连接到数据库。完成交易后,您也应该关闭 session 。

可以先定义一个TransactionCallback:

public interface TransactionCallable<T> {
public abstract T execute(Session session);
}

然后定义一个Helper session 管理方法:

protected <T> T doInTransaction(TransactionCallable<T> callable) {
T result = null;
Session session = null;
Transaction txn = null;
try {
session = sf.openSession();
txn = session.beginTransaction();

session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {

}
});

result = callable.execute(session);
txn.commit();
} catch (RuntimeException e) {
if ( txn != null && txn.isActive() ) txn.rollback();
throw e;
} finally {
if (session != null) {
session.close();
}
}
return result;
}

因此您甚至可以按照您的建议运行 native 查询:

final String sql = ...;

doInTransaction(new TransactionCallable<Object>() {
Boolean result;
@Override
public Object execute(Session session) {
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
Statement statement = connection.createStatement();
result = statement.execute(sql);
}
});
return result;
}
});

关于java - MySql 数据库连接错误 Managed ConnectionFactory is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27956757/

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