Grails Transactions

2,323 views

Published on

Slides from my GR8Conf EU 2013 talk

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,323
On SlideShare
0
From Embeds
0
Number of Embeds
1,306
Actions
Shares
0
Downloads
24
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Grails Transactions

  1. 1. © 2012 SpringSource, by VMware. All rights reservedBurt BeckwithSpringSourceGrails Transactions
  2. 2. 22What is a Transaction?
  3. 3. 33What is a Transaction? getConnection() setAutoCommit(false) do work COMMIT Plus error handling, rollback
  4. 4. 55ACID Atomicity Consistency Isolation Durability
  5. 5. 66How To Use Transactions in Grails?
  6. 6. 77How To Use Transactions in Grails? Transactional services Springs @Transactional annotation And new in Grails 2.3, the grails.transaction.Transactionalannotation
  7. 7. 88Grails Transactional services $ grails create-service thepackage txclass TheService {}
  8. 8. 99@Transactional
  9. 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. 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. 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. 12. 1313Demo
  13. 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. 14. 1515Transaction Propagation org.springframework.transaction.annotation.Propagation REQUIRED SUPPORTS MANDATORY REQUIRES_NEW NOT_SUPPORTED NEVER NESTED
  15. 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. 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. 17. 1818Annotated Service Examples
  18. 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. 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. 20. 2121Unintentionally Bypassing the Bean Proxy
  21. 21. 2222Unintentionally Bypassing the Bean Proxy@Transactionalvoid someMethod(...) {// do some work ...storeAuditData(...)}@Transactional(propagation=Propagation.REQUIRES_NEW)void storeAuditData(...) {//}
  22. 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. 23. 2424Spring Classes and Utility Methods
  24. 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. 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. 26. 2727TransactionSynchronizationManager For example:• TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session))• TransactionSynchronizationManager.bindResource(getDataSource(), new ConnectionHolder(connection));
  27. 27. 2828TransactionAspectSupport org.springframework.transaction.interceptor.TransactionAspectSupport• public static TransactionStatus currentTransactionStatus() throwsNoTransactionException
  28. 28. 2929TransactionAspectSupport Useful for MetaClass utility methods:• isTransactionActive()• → TransactionSynchronizationManager.isSynchronizationActive()• getCurrentTransactionStatus()• → TransactionAspectSupport.currentTransactionStatus()• setRollbackOnly()• → TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()• isRollbackOnly()• → getCurrentTransactionStatus().isRollbackOnly()
  29. 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. 30. 3131Two-phase Commit (2PC)
  31. 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. 32. 3333Demo
  33. 33. 3434Some Thoughts About Scaffolding
  34. 34. 3535Want To Learn More?
  35. 35. 3737Thanks!

×