Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring Transaction & Spring AOP

Similar presentations

Presentation on theme: "Spring Transaction & Spring AOP"— Presentation transcript:

1 Spring Transaction & Spring AOP
ศิรส สุภาวิตา @siros_s

2 Agenda: Spring Transaction
Why Spring Transaction Transaction Manager Declarative Transaction with Annotation @Transactional Propagation Exception Handling

3 Why Spring Transaction?
Uniform API Transactional resource synchronization On and off server Declarative Transaction

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

5 Uniform API Application Spring Transaction JDBC ORM JCA JTA

6 Transactional Resource Sync
Spring Transaction

7 On and Off Server Spring Spring Spring Servlet Container J(2)EE

8 Declarative Transaction
B C Begin Commit D Begin Commit D B C

9 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

10 Transactional Resource
Transaction Manager Transactional Resource Transaction Manager

11 Transaction Annotation
Use annotation to declare transaction scope Require just one line of config in XML

12 Example: MyService

13 Example: Transactional MyService

14 Example: Detailed MyService

15 Propagation Define how transaction manager handles transaction


17 Exception Handling Default behavior
Rollback for unchecked exception No rollback for checked exception Can be changed properties rollbackFor rollbackForClassname noRollbackFor noRollbackForClassname

18 Example: Exception Handling

19 Wait!! There is a better way!!
Q & A

20 Agenda: Spring AOP Problems of existing approach AOP Definition
AOP Proxy Spring AOP Spring Transaction with AOP

21 Problems?? Transaction policy is still defined locally on each class rather than globally across all classes. Modularity & Reusability Scattering & Tangling Even a medium sized application has multiple “concerns”.

22 Example of Transaction Policy
All 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.

23 Scattering obj.lastModified = new Date();
Customer cust = new Customer(); cust.lastModified = new Date(); saveCustomer(cust); Invoice inv = new Invoice(); inv.lastModified = new Date(); saveInvoice(inv); Course crs = new Course(); crs.lastModified = new Date(); saveCourse(crs);

24 Scattering: Modified obj.lastModifiedBy = currentUser();
obj.lastModified = new Date(); 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); Course crs = new Course(); crs.lastModifiedBy = currentUser(); crs.lastModified = new Date(); saveCourse(crs);

25 Tangling obj.lastModified = new Date(); Course crs = new Course(); …
saveCourse(crs); logger.log(“Start operation”); logger.log(“Start operation”); tx = new Transaction(); tx.begin(); Course crs = new Course(); = generateUUID(); crs.lastModified = new Date(); saveCourse(crs); tx.commit(); = generateUUID(); tx = new Transaction(); tx.begin(); tx.commit();

26 Aspect-Oriented Programming
Concerns are designed and implemented independently During build time or runtime, concerns are compiled into full functioned application. This process is called “weaving”.

27 AOP Definition Aspect Join Point Pointcut Advice

28 Join Point public void createCustomer() { x
Customer cust = new Customer(); saveCustomer(cust); }

29 Pointcut public void createCustomer() { x
Customer cust = new Customer(); saveCustomer(cust); }

30 Advice public void createCustomer() { logger.log(“Start create…”);
Customer cust = new Customer(); = generateUUID(); cust.lastModified = new Date(); saveCustomer(cust); }

31 AOP Proxy Caller Target Object Advice Proxy Advice Advice

32 Spring AOP

33 Supported Pointcuts execution within this target args

34 execution execution(<modifier> <return-type> <declaring-type> <method-name> (<param>) <throws>) execution(* *(..)) execution(public void set*(..)) execution(public * com.spring66.service.*.*(..)) execution(public * com.spring66.service..*.*(..))

35 within within(com.spring66.service.*) within(com.spring66.service..*)

36 Advice before after after-returning after-throwing around

37 Spring Transaction with AOP

38 Is that all? Q & A

Download ppt "Spring Transaction & Spring AOP"

Similar presentations

Ads by Google