Spring @Transactional--Transaction rollback

Source: Internet
Author: User
Tags throw exception

Working principle
When you run a test class that configures @transactional annotations, the following steps occur
1) at the beginning of the transaction, through the AOP mechanism, a proxy connection object is generated and placed into a datasourcetransactionmanager-related container in the DataSource instance. In the next whole transaction, the client code should use the connection to connect to the database, execute all database commands [do not use the database command that the connection connection database executes, and not be rolled back when this transaction is rolled back]
2) When the transaction ends, roll back the database command executed on the agent connection object in the 1th step, and then close the agent Connection object


As described above, the customer code we use should have the following capabilities:
1) Every time you execute a database command
If in the context of a transaction, instead of creating a new Connection object directly, try to get the connection object from a Datasourcetransactionmanager-related container in the DataSource instance In a non-transactional context, create a new Connection object directly
2) Every time the database command is executed
If the connection object is not closed directly in the context of the transaction, because the connection object needs to be used throughout the transaction, but only the hold of the Connection object is released from this database command; In a non-transactional context, Close the Connection object directly

With @transactional in front of the service class, declare that all methods of the service require transaction management. Each business method starts with a transaction open.

Spring defaults to a transaction rollback of the run-time exception (RuntimeException). The exception is unchecked.

If you encounter a checked accident, do not roll back.

How to change the default rule:

1 Let checked exception also rollback: Add @Transactional before the whole method (Rollbackfor=exception.class)

2 Let unchecked exception not rollback: @Transactional (Notrollbackfor=runtimeexception.class)

3 (Query-only) methods that do not require transaction management: @Transactional (propagation=propagation.not_supported)

4 If you do not add attributes such as Rollbackfor, Spring encounters unchecked exceptions will rollback, not only runtimeexception, but also error.

Note: If the exception is try{}catch{}, the transaction is not rolled back, and if you want the transaction to roll back, you must throw the Try{}catch{throw Exception} out.

Spring @Transactional--Transaction rollback

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.