Springframework (8)

zhaozj2021-02-16  124

5, transaction management

(1) transaction

l global affairs

Ø Use JTA management by the application server

Ø Ability to work with multi-transaction resources

l local affairs

Ø Resource Specification: If the transaction associated with JDBC

Ø Unable to cross the work between multi-transaction resources

Ø Cannot run in the global JTA transaction

l Different programming models

(2) Spring solution

l Use the same programming model for global and local affairs

Ø Different environments use different transaction management strategies

l Transaction management:

Ø Programming method

Ø Declaration mode (such as EJB CMT)

(3) Transaction extraction

l Transactions Extract through the PlatformTransactionManager interface

Ø GetTransaction (TransactionsDefinition)

Ø Commit (TransactionsTatus)

Ø Rollback (TransactionsTatus)

l TransactionDefinition: Isolation, propagation, timeout, and read-only state

l TransactionsTatus

Ø isnewtransaction ()

Ø setRollbackOnly ()

Ø IsrollbackOnly ()

(4) Transaction management

l Built-in platform transaction management:

Ø JTATRRANSCRIONMANAGER

Ø DataSourceTransacrionManager

Ø HibernateTransacrionManager

Ø JDOTRANSCRIONMANAGER

(5) example

l Define a JTATRANSACRIONMANAGER

myds

Class = "... Transaction.jta.jtatransactionManager" />

Data sources must be configured as transaction resources in the application server

l Define a DataSourceTransacrionManager

Class = "Org.apache.commons.dbcp.basicdataasource">

...

Class = "... jdbc.datasource.datasourcetransactionManager">

L Defines a HibernateTransacrionManager

Class = "... ORM.HIBERNATE.LOCALESSIONFAAANEBEAN">

...

Class = "... Orm.Hibernate.HibernateTransactionManager">

Use JTA to apply Hibernate without HibernateTransacrionManager, just configure JTATRRANSCRIONMANAGER, give the sessionFactory data source obtained from JNDI

(6) TransactionTemplate

l Programming method transaction management

l Create a TransactionTemplate

PlatformTransactionManager TransactionManager =

(PlatformTransactionManager) CTX.getBean ("MyTransactionManager);

TransactionTemplate Transaction =

New TransactionTemplate (TransactionManager);

l Execute in a transaction

Transaction.execute (New TransactionCallbackwithoutResult () {

Public void DointransactionwithoutResult (TransactionsTatus s) {

UpdateOperation1 ();

UpdateOperation2 ();

}

});

l TransactionTemplate other methods

Ø setPropagationBehavior (int)

Ø setisolationledLel (int)

Ø setReadonly (Boolean)

Ø settimeout (int)

(7) Benefits

l Conversion between different transaction management

Ø just some configuration

Ø Do not change the code

l The same components can be run:

Ø Application server using JTA transaction

Ø Independent application or Web container: use JDBC or open source JTA (such as JOTM)

(8) Declaration of affairs

l Don't need TransactionTemplate

l Implement using Spring AOP

l Similar to EJB CMT: Specifies a transaction behavior to an independent method

L and EJB CMT:

Ø Apply to any Pojo

Ø No need to bind to JTA (you can work with JDBC, JDO, and Hibernate)

Ø has a declarative ROLLBACK rule

Ø Custom transaction behavior

Ø No need to support transaction context via remote call

(9) TransactionAttributeSourceLeL defines how transaction attributes apply

l TransactionAttributeEditor reads the definition of the following form:

Ø Propagation_name, isolation_name, readonly, exception1, -exception2

Ø The front of the abnormal name means submit (commit) even if it is abnormal; the exception name - indicates that the rollback is required (ROLLBACK)

l example:

Ø Propagation_mandatory, isolation_default, -createexception, -duplicateKeyException

(10) Statement affairs

l Defining a transaction interceptor

Class = "... matchalwaystransactionattributesource">

propagation_required

Class = "... transaction.interceptor.transactioninterceptor">

MatchalwayStransactionAttributeSource apps the same properties to all methods

l-variable transactiontnessattributesource

Class = "... interceptor.namematchtransactionattributesource">

Get * = propagation_required, readonly

Find * = propagation_required, readonly

Load * = propagation_required, readonly

Store * = Propagation_Required

NameMatchTransactionAttributeSource Apply specific attributes to matching methods

l Auto Agent Business Beans

Class = "... framework.autoproxy.beannameautoproxycreator">

txinterceptor

* Dao

l Use metadata properties

Class = "... aop.framework.autoproxy.defaultadvisorautoproxycreator">

Class = "... transaction.interceptource.transactionattributesourceadvisor"

Autowire = "constructor">

Class = "... transaction.interceptor.transactioninterceptor"

AutoWire = "ByType">

Class = "... transaction.interceptor.attributestransactionattributesource"

Autowire = "constructor">

Class = "... Metadata.commons.commonsattributes">

转载请注明原文地址:https://www.9cbs.com/read-8828.html

New Post(0)