The traditional transactions of Java EE applications have two strategies: global transactions and local transactions.
Global transactionsManaged by the application server, which must be supported by the JTA of the underlying server.
Local transactionIt is related to the persistence technology used at the underlying layer.JDBCIn the persistence technology, the connection object must be used to operate transactions.HibernateThe Session object must be used to operate transactions.
When a traditional transaction programming strategy is adopted, the program code must be coupled with the specific transaction operation code. The consequence is that when the application needs to switch between different transaction policies, the developer must manually modify the program code. This situation can be changed when the Spring transaction policy is used.
Spring transaction policyYesPlatformtransactionmanagerInterface implementation, which is the core of Spring transaction policy. The source code of this interface is as follows:
Public interface platformtransactionmanager {// platform-independent method for obtaining transactions transactionstatus gettransaction (transactiondefinition definition) throws transactionexception; // platform-independent transaction commit method void commit (transactionstatus status) throws transactionexception; // platform-independent transaction rollback method void rollback (transactionstatus status) throws transactionexception ;}
Platformtransactionmanager is an interface separated from any transaction policy. With the switching of different underlying transaction policies, applications must adopt different implementation classes. The platformtransactionmanager interface is not bundled with any transaction resources. It can adapt to any transaction policy. Combined with spring's IOC container, it can inject platform features to platformtransactionmanager.
The platformtransactionmanager interface has many different implementation classes. The application is oriented to platform-independent interface programming. When the underlying layer uses different persistence layer technologies, the system only needs to use different
Platformtransactionmanager implementation class. This type of switchover is usually managed by the spring container. applications do not need to be coupled with specific transaction APIs or with specific implementation classes, this completely isolates applications from Persistence Technologies and transaction APIs.
The transactionstatus object indicates a transaction. The transactionstatus object returned by gettransaction (transactiondefinition definition) may be a new transaction or an existing transaction object. Otherwise, the system will create a new transaction object and return it.
TransactiondefinitionThe interface defines a transaction rule, which must specify the following attributes:
Attribute |
Description |
Transaction isolation |
The degree of isolation between the current transaction and other transactions. For example, can this transaction see the uncommitted data of other transactions. |
Transaction Propagation |
Generally, the Code executed in the transaction runs in the current transaction. However, if a transaction context already exists, several options can be used to specify the execution behavior of the transaction method. For example, in most cases, you can simply run an existing transaction in the context of an existing transaction, or suspend an existing transaction to create a new transaction. Spring provides all the transaction propagation options in the EJB cmt. |
Transaction timeout |
The maximum duration of a transaction before it times out. If the transaction has not been committed or rolled back, the system will automatically roll back the transaction after this time. |
Read-Only status |
Read-only transactions do not modify any data. In some cases, for example, when Hibernate is used, read-only transactions are very useful for optimization. |
TransactionstatusIt represents the transaction itself. It provides a simple method to control transaction execution and query the transaction State. These methods are the same in all transaction APIs:
public interface TransactionStatus extends SavepointManager {boolean isNewTransaction();boolean hasSavepoint();void setRollbackOnly();boolean isRollbackOnly();boolean isCompleted();}
public interface SavepointManager {Object createSavepoint() throws TransactionException;void rollbackToSavepoint(Object savepoint) throws TransactionException;void releaseSavepoint(Object savepoint) throws TransactionException;}
The specific transaction management of spring is completed by the different implementation classes of platformtransactionmanager. When configuring platformtransactionmanager bean in spring containers, different implementation classes must be provided for different environments.
For exampleJDBC Data SourceThe configuration file of the local transaction policy is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" 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.5.xsdhttp://www.springframework.org/schema/contexth Ttp: // www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">! -- Define the data source bean and use the c3p0 data source --> <bean id = "datasource" class = "com. mchange. v2.c3p0. combopooleddatasource "> <property name =" driverclass "value =" oracle. JDBC. driver. oracledriver "/> <property name =" jdbcurl "value =" JDBC: oracle: thin :@ localhost: 1521: orcl "/> <property name =" user "value =" Scott "/> <property name =" password "value =" tiger "/> <property name =" maxpoolsize "Value = "40"/> <property name = "minpoolsize" valu E = "1"/> <property name = "initialpoolsize" value = "1"/> <property name = "maxidletime" value = "20"/> </bean> <! -- Configure the local Transaction Manager of the JDBC data source and use the datasourcetransactionmanager class --> <! -- This class implements the platformtransactionmanager interface, which is specific to the use of data source connection --> <bean id = "transactionmanager" class = "org. springframework. JDBC. datasource. datasourcetransactionmanager "> <property name =" datasource "ref =" datasource "/> </bean> </beans>
ForHibernateThe configuration file of the local transaction policy is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" 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.5.xsdhttp://www.springframework.org/schema/contexth Ttp: // www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">! -- Define the data source bean and use the c3p0 data source --> <bean id = "datasource" class = "com. mchange. v2.c3p0. combopooleddatasource "> <property name =" driverclass "value =" oracle. JDBC. driver. oracledriver "/> <property name =" jdbcurl "value =" JDBC: oracle: thin :@ localhost: 1521: orcl "/> <property name =" user "value =" Scott "/> <property name =" password "value =" tiger "/> <property name =" maxpoolsize "Value = "40"/> <property name = "minpoolsize" valu E = "1"/> <property name = "initialpoolsize" value = "1"/> <property name = "maxidletime" value = "20"/> </bean> <! -- Define sessionfactory of hibernate --> <bean id = "sessionfactory" class = "org. springframework. orm. hibernate3.localsessionfactorybean "> <property name =" datasource "ref =" datasource "/> <property name =" mappingresources "> <list> <value> Lee/xxx. HBM. XML </value> </List> </property> <property name = "hibernateproperties"> <props> <prop key = "hibernate. dialect "> Org. hibernate. dialect. mysqlinnodbdialect </prop> <prop key = "Hibernate. hbm2ddl. Auto"> Update </prop> </props> </property> </bean> <! -- Configure the local Transaction Manager of Hibernate and use the hibernatetransactionmanager class --> <! -- This class implements the platformtransactionmanager interface, which is for the specific implementation using hibernate --> <bean id = "transactionmanager" class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <property name =" sessionfactory "ref =" sessionfactory "/> </bean> </beans>
From the preceding configuration file, we can see that when Spring transaction management policy is adopted, the application does not need to be coupled with the specific transaction policy. Spring provides two transaction management methods:
① Programmed Transaction Management: Even when spring-based transaction is used, the program can directly obtain the transactionmanager bean in the container. This bean is always an instance of platformtransactionmanager, therefore, you can start, commit, and roll back transactions using the three methods provided by this interface.
② Declarative Transaction Management: You do not need to write any transaction operation code in a Java program. Instead, you configure the transaction proxy for the business component in the XML file. The enhanced processing that AOP binds to the transaction proxy is also provided by spring: before the target method is executed, the transaction starts. After the target method is executed, the transaction ends.
Regardless of the persistence policy, Spring provides consistent transaction abstraction. Therefore, application developers can use consistent programming models in any environment. The application can switch between different transaction management policies without changing the code.