Presentation is loading. Please wait.

Presentation is loading. Please wait.

@siros_s. Agenda: Spring Transaction Why Spring Transaction Transaction Manager Declarative Transaction with Propagation Exception.

Similar presentations


Presentation on theme: "@siros_s. Agenda: Spring Transaction Why Spring Transaction Transaction Manager Declarative Transaction with Propagation Exception."— Presentation transcript:

1 @siros_s

2 Agenda: Spring Transaction Why Spring Transaction Transaction Manager Declarative Transaction with 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 JDBC Spring Transaction ORMJCA Application JTA

6 Transactional Resource Sync CC C Spring Transaction

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

8 Declarative Transaction ACB D DCB Commit Begin Commit

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 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 SUPPORTS NOT_SUPPORTED REQUIRED REQUIRES_NEW MANDATORY NEVER NESTED

16 Example: Propagation ACB D NEVER REQUIRED REQUIRES_NEW REQUIRED SUPPORTS MANDAROTY REQUIRES_NEW

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); 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);

24 Scattering: Modified obj.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);

25 Tangling obj.lastModified = new Date(); tx = new Transaction(); tx.begin(); … tx.commit(); tx = new Transaction(); tx.begin(); … tx.commit(); logger.log(Start operation); 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();

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(); x … x saveCustomer(cust); x } public void createCustomer() { x x Customer cust = new Customer(); x … x saveCustomer(cust); x x }

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

30 Advice public 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); }

31 AOP Proxy Caller Target Object Proxy Advice

32 Spring AOP

33 Supported Pointcuts execution within this target args

34 execution execution( ( ) ) 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 "@siros_s. Agenda: Spring Transaction Why Spring Transaction Transaction Manager Declarative Transaction with Propagation Exception."

Similar presentations


Ads by Google