我希望在我的代码中发生异常时支持回滚。
我在Spring配置文件中使用junit + datasource进行测试,使用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中,我有变压器。
我在这个webapp中有一个父Spring配置,并且有一个共享的spring配置与其他webapps共享。
这里是文件。
demo.war
网络的弹簧-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
共弹簧-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>
答案 0 :(得分:0)
您的交易经理
org.springframework.orm.hibernate3.HibernateTransactionManager
但您使用JdbcTemplate
AOP配置为在休眠操作上自动开始/提交/回滚。 jdbcTemplate不会参与任何交易。 没有事务=没有回滚。 它就像connection.setAutoCommit(true);