[英]spring @Transactional don't work?
i currently build an application use spring as framework. 我目前使用Spring作为框架来构建应用程序。 and i want to test batch transaction using spring.
我想使用spring测试批量交易。 here is my code :
这是我的代码:
public class SqlMapTestDao extends SqlMapClientDaoSupport implements TestDao {
public List<Test> getAllTest() {
return getSqlMapClientTemplate().queryForList("getAllTest");
}
public Test getTest(int param) {
return (Test)getSqlMapClientTemplate().queryForObject("getTest" , param);
}
public void insertTest(Test test) {
getSqlMapClientTemplate().insert("insertTest", test);
}
@Transactional(readOnly = false)
public void insertBatch(List<Test> batch) throws SQLException{
for(Test test : batch) {
getSqlMapClientTemplate().insert("insertTest", test);
}
}
}
and i try to insert a same primary key as bellow. 我尝试插入与波纹管相同的主键。
@Autowired
private TestDao testDao;
@RequestMapping(value="/", method=RequestMethod.GET)
public String home(@ModelAttribute Account acc) {
List<Test> test = new ArrayList<Test>();
test.add(new Test(7, "ini empat"));
test.add(new Test(1, "ini satu"));
test.add(new Test(8, "ini lima"));
try {
testDao.insertBatch(test);
}catch (Exception e) {
logger.error("Error", e.getStackTrace());
}
logger.info("Welcome Home");
return "home";
}
when it execute with Id 1, it will throw error, and i expect all query will be rollback. 当它以ID 1执行时,将引发错误,并且我希望所有查询都将回滚。 but 7 get into the database.
但是7进入数据库。 why it cannot rollbacked?
为什么不能回滚? where am i wrong?
我哪里错了?
i use ibatis and mysql as database. 我使用ibatis和mysql作为数据库。
and here is xml configuration : 这是xml配置:
<!-- Configures transaction management around @Transactional components -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Resource loader for jdbc configuration -->
<context:property-placeholder location="WEB-INF/jdbc.properties"/>
<!-- Local Apache Commons DBCP DataSource that refers to a combined database -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- SqlMap setup for iBATIS Database Layer -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/sql-map-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- testing purpose -->
<bean id="testDao" class="com.shop.cart.dao.ibatis.SqlMapTestDao">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
事务可能无法正常运行,因为您的mysql表不是InnoDB类型。
Do you have all of the required elements defined in your Spring XML configuration? 您在Spring XML配置中是否定义了所有必需的元素?
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="dataSource"/>
</bean>
Either way, you should probably post your Spring XML configuration here to for additional diagnostic help. 无论哪种方式,您都应该将Spring XML配置发布到此处以获取其他诊断帮助。 See Transaction Management for more information.
有关更多信息,请参见事务管理 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.