Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring's JdbcTemplate and Transactions

Tags:

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(...); 
like image 334
loyalflow Avatar asked Sep 28 '12 14:09

loyalflow


People also ask

Can we use JdbcTemplate with a transactional?

It loops through the list of people and, for each person, inserts that person into the BOOKINGS table by using the JdbcTemplate . This method is tagged with @Transactional , meaning that any failure causes the entire operation to roll back to its previous state and to re-throw the original exception.

How does JdbcTemplate handle transactions in Spring?

Spring Transaction Management JDBC Example. We will create a simple Spring JDBC project where we will update multiple tables in a single transaction. The transaction should commit only when all the JDBC statements execute successfully otherwise it should rollback to avoid data inconsistency.

How does Spring data JPA manage transactions?

Spring Boot and Spring Data JPA make the handling of transactions extremely simple. They enable you to declare your preferred transaction handling and provide seamless integration with Hibernate and JPA. The only thing you need to do is to annotate one of your methods with @Transactional.


2 Answers

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.

like image 135
David Grant Avatar answered Sep 23 '22 12:09

David Grant


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.

like image 33
Nathan Hughes Avatar answered Sep 19 '22 12:09

Nathan Hughes