30

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 into my UserService, and then my Controllers will make calls on methods in my UserService.

I want to keep things as simple as possible transaction wise, and I don't need multiple database calls to span a transaction.

By default, do I have to do anything in my configuration file or use a @Transaction annotation anywhere?

Now say in my controller I need to make 2 calls on my userService and accountService, could I explicitly wrap it in a transaction somehow?

userService.updateUser(user);
accountService.updateXXX(...);
David Grant
  • 13,929
  • 3
  • 57
  • 63
loyalflow
  • 14,275
  • 27
  • 107
  • 168

4 Answers4

38

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.

David Grant
  • 13,929
  • 3
  • 57
  • 63
  • 3
    wow, that's allot of code for a transaction, @Transactional sure makes it less boilerplate to write! – loyalflow Sep 28 '12 at 15:07
  • It is, but it's quite useful if you need to access the `TransactionStatus`. – David Grant Sep 28 '12 at 15:09
  • 2
    +1 for the business service idea. IMHO, life would be easier if everyone did this. – xdhmoore Jul 23 '14 at 18:37
  • I just want to know how PlatformTransactionManager make a relationship with jdbcTemplate, because they have a Datasource – deFreitas Sep 12 '17 at 18:09
  • 2
    How to use `AOP` with `jdbcTemplate`? I seen some examples with require a `JpaTransactionManager` depdendency, but is JPA correct for `jdbcTemplate`? – User Oct 06 '19 at 13:12
17

If your controller wants to do several things with users and accounts and have all that happen within one transaction, then you should have a service with one method that does all that stuff. Creating one service per DAO is not a great idea, because you end up with do-nothing wrappers around DAOs and processing will be slow because the database will have to create a separate transaction for each call to a DAO, you're making it do a lot more work than it should have to.

The service should provide functionality to the controller or whoever else is calling it. I try to create services with the idea that the service provides specific functions useful to a certain type of user.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • 1
    +1 seconded. This makes several things easier, including testing, and makes it easy if you need to expose any functionality to another view other than a controller (a REST API, for example). – xdhmoore Jul 23 '14 at 18:39
1

Spring JdbcTemplate is not aware about the transactions at all. If you have not configured transactions in any way - programmatically, usually via PlatformTransactionManager or TransactionTemplate (which is just a more high level api for PlatformTransactionManager), or using declarative approach, via annotations, like spring @Transactional - then every SQL query issued by JdbcTemplate will be run outside any transaction. So yes, transactions configuration is required.

Mikhail2048
  • 1,715
  • 1
  • 9
  • 26
0

Yes, wrap it using the TransactionTemplate

transactionTemplate.executeWithoutResult((TransactionStatus ts) -> {
    userService.updateUser(user);
    accountService.updateXXX(...);
});

https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#tx-prog-template

stanley
  • 119
  • 1
  • 3