Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动刷新/提交过程中Spring Hibernate回滚失败_Spring_Hibernate_Transactions - Fatal编程技术网

自动刷新/提交过程中Spring Hibernate回滚失败

自动刷新/提交过程中Spring Hibernate回滚失败,spring,hibernate,transactions,Spring,Hibernate,Transactions,这是我的第一篇文章,所以我真的很挠头,因为这里的资源非常好 SpringMVC4.2.5。hibernate-core-5.1.0 JDK8 我遇到了一个问题,当离开一个被调用的@Transactional方法时,事务没有回滚。我调用的方法具有适当的设置: @Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor = Exception.class) public byte process(CommonD

这是我的第一篇文章,所以我真的很挠头,因为这里的资源非常好

SpringMVC4.2.5。hibernate-core-5.1.0 JDK8

我遇到了一个问题,当离开一个被调用的@Transactional方法时,事务没有回滚。我调用的方法具有适当的设置:

@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor = Exception.class)
public byte process(CommonDataBlock commonDataBlock) throws Exception   {
该方法调用dao对象,假设我们保存大约8个对象(“到数据库”),例如:

对于典型的dao,只需执行以下操作:

this.getSessionFactory().getCurrentSession().save(appParty);
首先,我从一个普通的POJO调用上面的事务方法“process”:

        try {
        // Database transaction starts/ends here
        submitAccess.process(commonDataBlock);
    }
        catch (Exception e) {
            logUtil.debug(this, "Error submitting application: "+e.getMessage());
            e.printStackTrace();
            return Codes.RET_BYPASS_ALL_ADAPTORS;
    }
(该注释不太正确,数据库事务以“process”方法开始和结束。)

所以-当“进程”中的代码中出现错误时,所有代码都会“回滚”(有意引用)。但当出现数据库错误(如FK问题或NOT NULL列保留为NULL)时,不会回滚

上面的引用是有意的,因为我意识到它实际上并没有像我所想的那样成功地从“process”方法中的错误中回滚,因为它还没有写入数据库。这是在出口时做的

在进程方法和调用pojo之间发生数据库错误。Hibernate已经存储了第一级数据,并开始将所有对象写入数据库

我尝试过在“process”方法中对所有内容进行try/catch,但由于没有发生错误(当它离开该方法时,操作开始!),因此没有抛出和捕获任何内容

调用的pojo确实捕获了一个错误(并且可以看到NOTNULL的数据异常错误),但是它已经将对象写入数据库,并在错误发生之前将它们留在那里

rollbackFor是好的,但故障似乎在那之后-它捕获该方法中的错误,但当它离开该方法,Spring/Hibernate完成它的工作并提交时。。。它留在数据库里。没有回滚。因此,rollbackFor似乎只适用于关联方法中的错误,而不适用于之后自动发生的错误(write-behind)

几乎与自动提交行为相似,但Hibernate(Oracle 10)不应如此

有什么想法吗?今天我将放一个flush(),看看这是否会导致process方法中出现错误,从而被rollboor捕获。。。但我们不应该这么做?(如果可行的话)


谢谢,明白了。问题是我要更改事务管理器的名称:

<bean id="transactionManager_guardianship"  class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory_guardianship" />
    <property name="dataSource"><ref bean="dataSource_guardianship"/></property>
</bean>
这是针对事务管理器名称更改时,以及(更改原因)我有两个事务管理器时。类似的@Transactional(值=另一个的设置)

干杯

<bean id="transactionManager_guardianship"  class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory_guardianship" />
    <property name="dataSource"><ref bean="dataSource_guardianship"/></property>
</bean>
<bean id="transactionManager_guardianship"  class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory_guardianship" />
    <property name="dataSource"><ref bean="dataSource_guardianship"/></property>
    <qualifier value="txGuardianship"/>
</bean>
@Transactional(value="txGuardianship", propagation=Propagation.REQUIRED,readOnly=false,rollbackFor = Exception.class)
public byte process(CommonDataBlock commonDataBlock) throws Exception   {