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

当内部事务引发异常时,Spring@Transactional(propagation=propagation.REQUIRED)不会推出外部事务,spring,transactions,spring-transactions,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");

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

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>

org.hibernate.dialogue.mysql5dialogue
真的
由于两个bean中的testRequired()方法都标记为required,因此当内部事务抛出异常时,外部事务必须展开该事务。但这不是在这里发生的。 谁能告诉我我做错了什么


谢谢,

在OuterBeanImpl上引发异常时是否有回滚功能?
启用日志记录并记录spring事务调试消息。这可能有助于了解发生了什么。

您正在使用哪个transactionManager?@Massimo:@Massimo:FYI,我指的是以下教程:您是否尝试过使用JtaTransactionManager?就像这里所表达的:我猜HibernateTM不支持这种传播技巧,但是我可能会被误解,如果你也指定的话?