It seems that my configuration missed this line:
<tx:annotation-driven transaction-manager="txManager"/>
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
}
}
If there is a exception happening in the addCompany_fail(), the first addCompany() one will also be rollbacked.
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
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
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:
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 n
answered 2012-03-14T22:08:17.997