Objective:
The database transaction has been mentioned in the previous blog, but it is more about the locking mechanism of the transaction, and today it is some basic knowledge of spring management transaction.
Previous article: [Database transactions and locks] detail one: Thorough understanding of database transactions
One, what is a transaction
A transaction is a logical set of actions that either succeed or fail.
Properties of the transaction: ACID
Atomicity (atomicity): A transaction is executed as a whole, and the manipulation of the data contained therein is either completely executed or not executed.
Consistency (consistency): a transaction should ensure that the state of the database transitions from one consistent state to another consistent state. The meaning of a consistent state is that the data in the database should be satisfied as an integrity constraint.
Isolation (Isolation): When multiple transactions are executing concurrently, the execution of one transaction should not affect the execution of other transactions.
Persistence (Durability): Once a transaction is committed, his modifications to the database should be persisted in the database.
Two, spring's transaction management
Category: Programmatic transaction management. Manually write code-management transactions.
Type two: declarative transaction management. Transaction management is done through configuration. (AOP): Only declarative transaction management is mentioned here.
API for Transaction Management:
Platformtransactionmanager : Platform transaction manager
* is the object of real management transactions.
transactiondefinition : Transaction definition information.
* Define transactions (Isolation level, propagation behavior, timeout information, read-only)
transactionstatus : Transaction state.
* In the process of managing transactions, transactions have some state changes. The status information is recorded in the object.
Spring uses Platformtransactionmanager to manage transactions based on information defined in Transactiondefinition, generating state during management transactions, and logging state to Transactionstatus.
Three, spring-related API detailed
1,Platformtransactionmanager
2,transactiondefinition
Constant:
* isolation_* : Defines the isolation level of the transaction.
* propagation_* : Defines the propagation behavior of a transaction.
* Timeout Information:
Method:
Transaction transaction Status:
|
Spring's transactional propagation behavior:
Propagation_required: If a, b operates, if a has a transaction that uses a transaction, the B is included in. If a does not have a transaction create a new transaction will be a A, a and a. Propagation_supports: If a , B operates, if A has a transaction , a transaction using a is included in the B. If a does not have a transaction, the transaction is not used. Propagation_mandatory : If a, b operates, if a has a transaction, a transaction using a is included in the B. If a does not have a transaction, an exception is thrown. Propagation_requires_new : If a, b operates, if a has a transaction, the transaction of a is suspended. Create a new transaction to perform a B operation. propagation_not_supported : If a, B operates, Always runs in a non-transactional manner, if a has one, hang a. Propagation_never: If a, b operations, always run in a non-transactional manner, if a has a transaction throws an exception. propagation_nested : Nested transactions. If a, b operation, A has a transaction , set a savepoint after a executes. If B executes without problems, commit together. If a problem occurs with B. Allows the user to control the rollback to the original or rollback to the savepoint.
Four, spring transaction management instance (declarative transaction management)
Spring declarative transaction management can be divided into three types:
1, based on Transactionproxyfactorybean
2, transaction management based on AspectJ XML method
3, annotation mode transaction management based on ASPECTJ
Transaction management based on the ASPECTJ XML approach:
1, the introduction of the development package:
2,service Layer Code:
Accountservice.java
1 public interface Accountservice {2 3 /**4 * Transfer method 5 */6 public void Transfer (String from,string to, Double money); 7}
Accountserviceimpl.java:
1 private Accountdao Accountdao; 2 3 public void Setaccountdao (Accountdao accountdao) {4 This.accountdao = Accountdao; 5 } 6 7 @Override 8 /** 9 * Business Layer Transfer method: * from : Transfer account * to : Transfer account 12 * Money: Transfer amount */14 public void Transfer (final string from, final string to, final Double) {15 Accountdao.outmoney (from, money); + int d = 1/0;17 Accountdao.inmoney (to, money); }19 20}
3,dao Layer Code
Accountdao.java:
1 public interface Accountdao {2 publicly void Outmoney (string from, Double money); 3 4 public void Inmoney (String to , Double money); 5}
Accountdaoimpl.java:
1 public class Accountdaoimpl extends Jdbcdaosupport implements Accountdao {2 3 @Override 4 public void Outmo Ney (String from, Double money) {5 this.getjdbctemplate (). Update ("Update account Set money = Money-?") WHERE name =? ", Money,from); 6 } 7 8 @Override 9 public void Inmoney (String to, Double money) { this.getjdbctemplate (). Update ("Update account Set money = Money +?") WHERE name =? ", money,to); }12 13}
4, configuration file
Jdbc.properties:
Jdbc.driverclass=com.mysql.jdbc.driverjdbc.url=jdbc:mysql:///spring_transactionjdbc.user=rootjdbc.password=123
Applicationcontext.xml:
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <beans xmlns= "Http://www.springframework.org/schema/beans" 3 xmln S:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context= "Http://www.springframework.org/schema/context "5 xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "6 xmlns:tx=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX "7 xsi:schemalocation= "Http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/sp Ring-beans.xsd 9 http://www.springframework.org/schema/context10 Http://www.springframework.org/schema/context/sp Ring-context.xsd11 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP12 http://www.springframework.org/schema/aop/ Spring-aop.xsd13 Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.x SD ">15 <context:property-placeholder location=" classpath:jdbc.properties "/>17 <!--configuration c3p 0 Connection Pool-->19 <bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >20 <property name= "Driverclass" value= "${jdbc.driverclass } "/>21 <property name=" Jdbcurl "value=" ${jdbc.url} "/>22 <property name=" user "value=" ${jdbc.us ER} "/>23 <property name=" password "value=" ${jdbc.password} "/>24 </bean>25 <!--Configure Service -->26 <bean id= "Accountservice" class= "Cn.augmentum.transaction.demo3.service.AccountServiceImpl" >27 <property name= "Accountdao" ref= "Accountdao"/>28 </bean>29 <!--configuration DAO-->31 <bean Id= "Accountdao" class= "Cn.augmentum.transaction.demo3.dao.AccountDaoImpl" >32 <property name= "DataSource" re f= "DataSource"/>33 </bean>34 <!--configuration transaction manager-->36 <bean id= "TransactionManager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager ">37 <property name=" datasource "ref=" DataS Ource "/>38 </bean>39 <!--configuring transaction enhancement-->41 <tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >42 <tx:attributes>43 <!--propagation: Transaction propagation Behavior: Isolation: Isolation Level read-only: Whether it is a read-only transaction. A timeout: whether to time out. Rollback-for: Similar to-exception. occurred Which exceptions rollback the transaction. No-rollback-for: Similar to +exception. What exceptions do not roll back.-->51 <tx:method Name = "Transfer" propagation= "REQUIRED"/>52 </tx:attributes>53 </tx:advice>54 < !--AOP configuration-->57 <aop:config>58 <aop:pointcut expression= "Execution (* cn.augmentum.transaction.dem o3.service.accountserviceimpl.* (..)) " Id= "POINTCUT1"/>59 <aop:advisor advice-ref= "Txadvice" pointcut-ref= ""/>60 </aop:config>61 </ Beans>
5, test method
Springdemo.java:
1 @RunWith (Springjunit4classrunner.class) 2 @ContextConfiguration ("Classpath:applicationContext.xml") 3 public class SPRINGDEMO3 {4 5 @Resource (name= "Accountservice") 6 private Accountservice accountservice; 7 8 @Test 9 public void Demo1 () { accountservice.transfer ("Lao Ma", "Feng elder sister", 1000d); }12}
Declarative transaction management on XML that's all, so here's a simpler approach to declarative transactional management of annotations.;
Just see how the service layer is annotated and how it is configured in ApplicationContext.
Accountserviceimpl.java:
1/** 2 * properties in @Transactional 3 * Propagation: Propagation behavior. 4 * ReadOnly: Read-only 5 * Timeout: Timeout information 6 * Isolation: Isolated Level 7 */8 @Transactional (propagation=propagation.required,readonly=false,timeout=-1,isolation= Isolation.default) 9 public class Accountserviceimpl implements Accountservice {Ten private Accountdao accountdao;11 Public void Setaccountdao (Accountdao accountdao) { This.accountdao = accountdao;14 }15 Override17 /**18 * Business Layer Transfer method: * from : Transfer Account number * To: transfer Account * Money: Transfer amount 22 */23 public void Transfer (final string from, final string to, final Double money) { Accountdao.outmoney (from, money); + int d = 1/0;26 Accountdao.inmoney (to, money); }28}
Applicationcontext.xml:
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <beans xmlns= "Http://www.springframework.org/schema/beans" 3 xmln S:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context= "Http://www.springframework.org/schema/context "5 xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "6 xmlns:tx=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX "7 xsi:schemalocation= "Http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/sp Ring-beans.xsd 9 http://www.springframework.org/schema/context10 Http://www.springframework.org/schema/context/sp Ring-context.xsd11 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP12 http://www.springframework.org/schema/aop/ Spring-aop.xsd13 Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.x SD ">15 <context:property-placeholder location=" classpath:jdbc.properties "/>17 <!--configuration c3p 0 Connection Pool-->19 <bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >20 <property name= "Driverclass" value= "${jdbc.driverclass } "/>21 <property name=" Jdbcurl "value=" ${jdbc.url} "/>22 <property name=" user "value=" ${jdbc.us ER} "/>23 <property name=" password "value=" ${jdbc.password} "/>24 </bean>25 <!--Configure Service -->26 <bean id= "Accountservice" class= "Cn.augmentum.transaction.demo4.service.AccountServiceImpl" >27 <property name= "Accountdao" ref= "Accountdao"/>28 </bean>29 <!--configuration DAO-->31 <bean Id= "Accountdao" class= "Cn.augmentum.transaction.demo4.dao.AccountDaoImpl" >32 <property name= "DataSource" re f= "DataSource"/>33 </bean>34 <!--configuration transaction manager-->36 <bean id= "TransactionManager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager ">37 <property name=" datasource "ref=" DataS Ource "/>38 </bean>39 <!--opening annotation transactions-->41 <tx:annotation-driven transaction-manager= "TransactionManager"/>42 & Lt;/beans>
Declarative transaction management for annotations is just as much, and it does seem a lot easier.
[Spring Frame] Spring Transaction Management Basics Primer Summary.