I have used below code from some tutorial:

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 class

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

My spring configuration is as below:

<?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>

As the methods testRequired() in both beans are marked as required, as the inner transaction throws an exception the outer transaction must roll out the transaction. But this is not happening in here. Can anyone suggest as what I am doing wrong?

THanks,

1

There are 1 answers

0
user921176 On

Do you have rollback when throwing exception at OuterBeanImpl? Enable logging and log spring transaction debug messages. That could help seeing what is going on.