• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,576
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
19
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. © 2012 SpringSource, by VMware. All rights reservedBurt BeckwithSpringSourceGrails Transactions
  • 2. 22What is a Transaction?
  • 3. 33What is a Transaction? getConnection() setAutoCommit(false) do work COMMIT Plus error handling, rollback
  • 4. 55ACID Atomicity Consistency Isolation Durability
  • 5. 66How To Use Transactions in Grails?
  • 6. 77How To Use Transactions in Grails? Transactional services Springs @Transactional annotation And new in Grails 2.3, the grails.transaction.Transactionalannotation
  • 7. 88Grails Transactional services $ grails create-service thepackage txclass TheService {}
  • 8. 99@Transactional
  • 9. 1010@Transactional String value() default ""; Propagation propagation() default Propagation.REQUIRED; Isolation isolation() default Isolation.DEFAULT; int timeout() default TransactionDefinition.TIMEOUT_DEFAULT; boolean readOnly() default false; Class<? extends Throwable>[] rollbackFor() default {}; String[] rollbackForClassName() default {}; Class<? extends Throwable>[] noRollbackFor() default {}; String[] noRollbackForClassName() default {};
  • 10. 1111@Transactional String value() default ""; Propagation propagation() default Propagation.REQUIRED; Isolation isolation() default Isolation.DEFAULT; int timeout() default TransactionDefinition.TIMEOUT_DEFAULT; boolean readOnly() default false; Class<? extends Throwable>[] rollbackFor() default {}; String[] rollbackForClassName() default {}; Class<? extends Throwable>[] noRollbackFor() default {}; String[] noRollbackForClassName() default {};
  • 11. 1212Transaction Isolation org.springframework.transaction.annotation.Isolation READ_UNCOMMITTED• Order is maintained• Allows dirty reads READ_COMMITTED• Only reads committed data, but allows non-repeatable reads REPEATABLE_READ• Allows phantom reads SERIALIZABLE• Blocks read and write access between transactions, guarantees correct results DEFAULT• Use the default database isolation, often READ_COMMITTED (MySQL usesREPEATABLE_READ)
  • 12. 1313Demo
  • 13. 1414@Transactional String value() default ""; Propagation propagation() default Propagation.REQUIRED; Isolation isolation() default Isolation.DEFAULT; int timeout() default TransactionDefinition.TIMEOUT_DEFAULT; boolean readOnly() default false; Class<? extends Throwable>[] rollbackFor() default {}; String[] rollbackForClassName() default {}; Class<? extends Throwable>[] noRollbackFor() default {}; String[] noRollbackForClassName() default {};
  • 14. 1515Transaction Propagation org.springframework.transaction.annotation.Propagation REQUIRED SUPPORTS MANDATORY REQUIRES_NEW NOT_SUPPORTED NEVER NESTED
  • 15. 1616@Transactional String value() default ""; Propagation propagation() default Propagation.REQUIRED; Isolation isolation() default Isolation.DEFAULT; int timeout() default TransactionDefinition.TIMEOUT_DEFAULT; boolean readOnly() default false; Class<? extends Throwable>[] rollbackFor() default {}; String[] rollbackForClassName() default {}; Class<? extends Throwable>[] noRollbackFor() default {}; String[] noRollbackForClassName() default {};
  • 16. 1717@Transactional String value() default ""; Propagation propagation() default Propagation.REQUIRED; Isolation isolation() default Isolation.DEFAULT; int timeout() default TransactionDefinition.TIMEOUT_DEFAULT; boolean readOnly() default false; Class<? extends Throwable>[] rollbackFor() default {}; String[] rollbackForClassName() default {}; Class<? extends Throwable>[] noRollbackFor() default {}; String[] noRollbackForClassName() default {};
  • 17. 1818Annotated Service Examples
  • 18. 1919Annotated Service Examplespackage com.mycompanyimport org.springframework.transaction.annotation.Propagationimport org.springframework.transaction.annotation.Transactional@Transactionalclass SomeService {def someMethod() {...}@Transactional(propagation=Propagation.MANDATORY)def someOtherMethod() {...}}
  • 19. 2020Annotated Service Examplespackage com.mycompanyimport org.springframework.transaction.annotation.Propagationimport org.springframework.transaction.annotation.Transactionalclass SomeOtherService {def someMethod() {...}@Transactionaldef someOtherMethod() {...}@Transactional(propagation=Propagation.MANDATORY)def yetAnotherMethod() {...}}
  • 20. 2121Unintentionally Bypassing the Bean Proxy
  • 21. 2222Unintentionally Bypassing the Bean Proxy@Transactionalvoid someMethod(...) {// do some work ...storeAuditData(...)}@Transactional(propagation=Propagation.REQUIRES_NEW)void storeAuditData(...) {//}
  • 22. 2323Unintentionally Bypassing the Bean Proxydef grailsApplication@Transactionalvoid someMethod(...) {// do some work ...def myProxy = grailsApplication.mainContext.fooServicemyProxy.storeAuditData(...)}@Transactional(propagation=Propagation.REQUIRES_NEW)void storeAuditData(...) {//}
  • 23. 2424Spring Classes and Utility Methods
  • 24. 2525PlatformTransactionManager org.springframework.transaction.PlatformTransactionManager(org.springframework.orm.hibernate3.HibernateTransactionManager)• TransactionStatus getTransaction(TransactionDefinition definition) throwsTransactionException;• void commit(TransactionStatus status) throws TransactionException;• void rollback(TransactionStatus status) throws TransactionException;
  • 25. 2626TransactionSynchronizationManager org.springframework.transaction.support.TransactionSynchronizationManager• public static void bindResource(Object key, Object value)• public static Object unbindResource(Object key)• public static boolean hasResource(Object key)• public static Object getResource(Object key)
  • 26. 2727TransactionSynchronizationManager For example:• TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session))• TransactionSynchronizationManager.bindResource(getDataSource(), new ConnectionHolder(connection));
  • 27. 2828TransactionAspectSupport org.springframework.transaction.interceptor.TransactionAspectSupport• public static TransactionStatus currentTransactionStatus() throwsNoTransactionException
  • 28. 2929TransactionAspectSupport Useful for MetaClass utility methods:• isTransactionActive()• → TransactionSynchronizationManager.isSynchronizationActive()• getCurrentTransactionStatus()• → TransactionAspectSupport.currentTransactionStatus()• setRollbackOnly()• → TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()• isRollbackOnly()• → getCurrentTransactionStatus().isRollbackOnly()
  • 29. 3030LazyConnectionDataSourceProxy org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy• Used by default in Grails 2.3• Waits to create a connection until its actually needed• Caches calls to setAutoCommit, setReadOnly, setTransactionIsolation, etc.
  • 30. 3131Two-phase Commit (2PC)
  • 31. 3232Two-phase Commit (2PC) The Atomicos plugin is the easiest way• http://grails.org/plugin/atomikos• http://grails-plugins.github.io/grails-atomikos/docs/manual/index.html Supports multiple databases and JMS (and any other XA-complianttechnology)
  • 32. 3333Demo
  • 33. 3434Some Thoughts About Scaffolding
  • 34. 3535Want To Learn More?
  • 35. 3737Thanks!