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

标签 java mysql database hibernate

在我的应用程序中,我使用 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/

相关文章:

php - 从sql php获取所有数据

mysql - 如何在大型数据库中优化数据库这个查询?

java - 在 Android < 2.0 上更新联系信息

java - XStream - 解码 - XML 中指定的类型不可见

java - 面板或按钮未显示在框架上

mysql - 简化mysql中的sum of sum查询

mysql - 如何编辑此 mySQL 以显示每天的结果?

database - GAE 数据存储的前端 - 类似 phpMyAdmin 的 GAE

java - a4j :commandLink stops working after being reRender

PHP 语法相当于 SQL %