看来我的配置错过了这一行:
<tx:annotation-driven transaction-manager="txManager"/>
然后,在我的服务类中,我使用@Transactional批注。例如
@Serviceclass 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()也将被回滚。
我阅读了这份文档,以了解在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
我还阅读了这个(免费)http://www.infoq.com/news/2009/04/java-transaction-models-
strategy。真的很好。我对作者的看法是,大多数人都不了解(或关心)交易。
PS:似乎很多人误解了使用这种Hibernate / Spring框架只是为了避免JDBC和事务控制的复杂性。许多人认为,“
JDBC和事务是如此复杂,只需使用Hibernate而不用考虑这两者”。互联网上有关Spring + Hibernate或Spring +
JDBC的许多示例似乎根本不在乎事务。我觉得这是个坏笑话。交易太严重了,以至于在没有真正理解的情况下让某些事情来处理。
Hibernate和Spring是如此强大且如此复杂。然后,就像有人说的那样,“强大的力量伴随着责任”。
**更新:2013-08-17:** http **://www.byteslounge.com/tutorials/spring-
transaction-propagation-tutorial上**
有一个很好的交易示例。但是,这并不能说明如果您想使用REQUIRES_NEW,为什么需要创建另一个类(否则,您将遇到此问题:Spring事务传播为REQUIRED,REQUIRES_NEW,看来REQUIRES_NEW并没有真正创建新的事务)
更新日期:2018年1月1日:
我已创建了春天引导1.5.8.RELEASE一个完整的例子这里https://www.surasint.com/spring-boot-database-
transaction-jdbi/ 这里一些基本的实验例子HTTPS ://www.surasint.com/spring-boot-connection-
transaction/



