开发者

spring @Transactional don't work?

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="transactionManage开发者_运维百科r" />


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


May be the transaction is not working because your mysql table is not InnoDB type.


Do you have all of the required elements defined in your Spring XML configuration?

<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. See Transaction Management for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜