简体   繁体   中英

How to handle commit and rollback in @Transactional?

Can somebody tell me how to handle commit and rollback using Spring's @Transactional annotation? are they automatically handled by spring or are they required to be handled manually?

The whole point of having the @Transactional annotation is to have the container (in this case Spring) handle all the transaction related stuff automatically. What this means is that if all goes well and no exceptions are thrown the container will commit the transaction upon exit from the @Transactional code, or the transaction will be rolled back if an exception occurs.

In case you where wondering, @Transaction in Spring is implemented using AOP, because it is a crosscutting concern. You can read the documentation for all the details

@Transactional annotation will reduce the burden for the developer.As transaction code is a cross cutting concern spring itself provides us an annotation internally developed using aop concepts if we declare this annotation around a method. Spring will take care of transaction,if any exception raises it will issue a rollback on transaction manager otherwise commit on transaction manager

If you want to learn some more internals of spring transactons go through this doc spring transactions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM