Annotation mode Configure transaction reference from http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html
<!--configuration transaction Manager-<bean id= TransactionManager class =< Span style= "color: #800000;" > " org.springframework.jdbc.datasource.datasourcetransactionmanager " Span style= "color: #800000;" > " > <property name=" datasource
ref = " datasource /> </bean> <!--annotations Configure things--<!--<tx:annotation-driv En transaction-manager= transactionmanager />---
Namespaces that need to be introduced into TX
xmlns:tx="http://www.springframework.org/schema/tx"
In the declaration of an interface or class, 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
If some methods do not require a transaction
@Transactional public class Testservicebean implements Testservice { private Testdao DAO; public void Setdao (Testdao dao) { DAO; } @Transactional (propagation = propagation.not_supported) public list<object> GetAll () { re Turn null ; } }
common parameters in @Transactional annotations description of 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=trueRollbackfor 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.classSpecifies multiple exception classes: @Transactional (rollbackfor={runtimeexception.class, Exception.class}) Parameter name function Description Rollbackforclassname This property is used to set the exception class name array that needs 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"specifies 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.classSpecifies 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"specifies multiple exception class names: @Transactional (Norollbackforclassname={"runtimeexception","Exception"}) Propagation This property is used to set the propagation behavior of a transaction, and the value can be referenced in table 6-7. Example: @Transactional (Propagation=propagation.not_supported,readonly=trueIsolation This property is used to set the transaction isolation level of the underlying database, which is used to handle multi-transaction concurrency, typically using the default isolation level of the database, which does not need to be set to timeout this property is used to set the time-out seconds for a transaction, and the default value is-1 means 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.
Spring Transaction configuration mode (i) annotation mode configuration