org.hibernate.TransactionException:不支持嵌套事务

时间:2016-08-02 08:31:48

标签: java spring hibernate

我正在使用spring core和hibernate集成。虽然我使用以下代码

    @Transactional
public void updateExecutedStatus(Processing_File_data file,int status) throws SQLException {
    System.out.println(this.getClass().getName()+"and method name is updateExecutedStatus start");  
    /*synchronized (this) {*/

    //Session session=sessionFactory.openSession();
    Session session=sessionFactory.getCurrentSession();
    if(session==null||session.equals(null)){
        session=sessionFactory.openSession();
    }
            try { 

                //Transaction tx1 = sessionFactory.getCurrentSession().beginTransaction();   

                Transaction tx1 = session.beginTransaction();
                file.setStatus(status);
                file.getLdb().setExecuted(status);
                session.saveOrUpdate(file);
            tx1.commit();
            }
            catch (Exception e) {
                //log.error(e.getMessage());
                e.printStackTrace();
            }finally{
                session.close();
            }

    /*}*/
            System.out.println(this.getClass().getName()+"and method name is updateExecutedStatus end");        
}

我收到以下异常。

org.hibernate.TransactionException: nested transactions not supported
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:154)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)
at pe.entel.dao.impl.MasDatabaseTransactImpl.updateExecutedStatus(MasDatabaseTransactImpl.java:147)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy23.updateExecutedStatus(Unknown Source)
at pe.entel.service.MassiveDatabaseServiceImpl.updateExecutedStatus(MasDatabaseServiceImpl.java:54)
at pe.entel.service.activation.masivas.servadd.core.SeMain.handler(SeMain.java:284)
at pe.entel.integrated.MasiRoot.getlists(MasiRoot.java:71)
at pe.entel.integrated.RecordsDistributedThread.run(RecordsDistributedThread.java:44)

我在事务上应用线程。而我正在尝试没有线程,代码工作正常。但是当应用线程获得上述异常时。

1 个答案:

答案 0 :(得分:0)

您无法在方法内部启动新事务,该方法使用@Transactional进行注释。因此,您必须删除方法中的交易哈希或@Transactional注释

@Transactional
public void updateExecutedStatus(Processing_File_data file,int status) throws SQLException {
    System.out.println(this.getClass().getName()+"and method name is updateExecutedStatus start");  
    /*synchronized (this) {*/

    //Session session=sessionFactory.openSession();
    Session session=sessionFactory.getCurrentSession();
    if(session==null||session.equals(null)){
        session=sessionFactory.openSession();
    }
            try { 

                 file.setStatus(status);
                file.getLdb().setExecuted(status);
                session.saveOrUpdate(file);
            }
            catch (Exception e) {
                //log.error(e.getMessage());
                e.printStackTrace();
            }

    /*}*/
            System.out.println(this.getClass().getName()+"and method name is updateExecutedStatus end");        
}