无法获取当前线程的事务同步会话

时间:2014-10-03 12:44:02

标签: java spring hibernate spring-java-config

我通过从xml-转换为Java-Config的Spring4 / Hibernate4项目获得以下异常。

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

项目在Eclipse中启动属性和错误,但在第一次请求时出现Exception。在我的ConfigRoot课程中,我为@BeanDataSourceSessionFactoryHibernateTransactionManager配置了ImprovedNamingStrategy

我的所有@Service服务都注明了@Transactional

知道这可能来自何处?

修改1

根据要求,这里是stacktrace:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
    org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    employees.service.PersonService.getAllInHierarchcalOrder(PersonService.java:58)
    employees.controller.PersonController.getPersons(PersonController.java:64)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

修改2

奇怪的是,我从另一个完美无缺的项目中借用了整个Java-Config代码,当然我错过了一个细节。这就是为什么我不考虑Some transaction propagations not working with Spring/Hibernate 4

3 个答案:

答案 0 :(得分:8)

刚刚发现它...我的根配置类中缺少@EnableTransactionManagement

感谢大家的指导。

答案 1 :(得分:1)

我认为这是一个没有使用事务代理的问题(只是来自堆栈跟踪的猜测)。默认情况下,spring使用Jdk代理,但需要将服务作为接口导入控制器中。

如果是这种情况,请创建一个包含IPersonService相关方法的界面PersonService,然后将其导入PersonController @Autowired IPersonService personService;,或者更好,重命名{{1} } PersonServicePersonServiceImpl接口。

并且......始终如一地为您的所有交易服务做到这一点......

答案 2 :(得分:0)

这种情况:

使用此 Thumb规则

1)每个@Entity @Table(name =" Table1")

创建相应的服务和相应的DAO。