0

We are migrating from Spring 3 to Spring 4 along with introducing EntityManager. Earlier in the code we had used OpenSessionInViewInterceptor as interceptor in the bean with urlmapping. It was working fine by attaching a session to every thread request from web. Now I am using OpenEntityManagerInViewInterceptor but the EntityManager is not attached to the current thread, and get the exception No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call If I put @Transactional above my DAO method then it works fine, but I will have to do it in lot of places, so want to avoid it and trying to fix it with OpenEntityManagerInViewInterceptor. Please let me know how can I get it working.

applicationContext.xml -

<bean id="openEntityManagerInViewInterceptor" class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">  
                <property name="entityManagerFactory" ref="entityManagerFactory" />  
            </bean>

dispatcher-servlet.xml -

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>                
                <prop key="/EditHelp/*.do">EditHelpController</prop>
            </props>
        </property>
        <property name="interceptors">
            <list>
                <ref bean="openEntityManagerInViewInterceptor" />
            </list>
        </property>    
    </bean>

Any help would be appreciated. Thank you.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
kapil gupta
  • 335
  • 3
  • 19
  • Possible duplicate of [Spring - No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call](https://stackoverflow.com/questions/32269192/spring-no-entitymanager-with-actual-transaction-available-for-current-thread) – Strelok Oct 29 '18 at 11:16
  • So you aren't only upgrading Spring but also changing the way you are doing persistence... Also the fact that you didn't have transactions is basically a bit worrying as you shouldn't do database persistence without a transaction (hence the error/warning you get). The only right way to fix this is to use transactions, anything else isn't. Next to that you should be doing 1 thing at at the time, first do the upgrade, then switch your persistence (or vice-versa) but don't do multiple things at once. It complicates things and leads to confusion. – M. Deinum Oct 29 '18 at 11:25
  • I am new to JPA, not sure whether my understanding is correct or not. As per javadoc of OpenEntityManagerinViewInterceptor the entity manager is associated with current thread request : "Spring web request interceptor that binds a JPA EntityManager to the thread for the entire processing of the request. Intended for the "Open EntityManager in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed. This interceptor makes JPA EntityManagers available via the current thread, which will be autodetected by transaction managers" – kapil gupta Oct 30 '18 at 04:07
  • My application is very big, so cannot change the code that much, please let me know if there is any easy solution to solve it – kapil gupta Oct 30 '18 at 04:23

0 Answers0