Through the @Transactional annotation, the transaction can be woven into the corresponding public method to realize transaction management.
All optional attributes of @Transactional are as follows:
-
propagation: Used to set transaction propagation properties. The attribute type is Propagation enumeration, and the default value is Propagation.REQUIRED.
-
isolation: used to set the isolation level of the transaction. The property type is Isolation enumeration, and the default value is Isolation.DEFAULT.
-
readOnly: It is used to set whether the operation of the method on the database is read-only. This property is boolean, and the default value is false.
-
timeout: Used to set the timeout period for this operation to connect to the database. The unit is second, the type is int, and the default value is -1, that is, there is no time limit.
-
rollbackFor: Specifies the exception class that needs to be rolled back. The type is Class[], and the default value is an empty array. Of course, if there is only one exception class, you don't need to use an array.
-
rollbackForClassName: Specifies the name of the exception class that needs to be rolled back. The type is String[], and the default value is an empty array. Of course, if there is only one exception class, you don't need to use an array.
-
noRollbackFor: Specifies exception classes that do not require rollback. The type is Class[], and the default value is an empty array. Of course, if there is only one exception class, you don't need to use an array.
-
noRollbackForClassName: Specifies the name of the exception class that does not need to be rolled back. The type is String[], and the default value is an empty array. Of course, if there is only one exception class, you don't need to use an array.
It should be noted that if @Transactional is used on a method, it can only be used on a public method. For other non-public methods, if the annotation @Transactional is added, although Spring will not report an error, it will not weave the specified transaction into the method. Because Spring ignores @Transaction annotations on all non-public methods.
If the @Transaction annotation is on the class, it means that all methods on the class will be woven into the transaction when executed.
Transaction steps to implement annotations:
Duplicate trans_sale item, new item trans_sale_annotation
1. Declare the transaction manager
2. Enable annotation-driven transaction-manager
the id of the transaction manager bean
3. The public method of the business layer adds transaction attributes
Run the program:
Check out the database: