Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

java - Spring's JdbcTemplate and Transactions

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(...);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...