如何在Spring + JDBC中配置关闭自动提交? [英] How can I config to turn off autocommit in Spring + JDBC?

查看:175
本文介绍了如何在Spring + JDBC中配置关闭自动提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring和JDBC,并发现它是自动提交。

I am using Spring with JDBC and found that it is autocommit.

我如何配置在spring-servlet.xml中将其关闭?

How can I config to turn it off in spring-servlet.xml?

这是我当前的配置:

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
    p:password="${jdbc.password}" />

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"/>
</bean>


推荐答案

似乎我的配置错过了这一行:

It seems that my configuration missed this line:

<tx:annotation-driven transaction-manager="txManager"/>

然后,在我的服务类中,我使用@Transactional注释。例如

Then, in my service classes, I use @Transactional annotation. For example

@Service
class CompanyServiceImpl implements CompanyService{
    @Autowired
    private CompanyDAO companyDAO;

    @Transactional
    public void addCompany(Company company) {
            companyDAO.addCompany(company); // in here, there is JDBC sql insert
            companyDAO.addCompany_fail(company); // just for test
    }
}

如果发生异常在addCompany_fail()中,第一个addCompany()也将被回滚。

If there is a exception happening in the addCompany_fail(), the first addCompany() one will also be rollbacked.

我按照这个文档来理解Spring中如何控制事务。
http:// static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html

I followed this document to understand idea how transaction controlled in Spring. http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html

我按照本文档了解如何在Spring中使用JDBC进行编码。
http:// static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html

I followed this document to understand how to code with JDBC in Spring. http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html

我也看过这个(免费) ) http://www.infoq.com/news/2009 / 04 / Java的交易模型策略。这真的很棒。对于大多数人不理解(或关心)交易的作者,我也有同样的感受。

I also read this (Free) http://www.infoq.com/news/2009/04/java-transaction-models-strategy. It is really good one. And I feel the same with the writer that most people do not understand (or care) about transaction.

PS:
似乎很多人误解了这样做Hibernate / Spring框架仅用于避免JDBC和事务控制的复杂性。许多人认为JDBC和Transaction是如此复杂,只需使用Hibernate而忘记这两者。关于Spring + Hibernate或Spring + JDBC的互联网上的许多例子似乎根本不关心事务。我觉得这是一个糟糕的笑话。事务太严重了,只是让事情处理它而没有真正理解。

PS: Seem that many people misunderstand that using such Hibernate/Spring framework is only for avoid complexity of JDBC and Transaction Control. Many people think like "JDBC and Transaction are so complex, just use Hibernate and forget about those two". Many examples on the internet about Spring+Hibernate or Spring+JDBC seemingly not care about transaction at all. I feel that this is a bad joke. Transaction is too serious for just letting something handle it without truly understanding.

Hibernate和Spring是如此强大和如此复杂。然后,正如有人所说,强大的力量伴随着责任。

Hibernate and Spring is so powerful and so complex. Then, as someone said, "Great power comes with responsibilities".

更新:2013-08-17:关于交易有很好的例子 http://www.byteslounge.com/tutorials/spring-transaction-propagation -tutorial 。但是,这并不能说明如果你想使用REQUIRES_NEW,为什么你需要创建另一个类(否则你会遇到这个问题 Spring Transaction propagation required REQUIRED,REQUIRES_NEW ,看起来REQUIRES_NEW并没有真正创建新的交易)

UPDATE: 2013-08-17: There are good example about transaction here http://www.byteslounge.com/tutorials/spring-transaction-propagation-tutorial. However, this is not explain that if you want to use REQUIRES_NEW, why you need to create another class (otherwise you will get this problem Spring Transaction propagation REQUIRED, REQUIRES_NEW , which it seems REQUIRES_NEW does not really create a new transaction)

更新:2018-01-01:我在这里用Spring Boot 1.5.8.RELEASE创建了一个完整的例子 https://www.surasint.com/spring-boot-database-transaction-jdbi/
以及此处的一些基本实验示例 https://www.surasint.com/spring-boot-connection-transaction/

Update: 2018-01-01: I have created a full example with Spring Boot 1.5.8.RELEASE here https://www.surasint.com/spring-boot-database-transaction-jdbi/ and some basic experiment examples here https://www.surasint.com/spring-boot-connection-transaction/

这篇关于如何在Spring + JDBC中配置关闭自动提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆