简体   繁体   English

如何在Spring + JDBC中配置关闭自动提交?

[英]How can I config to turn off autocommit in Spring + JDBC?

I am using Spring with JDBC and found that it is autocommit. 我使用Spring和JDBC,发现它是自动提交。

How can I config to turn it off in spring-servlet.xml? 如何配置在spring-servlet.xml中将其关闭?

This is my current configuration: 这是我目前的配置:

<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"/>

Then, in my service classes, I use @Transactional annotation. 然后,在我的服务类中,我使用@Transactional注释。 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
    }
}

If there is a exception happening in the addCompany_fail(), the first addCompany() one will also be rollbacked. 如果addCompany_fail()中发生异常,则第一个addCompany()也将被回滚。

I followed this document to understand idea how transaction controlled in Spring. 我按照这个文档来理解Spring中如何控制事务。 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html

I followed this document to understand how to code with JDBC in Spring. 我按照这个文档来了解如何在Spring中使用JDBC进行编码。 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html

I also read this (Free) http://www.infoq.com/news/2009/04/java-transaction-models-strategy . 我也读过这篇(免费) 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: Seem that many people misunderstand that using such Hibernate/Spring framework is only for avoid complexity of JDBC and Transaction Control. PS:似乎很多人误解使用这样的Hibernate / Spring框架只是为了避免JDBC和事务控制的复杂性。 Many people think like "JDBC and Transaction are so complex, just use Hibernate and forget about those two". 许多人认为“JDBC和Transaction是如此复杂,只需使用Hibernate而忘记这两者”。 Many examples on the internet about Spring+Hibernate or Spring+JDBC seemingly not care about transaction at all. 关于Spring + Hibernate或Spring + JDBC的互联网上的许多例子似乎根本不关心事务。 I feel that this is a bad joke. 我觉得这是一个糟糕的笑话。 Transaction is too serious for just letting something handle it without truly understanding. 交易太严重了,只是让某些事情处理它而没有真正理解。

Hibernate and Spring is so powerful and so complex. Hibernate和Spring是如此强大和复杂。 Then, as someone said, "Great power comes with responsibilities". 然后,正如有人所说,“强大的力量伴随着责任”。

UPDATE: 2013-08-17: There are good example about transaction here http://www.byteslounge.com/tutorials/spring-transaction-propagation-tutorial . 更新:2013-08-17:这里有关于交易的好例子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) 但是,这并不能解释如果你想使用REQUIRES_NEW,为什么你需要创建另一个类(否则你会得到这个问题Spring Transaction传播REQUIRED,REQUIRES_NEW ,看起来REQUIRES_NEW并没有真正创建一个新的事务)

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/ 更新: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/

Try defaultAutoCommit property. 尝试defaultAutoCommit属性。 Code would look like this: 代码看起来像这样:

<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}"
p:defaultAutoCommit="false" />

Look at javadoc: http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html#defaultAutoCommit 看看javadoc: http//commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html#defaultAutoCommit

You can't, simply run your code within a transaction, Spring will automatically disable auto-commit for you. 你不能,只需在一个事务中运行你的代码,Spring会自动禁用自动提交。 The easiest (at least to set-up) way to run a piece of code in a transaction in Spring is to use TransactionTemplate : 在Spring中的事务中运行一段代码的最简单(至少是设置)方式是使用TransactionTemplate

TransactionTemplate template = new TransactionTemplate(txManager);

template.execute(new TransactionCallback<Object>() {
  public Object doInTransaction(TransactionStatus transactionStatus) {
    //ALL YOUR CODE ARE BELONG TO... SINGLE TRANSACTION
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以多次更改JDBC连接的AutoCommit属性吗 - Can I change AutoCommit property of a JDBC connection multiple times 我如何关闭码头调试? - how can i turn off jetty debuging? 如何关闭额外的日志记录? - How can I turn off extra logging? 如何使用 Spring Cloud Stream 和 Kafka Streams Binder 暂停(打开/关闭)stream 处理? - How can I pause (turn on/off) stream processing w/ Spring Cloud Stream & Kafka Streams Binder? 如何关闭 Spring 引导中的“NioEndPoint”日志记录? - How do I turn off the "NioEndPoint" Logging in Spring Boot? 使用Oracle JDBC的大型结果集,是否可以关闭游标获取? (获取大小问题) - Large result sets with Oracle JDBC, can I turn cursor fetching off? (fetch size issue) Lucene 如何在 StandardAnalyzer 中关闭“toLowerCase”? - Lucene how can i turn off “toLowerCase” in StandardAnalyzer? 如何从 Java 关闭 hadoop 推测执行 - How can I turn off hadoop speculative execution from Java 如何以编程方式关闭 4G/数据? - How can i turn off 4G/Data programmatically? 如何在Swing中打开/关闭助记符的自动可视化? - How can I turn on/off automatic visualization of mnemonics in Swing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM