Could not obtain transaction-synchronized Session for current thread

15,577

Solution 1

Just found it ... @EnableTransactionManagement was missing on my root configuration class.

Thanks everybody for the guidance.

Solution 2

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 interface in the controller.

If it is the case, create an interface IPersonService containing relevant method from PersonService and import it in PersonController as @Autowired IPersonService personService; or even better, rename PersonService to PersonServiceImpl and make PersonService an interface.

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

Share:
15,577
yglodt
Author by

yglodt

Software architect and full-stack developer with 15+ years of experience in software design and development, as well as in management of small teams of developers. Current position is Deputy Head of Business Development and Digitalization. I have created or co-created many web applications and software tools from scratch, and often also managed the Linux infrastructure behind them. My experience includes business process, domain model and database design, software architecture, agile development, responsive user interfaces, reporting, and maintenance of existing code bases. The technologies and tools I use, or have used in the past, are Java, Spring, Spring Boot, Hibernate, Thymeleaf, JSP, JasperReports, PHP, PostgreSQL, MySQL, Firebird, H2 Database, Polymer, Angular, JavaScript and Typescript, CSS, HTML, XML, SQL, REST, SOAP, Visual Studio Code, Eclipse, Maven, Subversion, Git, Bash, Ubuntu, CentOS, MS Windows, Samba, Wine. I enjoy to use open-source tools and components as much as possible. Other things I was working on are SmartHome (KNX) and IOT systems based on Raspberry Pi or ESP32.

Updated on August 02, 2022

Comments

  • yglodt
    yglodt almost 2 years

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

    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 @Bean configured for DataSource, SessionFactory, HibernateTransactionManager, ImprovedNamingStrategy.

    All my @Service services are annotated with @Transactional.

    Any idea where this could come from ?

    Edit 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

    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.