当内部事务抛出异常时,Spring @Transactional(propagation = Propagation.REQUIRED)不会推出外部事务

时间:2015-06-17 19:30:21

标签: spring transactions spring-transactions

我在以下教程中使用了以下代码:

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!");
    }
}

我的弹簧配置如下:

<?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()都标记为必需,因为内部事务抛出异常,外部事务必须推出事务。但这不会发生在这里。 任何人都可以建议我做错了吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

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