spring @Transactional 不起作用?
我目前构建一个使用 spring 作为框架的应用程序。我想使用 spring 测试批量事务。这是我的代码:
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);
}
}
}
我尝试插入与下面相同的主键。
@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";
}
当它以 Id 1 执行时,它会抛出错误,我希望所有查询都会回滚。但有 7 个进入数据库。为什么不能回滚?我哪里错了?
我使用ibatis和mysql作为数据库。
这是 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>
i currently build an application use spring as framework. and i want to test batch transaction using 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. but 7 get into the database. why it cannot rollbacked? where am i wrong?
i use ibatis and mysql as database.
and here is xml configuration :
<!-- 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是事务不起作用,因为您的 mysql 表不是 InnoDB 类型。
May be the transaction is not working because your mysql table is not InnoDB type.
您是否已在 Spring XML 配置中定义了所有必需的元素?
无论哪种方式,您都应该在此处发布您的 Spring XML 配置以获得额外的诊断帮助。请参阅 事务管理了解更多信息。
Do you have all of the required elements defined in your Spring XML configuration?
Either way, you should probably post your Spring XML configuration here to for additional diagnostic help. See Transaction Management for more information.