spring @Transactional 不起作用?

发布于 2024-10-06 17:25:04 字数 2876 浏览 0 评论 0原文

我目前构建一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2024-10-13 17:25:04

可能是事务不起作用,因为您的 mysql 表不是 InnoDB 类型。

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

昔日梦未散 2024-10-13 17:25:04

您是否已在 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>

无论哪种方式,您都应该在此处发布您的 Spring XML 配置以获得额外的诊断帮助。请参阅 事务管理了解更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文