0

当我的代码中发生异常时,我想支持回滚。

我在 Spring 配置文件中使用 junit + 数据源进行测试,使用 Glassfish 2.1 进行实际代码(使用 jndi 数据源)。

这里是代码示例。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-web-spring-config.xml" })
public class PersistTest {

    @Autowired
    Transformer transformer;

    @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    @Transactional("transactionManagerTest")
    //@Rollback(false)
    // @Ignore
    public void test() {

        transformer.export();

    }

}

@Component
public class Transformer {

    @Autowired
    ContextPersist context;

    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void export(){
        //code here
        // persist here
        context.persist();
        // to test a rollback
        throw new RuntimeException("testing rollback2");

    }

}

@Component
public class ContextPersist {

    @Autowired
    @Qualifier(value = "dataSource")
    DataSource dataSource;

    // bulk insert
    JdbcTemplate jdbcTemplate;

    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void persist() {
        jdbcTemplate = new JdbcTemplate(dataSource);

        //.. here I insert data with jdbcTemplate.batchUpdate(....)

        // to test a rollback
        throw new RuntimeException("testing rollback1");
    }
}

该代码不会回滚。

如果我在我的 Junit 中使用 @Rollback(true),事务将回滚。但我需要 JUnit 之外的相同行为。

编辑:(添加弹簧配置)

我的项目包含一个 webapp (demo.war) 和一个用于 DAO+businessrules 的 jar

在我的网络应用程序中,我有我的变压器。

我在这个 webapp 中有一个父 Spring 配置,以及一个与其他 webapps 共享的公共 spring 配置。

这里是文件。

演示.war

web-spring-config.xml

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    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-3.0.xsd 
         http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <context:component-scan base-package="com.test" />
    <tx:annotation-driven />
    <task:annotation-driven/>

    <import resource="classpath:common-spring-config.xml" />

</beans>

DAO.jar

common-spring-config.xml

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:task="http://www.springframework.org/schema/task"
    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-3.0.xsd 
        http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <context:component-scan base-package="com.test" />
    <tx:annotation-driven />
    <task:annotation-driven/>

    <!-- Hibernate -->
    <bean id="hibernateSessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="demo.datasource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="hibernateSessionFactory" />
    </bean>

    <!-- datasource -->
    <bean id="demo.datasource" class="org.springframework.jndi.JndiObjectFactoryBean"
        lazy-init="true">
        <property name="jndiName" value="jdbc/demo" />
    </bean>

</beans>
4

1 回答 1

0

你的交易经理是

org.springframework.orm.hibernate3.HibernateTransactionManager

但你用JdbcTemplate

AOP 配置为在休眠操作时自动开始/提交/回滚。jdbcTemplate 不会参与任何事务。没有事务=没有回滚。它就像 connection.setAutoCommit(true);

于 2013-11-13T10:38:13.600 回答