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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26179159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:26:50  来源:igfitidea点击:

Could not obtain transaction-synchronized Session for current thread

javaspringhibernatespring-java-config

提问by yglodt

I am getting the following Exception with a Spring4/Hibernate4 project I converted from xml- to Java-Config.

我从 xml- 转换为 Java-Config 的 Spring4/Hibernate4 项目出现以下异常。

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

The project starts up property and errorfree in Eclipse, but on the first request the Exception appears. In my ConfigRoot-class I have @Beanconfigured for DataSource, SessionFactory, HibernateTransactionManager, ImprovedNamingStrategy.

该项目在 Eclipse 中启动 property 和 errorfree,但在第一个请求时出现异常。在我的ConfigRoot-class 中,我@BeanDataSource, SessionFactory, HibernateTransactionManager,进行了配置ImprovedNamingStrategy

All my @Serviceservices are annotated with @Transactional.

我所有的@Service服务都用@Transactional.

Any idea where this could come from ?

知道这可能来自哪里吗?

Edit 1

编辑 1

As requested, here the 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)

Edit 2

编辑 2

Strangely, I borrowed the whole Java-Config code from another project which works flawlessly, certainly I missed a detail. That's why I am not considering Some transaction propagations not working with Spring/Hibernate 4.

奇怪的是,我从另一个完美无缺的项目中借用了整个 Java-Config 代码,当然我错过了一个细节。这就是为什么我不考虑某些事务传播不适用于 Spring/Hibernate 4 的原因

回答by yglodt

Just found it ... @EnableTransactionManagementwas missing on my root configuration class.

刚刚找到它......@EnableTransactionManagement我的根配置类中缺少它。

Thanks everybody for the guidance.

谢谢大家的指导。

回答by Serge Ballesta

I presume it is a problem of transactional proxy not being used (just a guess from the stacktrace). By default, spring uses a Jdk proxy for it, but that needs the service to be imported as an interfacein the controller.

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

If it is the case, create an interface IPersonServicecontaining relevant method from PersonServiceand import it in PersonControlleras @Autowired IPersonService personService;or even better, rename PersonServiceto PersonServiceImpland make PersonServicean interface.

如果是的话,创建一个接口IPersonService包含来自相关方法PersonService和导入PersonController@Autowired IPersonService personService;,甚至更好,重命名PersonService,以PersonServiceImpl做出PersonService一个接口。

And ... consistently do that for all your transactional services ...

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

回答by ArunDhwaj IIITH

Such scenario:

这样的场景:

Use this Thumb Rule:

使用此经验法则

1) For Every @Entity @Table(name = "Table1")

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

Create corresponding Services, and Corresponding DAO.

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