Steps to use:
Step one, introducing the <tx:> namespace into the spring configuration file
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx= "Http://www.springframework.org/schema/tx"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
Step two, beans with @transactional annotations are automatically configured for declarative transaction support
<!--transaction manager configuration, hibernate single data source transaction-- <bean id= "Defaulttransactionmanager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean> <!--using annotation to define transactions-- <tx:annotation-driven Transaction-manager= "Defaulttransactionmanager" proxy-target-class= "true"/>
Step three, in the interface or class declaration, write a @transactional.
If only write on the interface, the implementation of the interface class will inherit, the interface implementation of the specific method of the class, you can override the settings at the class declaration
@Transactional//class-level annotations, methods that apply to all public in a class
Propagation behavior and isolation level of a transaction
When you use spring's annotated transaction management, the propagation behavior and isolation level of the transaction may be a bit overwhelming, as detailed below for easy access.
How to annotate things: @Transactional
When marked in front of a class, all methods in the marked class are handled by the object, for example:
When certain methods in a class do not require things :
@Transactionalpublic class Testservicebean implements Testservice { private Testdao dao; public void Setdao (Testdao dao) { This.dao = DAO; } @Transactional (propagation = propagation.not_supported) Public list<object> GetAll () { return null; } }
Introduction to the act of spreading things:
@Transactional (propagation=propagation.required)
If there is a transaction, then join the transaction and create a new one (by default)
@Transactional (propagation=propagation.not_supported)
Container does not open transactions for this method
@Transactional (propagation=propagation.requires_new)
creates a new transaction regardless of whether a transaction exists, the original hangs, the new execution completes , and the old transaction continues
@Transactional (Propagation=propagation.mandatory)
Must be executed in an existing transaction, or throw an exception
@Transactional (Propagation=propagation.never)
must be executed in a transaction that does not, or throws an exception (Contrary to propagation.mandatory)
@Transactional (Propagation=propagation.supports)
If the other bean calls this method and declares the transaction in another bean, the transaction is used. If the other bean does not declare the transaction, then there is no transaction.
Things timeout settings:
@Transactional (timeout=30)//default is 30 seconds
Transaction ISOLATION Level:
@Transactional (isolation = isolation.read_uncommitted)
READ UNCOMMITTED data (dirty read, non-repeatable read) basic not used
@Transactional (isolation = isolation.read_committed)
Read committed data (non-repeatable read and Phantom reads occur)
@Transactional (isolation = isolation.repeatable_read)
REPEATABLE READ (phantom read occurs)
@Transactional (isolation = isolation.serializable)
Serialization
MYSQL: Default to Repeatable_read level
SQL Server: Default is read_committed
Dirty reads : One transaction reads uncommitted update data to another transaction
non-repeatable read : In the same transaction, multiple reads of the same data are returned with different results, in other words,
Subsequent reads can be read to the updated data submitted by another transaction. Conversely, "repeatable reads" are repeated in the same transaction
When reading data, it is guaranteed to read the same data, that is, subsequent reads cannot be read to another transaction committed update data
Phantom reads : One transaction reads the insert data that has been committed by another transaction
Description of common parameters in @Transactional annotations
Parameter name |
Function description |
ReadOnly |
This property is used to set whether the current transaction is a read-only transaction, set to True for read-only, false to read-write, and the default value to False. Example: @Transactional (readonly=true) |
Rollbackfor |
This property is used to set an array of exception classes that need to be rolled back, and when the method throws an exception in the specified exception array, the transaction is rolled back. For example: Specify a single exception class: @Transactional (Rollbackfor=runtimeexception.class) Specify multiple exception classes: @Transactional (Rollbackfor={runtimeexception.class, Exception.class}) |
Continuation form)
Parameter name |
Function description |
Rollbackforclassname |
This property is used to set an array of exception class names that need to be rolled back, and when the method throws an exception in the specified exception name array, the transaction is rolled back. For example: Specify a single exception class name: @Transactional (rollbackforclassname= "RuntimeException") Specify multiple Exception class names: @Transactional (rollbackforclassname={"RuntimeException", "Exception"}) |
Norollbackfor |
This property is used to set an array of exception classes that do not need to be rolled back, and when a method throws an exception in the specified exception array, the transaction is not rolled back. For example: Specify a single exception class: @Transactional (Norollbackfor=runtimeexception.class) Specify multiple exception classes: @Transactional (Norollbackfor={runtimeexception.class, Exception.class}) |
Norollbackforclassname |
This property is used to set an array of exception class names that do not need to be rolled back, and when a method throws an exception in the specified exception name array, the transaction is not rolled back. For example: Specify a single exception class name: @Transactional (norollbackforclassname= "RuntimeException") Specify multiple exception class names: @Transactional (norollbackforclassname={"RuntimeException", "Exception"}) |
Propagation |
This property is used to set the propagation behavior of a transaction, which can be referenced in table 6-7. Example: @Transactional (propagation=propagation.not_supported,readonly=true) |
Isolation |
This property is used to set the transaction isolation level of the underlying database, which is used to handle multi-transaction concurrency, usually using the default isolation level of the database, and does not need to be set |
Timeout |
This property is used to set the time-out seconds for a transaction, and the default value is 1 to never time out |
Note the points:
1 @Transactional can only be applied to the public method, for other non-public methods, if the tag @transactional will not error, but the method does not have transactional capabilities.
2 with the Spring transaction manager, Spring is responsible for opening, committing, and rolling back the database. Default run-time exceptions are encountered (the throw new RuntimeException ("comment");) rolled back, that is, the rollback occurs when an exception is encountered that is not checked (unchecked) , while encountering the exception that needs to be caught (the throw new Exception ("comment");) does not roll back, that is, when a check exception is encountered (that is, an exception that is thrown when it is not run-time, the compiler checks for an exception that is called a check exception or a check exception), we specify the way to let the transaction roll To get all the exceptions rolled back, add @Transactional (Rollbackfor={exception.class, other exceptions}). If unchecked exception is not rolled back: @Transactional ( Notrollbackfor=runtimeexception.class)
As follows:
@Transactional (Rollbackfor=exception.class)//Specify Rollback, rollback when an exception Exception is encountered
public void MethodName () {
throw new Exception ("note");
}
@Transactional (Norollbackfor=exception.class)//Specifies no rollback and encounters a run-time exception (the throw new RuntimeException ("note");) is rolled back
Public Itimdaoimpl Getitemdaoimpl () {
throw new RuntimeException ("note");
}
3. @Transactional annotations should only be applied to the public visibility method. If you use @Transactional annotations on protected, private, or package-visible methods, it will not error, but this annotated method will not show the configured transaction settings.
4. @Transactional annotations can be applied to interface definitions and interface methods, class definitions, and public methods of classes. Note, however, that only @Transactional annotations appear to be less than the open transaction behavior, which is only a meta-data that can be used to identify @Transactional annotations and the beans that are configured appropriately to have transactional behavior. In the above example, it is actually the presence of the <tx:annotation-driven/> element that opens the transaction behavior.
5. The spring team's recommendation is that you use @Transactional annotations on specific classes (or methods of classes) rather than on any interface that the class implements. You can certainly use @Transactional annotations on the interface, but this will only take effect if you set up an interface-based proxy. Because annotations are not inherited, this means that if you are using a class-based proxy, the transaction's settings will not be recognized by the class-based proxy, and the object will not be wrapped by the transaction proxy (it will be identified as critical). Therefore, accept the suggestions from the spring team and use @Transactional annotations on specific classes.
Specific issues: The isolation of Spring transactions, and the distinction between each isolation