Spring Declarative Transaction Management
Spring's declarative transaction management is implemented through spring AOP, and Spring provides a declarative rollback rule: We can specify what exceptions will result in a rollback. XML version Configuration
<!--AOP Transaction management start ... --> <!--Hibernatetransactionmanager class has setsessionfactory (), which injects sessionfactory into--> <bean id= "TxManager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name= "sessionfactory" ref = "Sessionfactory"/> </bean> <!--encapsulates the semantics we want to impose in a transaction in <tx:advice/>, where the default setting is: Transactional propagation settings are required ; The isolation level is default; transactions are read/write; The transaction timeout is dependent on the transaction system by default, or the transaction timeout is not supported; any runtimeexception will trigger a transaction rollback, but any checked Exception will not trigger a transaction rollback. --> <tx:advice id= "Txadvice" transaction-manager= "Txmanager" > <tx:attributes> <t
X:method name= "save*" propagation= "REQUIRED"/> <tx:method name= "add*" propagation= "REQUIRED"/> <tx:method name= "delete*" propagation= "REQUIRED"/> <tx:method name= "test*" propagation= "REQUIRE" D "/> </tx:attributes> </tx:advice> <!--set up a pointcut to ensure that the transaction notifications defined by the" txadvice "Bean are executed at the appropriate point in the application. Then use a notification (advisor) to bind the slice to the TxadviceTogether, where the expression attribute is the weaving point syntax, Com.niu.service packets under any package (..) Arbitrary method (*) parameter arbitrary (..) of any implementation class (*) --> <aop:config> <aop:pointcut expression= "Execution (public * Com.niu.service ... *.*(..))" Id= "Transactionaop"/> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "Transactionaop"/> fig> <!--AOP Transaction management end ... -->
Some of the methods configured for the service layer include transaction management, and when they are invoked, a transaction is started, hangs, marked as read-only, or otherwise (depending on the semantics you define).
Rollback rule:
<!--Specify the exception rollback type-->
<tx:method name= "save*" propagation= "REQUIRED" rollback-for= "
Noproductinstockexception/>
<!--do not roll back the transaction--> <TX even if you encounter a Instrumentnotfoundexception exception that is not handled
: Method Name= "save*" propagation= "REQUIRED" no-rollback-for= "instrumentnotfoundexception"
/>
<tx:method> settings
Property |
Whether you need |
Default Value |
Description |
Name |
Is |
|
The name of the method associated with the transaction property. |
Propagation |
No |
REQUIRED |
Transactional propagation behavior |
Isolation |
No |
DEFAULT |
Transaction ISOLATION LEVEL |
Timeout |
No |
-1 |
Transaction timeout, in seconds |
ReadOnly |
No |
False |
Whether the transaction is read-only |
Rollback-for |
No |
|
Exception that will be triggered to roll back, separated by commas |
No-rollback-for |
No |
|
Exception that are not triggered for rollback, separated by commas |
Annotation Version Configuration
<beans xmlns:tx= "Http://www.springframework.org/schema/tx"
xsi:schemalocation= "
http:// Www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
< Bean id= "Txmanager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property Name= "Sessionfactory" ref= "sessionfactory"/>
</bean>
<!--start the annotation version of the transaction management switch-->
<TX: Annotation-driven transaction-manager= "Txmanager"/>
<beans/>
The configuration work in XML is done and the @transactional tag is added to the interface definition, interface method, class definition, class method (public) that needs to be opened for transaction management. Note: @Transactional is just a metadata, simply because the Tx:annotation-driven tag opens the transaction. It is recommended that @transactional be used on specific classes (methods).
Tx:annotation-driven Configuration
Property |
Default Value |
Description |
Transaction-manager |
TransactionManager |
The name of the transaction manager to use. |
Mode |
Proxy |
The default mode "proxy" uses the spring AOP framework to broker annotated beans, and another viable pattern "ASPECTJ" uses spring's ASPECTJ transaction slice to weave classes |
Proxy-target-class |
False |
Valid only for agent mode. Determines what transaction agents are created for classes that use the @transactional annotation. If the "Proxy-target-class" property is set to True, the class based proxy is created. If the "Proxy-target-class" property is set to "false" or not set, the interface-based standard JDK Proxy is created. |
Order |
Ordered.lowest_precedence |
The order in which the transaction notifications are defined is applied to the bean using the @transactional annotation |
Properties of @Transactional annotations
Property |
type |
Description |
propagation |
enum type: propagation |
optional propagation setting |
isolation |
enum: Isolation |
Optional Isolation Level (default: Isolation_default) |
readOnly |
boolean |
read-write Transactions vs. readonly transactions |
timeout |
int (seconds) |
transaction timeout |
rollbackfor |
an instance of a set of class classes that must be a throwable subclass |
a set of exception classes that must be rolled back when they are encountered. By default, checked exceptions is not rolled back and only unchecked exceptions (that is, the RuntimeException subclass) to perform a transaction rollback |
Rollbackforclas Sname |
The name of a group of class classes, must be a throwable subclass |
A set of exception class names, must be rolled back |
norollbackfor |
the name of a group of class classes, which must be subclasses of Throwable |
a set of exception class names that must not be rolled back when they are encountered |
norollbackforclassname |
The name of a group of class classes that must be a throwable subclass |
A set of exception class names that must not be rolled back |