Spring's JdbcTemplate and Transactions

后端 未结 2 901
盖世英雄少女心
盖世英雄少女心 2020-12-14 03:01

When using JdbcTemplate, do I need to explicitly configure transactions?

My code layout looks like the following:

I will have a UserDao that will be injected

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 03:31

    Yes, JdbcTemplate is not a substitute for transaction management. You still benefit from database transactions, so userService.updateUser will operate in a database transaction, but if accountService.updateXXX fails, userService.updateUser will not rollback.

    If you don't want to use AOP, you can use TransactionTemplate instead. See programmatic transaction management in the Spring Reference Documentation.

    One pattern I've seen before is for the MVC controller class to invoke a business service, which encapsulates the operation. The method of the business class could then be annotated @Transactional.

提交回复
热议问题