我在我的应用程序中使用 Spring 和 Hibernate 。我使用 JBoss EAP 6.1 作为我的应用服务器。
我使用 Spring 通过以下代码为 Hibernate 创建sessionFactory
:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<!-- list of annotatedClasses -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.jdbc.batch_size">100</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">$(hibernate.hbm2ddl.auto)</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>
我在 Spring DAO 课程中 自动装配 sessionFactory
:
@Autowired
private SessionFactory sessionFactory;
我使用sessionFactory
如下获取 Hibernate Session
:
Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction();
Query sqlQuery = session.createSQLQuery(query).addEntity(className);
if (null != arg)
sqlQuery.setParameters(arg, type);
list = sqlQuery.list();
session.getTransaction().commit();
} catch (HibernateException ex) {
LOG.error(ex);
session.getTransaction().rollback();
throw ex;
}
sessionFactory.getCurrentSession().close();
如上面的代码所示,我每次都关闭了Hibernate Session
。在我运行我的应用程序大约半小时后,我仍然收到“关闭连接”问题:
17:25:15,500 ERROR [org.hibernate.transaction.JDBCTransaction] (http--0.0.0.0-8080-5) JDBC rollback failed: java.sql.SQLRecoverableException: Closed Connection
at oracle.jdbc.driver.PhysicalConnection.rollback(PhysicalConnection.java:3901) [ojdbc6.jar:11.2.0.2.0]
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.jdbcRollback(BaseWrapperManagedConnection.java:1010)
at org.jboss.jca.adapters.jdbc.WrappedConnection.rollback(WrappedConnection.java:778)
at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:217) [hibernate-core.jar:3.3.0.SP1]
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:196) [hibernate-core.jar:3.3.0.SP1]
at com.ghcm.emailselection.db.DBOperations.selectRecordForBean(DBOperations.java:91) [classes:]
at com.ghcm.emailselection.dao.UserSpecificDetailsDaoImpl.getUserInfo(UserSpecificDetailsDaoImpl.java:34) [classes:]
at com.ghcm.emailselection.service.UserSpecificDetailsImpl.getUserInfo(UserSpecificDetailsImpl.java:32) [classes:]
at com.ghcm.emailselection.dao.LoginDAOImpl.doLogin(LoginDAOImpl.java:97) [classes:]
at com.ghcm.emailselection.service.LoginServiceImpl.doLogin(LoginServiceImpl.java:80) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_22]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_22]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_22]
at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.6.0_22]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319) [spring-aop-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196) [spring-aop-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at $Proxy195.doLogin(Unknown Source) at com.ghcm.emailselection.controller.LoginController.dologin(LoginController.java:69) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_22]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_22]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_22]
at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.6.0_22]
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) [spring-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) [spring-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100) [spring-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604) [spring-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565) [spring-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) [spring-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) [spring-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) [spring-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) [spring-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at com.ghcm.emailselection.controller.CustomAuthenticationFilter.doFilter(CustomAuthenticationFilter.java:77) [classes:]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) [spring-security-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) [spring-web-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:]
at com.utxfrmwk.security.filter.SecurityInterceptor.doFilter(SecurityInterceptor.java:40) [UtxSecurityUtility.jar:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.10.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:154) [jboss-as-web-7.1.0.Final.jar:7.1.0.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.10.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.10.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.10.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.10.Final.jar:]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0_22]
我经常在大约半小时内面对这个问题。我需要使用JNDI池或重启JBOSS app服务器来解决它。
任何人都可以告诉我,我做错了什么或应该采取什么行动来解决“封闭连接”问题?
请帮忙!
答案 0 :(得分:1)
根据您ThreadLocalSessionContext
<prop key="hibernate.current_session_context_class">thread</prop>
this文档
为了可用性,决定让这个默认的impl在第一次请求时实际生成一个会话,然后在与该会话相关联的事务被提交/回滚后进行清理
这意味着当事务提交或回滚时,您的会话将自动关闭。
由于我假设您使用spring来管理事务,因此设置hibernate.current_session_context_class
永远不是一件好事。默认情况下,spring将使用CurrentSessionContext。
现在你在current_session_context_class中使用了线程。那么为了你自己的津津乐道,我会建议
try{
//... your code
} catch(HibernateException ex) {
LOG.error(ex);
session.getTransaction().rollback();
throw ex;
} finally {
if(session.isOpen()){ //I don't think this will return true in any case but still for your satisfaction you could do this
session.close();
}
}
<强>更新强>
从聊天中使用spring数据源bean中的以下配置
<max-pool-size>50</max-pool-size>
<query-timeout>500</query-timeout>
答案 1 :(得分:0)
我建议使用会话的一些方法:
使用openSession():
openSession()会打开一个新会话,完成后你必须关闭它。
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
//get a transaction before performing any operations..
tx = session.beginTransaction();
//perform your transaction here
//commit the transaction as it is success.
tx.commit();
} catch (HibernateException ex) {
//rollback the current transaction, since a exception occurred, while performing transaction
if(tx!=null)
tx.rollback();
throw ex;
}finally{
//finally close the session, since transaction is completed.
session.close();
}
使用getCurrentSession():
正如您在hibernate配置属性hibernate.current_session_context_class
中设置为thread
一样,这意味着当事务完成时,hibernate将自动关闭会话。
//getCurrentSession() returns a session bound to a context, you don't need to close this
Session session = sessionFactory.getCurrentSession();
Transaction tx = null;
try {
//get a transaction before performing any operations..
tx = session.beginTransaction();
//perform your transaction here
//commit the transaction as it is success.
tx.commit();
} catch (HibernateException ex) {
//rollback the current transaction, since a exception occurred, while performing transaction
if(tx!=null)
tx.rollback();
throw ex;
}