0
votes

I am working a product build in spring 3 and hibernate 3, i have updated this hibernate 5 and Spring 5. Now i am able to deploy the application, but when I try to connect with database then getting this "javax.persistence.TransactionRequiredException: no transaction is in progress"

<bean id="sessionFactoryBean"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceBean" />
    <property name="annotatedClasses">          
        <!-- The model class is incorporated here -->
        <list>               
             <value>com.test.ab.base.AaUser</value>
             <value>com.test.ab.dealer.AaDealer</value>
           <list>

<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactoryBean" />
</bean>

And this is my Dao Layer:

public class DatabaseConnection extends HibernateDaoSupport {

    public DatabaseConnection (){

    }

    public static String QUERY_PREFIX_COUNT = "SELECT COUNT(*) ";

    protected void applyNamedParameterToQuery(Query queryObject, String paramName, Object value)
            throws HibernateException {

        if (value instanceof Collection) {
            queryObject.setParameterList(paramName, (Collection) value);
        }
        else if (value instanceof Object[]) {
            queryObject.setParameterList(paramName, (Object[]) value);
        }
        else {
            queryObject.setParameter(paramName, value);
        }
    }

    protected String save(Object obj) throws HibernateException {
        return getHibernateTemplate().save(obj).toString();
    }

    protected void update(Object obj) throws HibernateException {
        getHibernateTemplate().update(obj);
    }

    //this is to be used to attached the detached object in session...
    protected void merge(Object obj) throws HibernateException {
        getHibernateTemplate().merge(obj);
    }

    protected void saveOrUpdObj(Object obj) throws HibernateException {
        getHibernateTemplate().saveOrUpdate(obj);
    }
}

javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:398) at org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3584) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1462) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1458) at org.springframework.orm.hibernate5.SessionFactoryUtils.flush(SessionFactoryUtils.java:148) at org.springframework.orm.hibernate5.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:95) at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:96) at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:919) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:727) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:631) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:385) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy89.getAllAbModuleUserMapping(Unknown Source) at com.actionbean.ab.base.WelcomeActionManager.fillModules(WelcomeActionManager.java:249) at com.actionbean.ab.base.WelcomeActionManager.createUsrSessBean(WelcomeActionManager.java:211) at com.actionbean.ab.base.WelcomeActionManager.retrieveSessionUserDataInternal(WelcomeActionManager.java:83) at com.actionbean.ab.base.WelcomeActionManager.retrieveSessionUserData(WelcomeActionManager.java:61) at com.actionbean.ab.base.WelcomeAction.performExecute(WelcomeAction.java:47) at com.actionbean.ab.base.BaseAction.execute(BaseAction.java:153) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at com.actionbean.ab.filter.AbFilter.doFilter(AbFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:444) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1137) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:398) at org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3584) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1462) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1458) at org.springframework.orm.hibernate5.SessionFactoryUtils.flush(SessionFactoryUtils.java:148) at org.springframework.orm.hibernate5.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:95) at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:96) at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:919) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:727) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:631) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:385) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy89.getAllAbModuleUserMapping(Unknown Source) at com.actionbean.ab.base.WelcomeActionManager.fillModules(WelcomeActionManager.java:249) at com.actionbean.ab.base.WelcomeActionManager.createUsrSessBean(WelcomeActionManager.java:211) at com.actionbean.ab.base.WelcomeActionManager.retrieveSessionUserDataInternal(WelcomeActionManager.java:83) at com.actionbean.ab.base.WelcomeActionManager.retrieveSessionUserData(WelcomeActionManager.java:61) at com.actionbean.ab.base.WelcomeAction.performExecute(WelcomeAction.java:47) at com.actionbean.ab.base.BaseAction.execute(BaseAction.java:153) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at com.actionbean.ab.filter.AbFilter.doFilter(AbFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:444) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1137) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)

1
i don't know the rest of your source code, but do you have a transaction open in your workflow? when does this exception happen?Marco Behler
@Override public Transaction getTransaction() throws HibernateException { if ( getFactory().getSessionFactoryOptions().getJpaCompliance().isJpaTransactionComplianceEnabled() ) { // JPA requires that we throw IllegalStateException if this is called // on a JTA EntityManager if ( getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta() ) { if ( !getFactory().getSessionFactoryOptions().isJtaTransactionAccessEnabled() ) { throw new IllegalStateException( "A JTA EntityManager cannot use getTransaction()" ); } } }Sachin Kumar
can u tell how how u resolved it?user124

1 Answers

0
votes

You have missed dependency injection dataSource for txManager so the bean entry should look like

<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    <property name="sessionFactory" ref="sessionFactoryBean" />
</bean>