【问题标题】:Spring @Transactional(propagation=Propagation.REQUIRED) does not roll out outer transaction when inner transaction throws an exception当内部事务抛出异常时,Spring @Transactional(propagation=Propagation.REQUIRED) 不会推出外部事务
【发布时间】:2015-06-17 19:30:20
【问题描述】:

我使用了一些教程中的以下代码:

public class Main 
{
    public static void main( String[] args )
    {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        OuterBean testBean = (OuterBean) ctx.getBean("outerBeanImpl");

        User user = new User();
        user.setUsername("johndoe");
        user.setName("John Doe");
        try{
            testBean.testRequired(user);
        } catch(Exception e){
            System.out.println("in main class");
            e.printStackTrace();
        }
    }
}

OuterBeanImpl 类

@Service
public class OuterBeanImpl implements OuterBean {

    @Autowired
    private TestDAO testDAO;

    @Autowired
    private InnerBean innerBean;

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void testRequired(User user) {
        testDAO.insertUser(user);
        try{
            innerBean.testRequired();
        } catch(RuntimeException e){
            System.out.println("OuterBeanImpl class");
        }
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void testRequiresNew(User user) {
        testDAO.insertUser(user);
        try{
            innerBean.testRequiresNew();
        } catch(Exception e){
            // handle exception
        }
    }

}

@Service
public class InnerBeanImpl implements InnerBean {

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void testRequired() {
        System.out.println("Rollback this required transaction!");
        throw new RuntimeException("Rollback this required transaction!");
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public void testRequiresNew() {
        System.out.println("Rollback this new transaction!");
        throw new RuntimeException("Rollback this new transaction!");
    }
}

我的spring配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <tx:annotation-driven />

    <context:component-scan 
        base-package="com.byteslounge.spring.tx.dao.impl" />
    <context:component-scan 
        base-package="com.byteslounge.spring.tx.test.impl" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="hibernateProperties">
          <props>
             <prop 
             key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
             <prop key="hibernate.show_sql">true</prop>
          </props>
       </property>
       <property name="packagesToScan" value="com.byteslounge.spring.tx.model" />
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
        p:sessionFactory-ref="sessionFactory">
    </bean>

</beans>

由于两个 bean 中的方法 testRequired() 都被标记为必需,因为内部事务抛出异常,外部事务必须推出该事务。但这不是在这里发生的。 谁能建议我做错了什么?

谢谢,

【问题讨论】:

标签: spring transactions spring-transactions


【解决方案1】:

在 OuterBeanImpl 抛出异常时是否有回滚? 启用日志记录并记录 spring 事务调试消息。这有助于了解正在发生的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 2017-07-24
    • 1970-01-01
    • 2017-03-11
    • 2016-11-16
    • 1970-01-01
    • 2014-05-12
    • 2018-08-05
    相关资源
    最近更新 更多