@siros_s. agenda: spring transaction why spring transaction transaction manager declarative...

Post on 26-Mar-2015

283 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

ศิ�รส ส�ภาวิ�ตา@siros_s

Agenda: Spring TransactionWhy Spring TransactionTransaction ManagerDeclarative Transaction with Annotation

@TransactionalPropagationException Handling

Why Spring Transaction?Uniform APITransactional resource synchronizationOn and off serverDeclarative Transaction

What about JTA?Require J(2)EE container (server)Require EJB for declarative transaction

Necessary for distributed transaction

Uniform API

JDBC

Spring Transaction

ORM JCA

Application

JTA

Transactional Resource SyncC C

C

Spring Transaction

On and Off Server

Spring

Java

J(2)EEContainer

ServletContainer

Spring Spring

Declarative Transaction

A CB

D

D CB

Commit

Begin

Begin

Commit

Declarative Transaction (cont.)Transaction Demarcation

Transaction logic is NOT the main business logic

Reuse business logic with different transaction logic

Programmatic transaction demarcation is NOT the answer

Transaction Manager Transactional Resource

Transaction Manager

Transaction AnnotationUse annotation to declare transaction scopeRequire just one line of config in XML

Example: MyService

Example: Transactional MyService

Example: Detailed MyService

PropagationDefine how transaction manager handles

transaction

SUPPORTSNOT_SUPPORTEDREQUIREDREQUIRES_NEWMANDATORYNEVERNESTED

Example: Propagation

A CB

D

NEVER

REQUIREDREQUIRES_NEW

REQUIREDSUPPORTSMANDAROTY

REQUIRES_NEW

Exception HandlingDefault behavior

Rollback for unchecked exceptionNo rollback for checked exception

Can be changed with @Transactional propertiesrollbackForrollbackForClassnamenoRollbackFornoRollbackForClassname

Example: Exception Handling

Wait!! There is a better way!!

Q & A

Agenda: Spring AOPProblems of existing approachAOP DefinitionAOP ProxySpring AOPSpring Transaction with AOP

Problems??Transaction policy is still defined locally on

each class rather than globally across all classes.

Modularity & ReusabilityScattering & Tangling

Even a medium sized application has multiple “concerns”.

Example of Transaction PolicyAll top level service operations must begin a

new transaction (REQUIRES_NEW).Lower level service operations must operate

under an existing transaction (MANDATORY).Data access operation should operate under a

transaction (REQUIRES).

The problem is that policies or concerns are usually applied based on neither class nor class hierarchy. They are mostly random.

Scatteringobj.lastModified = new Date();obj.lastModified = new Date();

Customer cust = new Customer();…

cust.lastModified = new Date();saveCustomer(cust);

Customer cust = new Customer();…

cust.lastModified = new Date();saveCustomer(cust);

Invoice inv = new Invoice();…

inv.lastModified = new Date();saveInvoice(inv);

Invoice inv = new Invoice();…

inv.lastModified = new Date();saveInvoice(inv);

Course crs = new Course();…

crs.lastModified = new Date();saveCourse(crs);

Course crs = new Course();…

crs.lastModified = new Date();saveCourse(crs);

Scattering: Modifiedobj.lastModifiedBy = currentUser();obj.lastModified = new Date();obj.lastModifiedBy = currentUser();obj.lastModified = new Date();

Customer cust = new Customer();…

cust.lastModifiedBy = currentUser();cust.lastModified = new Date();saveCustomer(cust);

Customer cust = new Customer();…

cust.lastModifiedBy = currentUser();cust.lastModified = new Date();saveCustomer(cust);

Invoice inv = new Invoice();…

inv.lastModifiedBy = currentUser();inv.lastModified = new Date();saveInvoice(inv);

Invoice inv = new Invoice();…

inv.lastModifiedBy = currentUser();inv.lastModified = new Date();saveInvoice(inv);

Course crs = new Course();…

crs.lastModifiedBy = currentUser();crs.lastModified = new Date();saveCourse(crs);

Course crs = new Course();…

crs.lastModifiedBy = currentUser();crs.lastModified = new Date();saveCourse(crs);

Tanglingobj.lastModified = new Date();obj.lastModified = new Date();

tx = new Transaction();tx.begin();…

tx.commit();

tx = new Transaction();tx.begin();…

tx.commit();

logger.log(“Start operation”);logger.log(“Start operation”);

obj.id = generateUUID();obj.id = generateUUID();

Course crs = new Course();…

saveCourse(crs);

Course crs = new Course();…

saveCourse(crs);logger.log(“Start operation”);tx = new Transaction();tx.begin();Course crs = new Course();crs.id = generateUUID();…

crs.lastModified = new Date();saveCourse(crs);tx.commit();

logger.log(“Start operation”);tx = new Transaction();tx.begin();Course crs = new Course();crs.id = generateUUID();…

crs.lastModified = new Date();saveCourse(crs);tx.commit();

Aspect-Oriented ProgrammingConcerns are designed and implemented

independently

During build time or runtime, concerns are compiled into full functioned application. This process is called “weaving”.

AOP DefinitionAspect

Join PointPointcutAdvice

Join Pointpublic void createCustomer() {

xxCustomer cust = new Customer();x…

xsaveCustomer(cust);xx

}

public void createCustomer() {xxCustomer cust = new Customer();x…

xsaveCustomer(cust);xx

}

Pointcutpublic void createCustomer() {

xxCustomer cust = new Customer();x…

xsaveCustomer(cust);xx

}

public void createCustomer() {xxCustomer cust = new Customer();x…

xsaveCustomer(cust);xx

}

Advicepublic void createCustomer() {

logger.log(“Start create…”);

Customer cust = new Customer();cust.id = generateUUID();…

cust.lastModified = new Date();saveCustomer(cust);

}

public void createCustomer() {logger.log(“Start create…”);

Customer cust = new Customer();cust.id = generateUUID();…

cust.lastModified = new Date();saveCustomer(cust);

}

AOP Proxy

CallerCaller Target ObjectTarget Object

ProxyAdviceAdviceAdviceAdviceAdviceAdvice

Spring AOP

Supported Pointcutsexecutionwithin

thistargetargs

executionexecution(<modifier> <return-type>

<declaring-type> <method-name> (<param>) <throws>)

execution(* *(..))execution(public void set*(..))execution(public *

com.spring66.service.*.*(..))execution(public *

com.spring66.service..*.*(..))

withinwithin(com.spring66.service.*)

within(com.spring66.service..*)

Advicebeforeafter

after-returningafter-throwing

around

Spring Transaction with AOP

Is that all?

Q & A

top related