在我的应用程序中,我使用hibernate和mysql连接到数据库。当有来自gui的请求时,这个应用程序连接到db。在尝试再次连接到数据库之后,在很长一段时间内没有来自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>
1条答案
按热度按时间zd287kbt1#
你做错了。hibernate会话只能在单个事务的生存期内连接到数据库。完成事务后,也应关闭会话。
您可以先定义transactioncallback:
然后定义助手会话管理方法:
因此,您甚至可以按照建议运行本机查询: