Grails
  1. Grails
  2. GRAILS-7093

Better support to programmatic transactions (withTransaction closure extension/changing)

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 2.4
    • Component/s: Persistence
    • Labels:
      None
    • Patch Submitted:
      Yes

      Description

      It's just a suggestion, but i really believe it would be a useful improvement:

      Apparently, withTransaction supports only propagation "required". This could be enough for regular applications, but it "fails" when you need a fine-grained transaction control.

      The suggested improvement is to offer another closure or to change withTransaction definition (HibernatePluginSupport.groovy) to support additional properties. These properties will be used to configure TransactionTemplate instance. I've written something similar in other project:

          def ctx = grailsApplication.mainContext
          grailsApplication.domainClasses.each {
            domainClass ->
      	domainClass.metaClass.static.withNewTransaction = {
      	  properties = [propagationBehavior: TransactionDefinition.PROPAGATION_REQUIRES_NEW],
      	  Closure callable ->
      	    def template = new TransactionTemplate(ctx.getBean('transactionManager'))
      	    properties.each {
      	      property ->
      		template[property.key] = property.value
      	    }
      	    template.execute({status ->
      	      callable.call(status)
      			     } as TransactionCallback)
              }
          }
      

      So we can easily start a new different transaction (even inside a Service class method/closure):

      MyDomainClass.withNewTransaction = {
         // Your code here
      }
      

      or explicitly:

      MyDomainClass.withNewTransaction(propagationBehavior: TransactionDefinition.PROPAGATION_REQUIRES_NEW) = {
         // Your code here
      }
      

      or even something more complex:

      MyDomainClass.withNewTransaction(propagationBehavior: TransactionDefinition.PROPAGATION_NESTED, isolationLevelName: 'ISOLATION_READ_UNCOMMITTED') = {
         // Your code here
      }
      

      It makes more sense to me to change withTransaction implementation than adding another closure because not every propagation type will create a "new transaction".

      For a new closure implementation, properties could be initialized as the sample (it's just a suggestion).
      For a changing in withTransaction implementation, properties should be initialized as an empty Map (properties = [:]) or null map (with null check), for backward compatibility.

      Reference:
      http://grails.org/doc/latest/ref/Domain%20Classes/withTransaction.html
      http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/transaction/TransactionDefinition.html#PROPAGATION_REQUIRED
      http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/transaction/support/TransactionTemplate.html

      Thanks!

        Issue Links

          Activity

          Hide
          Aaron Long added a comment -

          Some guy named Graeme added a withNewTransaction() method on 3/29/11 in revision a4717a1 of GormStaticApi

          There is also a version that let's you pass a TransactionDefinition. However, I don't see anyway to override the timeout and indicate read-only at the transaction level so maybe some of this ticket is still outstanding.

          Show
          Aaron Long added a comment - Some guy named Graeme added a withNewTransaction() method on 3/29/11 in revision a4717a1 of GormStaticApi There is also a version that let's you pass a TransactionDefinition. However, I don't see anyway to override the timeout and indicate read-only at the transaction level so maybe some of this ticket is still outstanding.
          Hide
          Mariano Lopez added a comment -

          withNewTransaction() works, but isn't documented anywhere.

          Show
          Mariano Lopez added a comment - withNewTransaction() works, but isn't documented anywhere.
          Hide
          Tamer Shahin added a comment -

          will this be fixed sooner or later?

          Show
          Tamer Shahin added a comment - will this be fixed sooner or later?
          Hide
          Graeme Rocher added a comment -

          Something else to sneak into 2.4 I think

          Show
          Graeme Rocher added a comment - Something else to sneak into 2.4 I think
          Hide
          Tamer Shahin added a comment -

          thank you!
          well, considering that it will be available on 2.4, for the moment we preferred not to use the plugin by Daniel Lima but we created a thinner helper with methods like this:

          static DefaultTransactionDefinition txDefinition()

          { DefaultTransactionDefinition definition = new DefaultTransactionDefinition() definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED) definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED) return definition }

          and, only where is needed we import statically the method above and using it like this:

          Book.withTransaction txDefinition(),

          { .. }
          Show
          Tamer Shahin added a comment - thank you! well, considering that it will be available on 2.4, for the moment we preferred not to use the plugin by Daniel Lima but we created a thinner helper with methods like this: static DefaultTransactionDefinition txDefinition() { DefaultTransactionDefinition definition = new DefaultTransactionDefinition() definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED) definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED) return definition } and, only where is needed we import statically the method above and using it like this: Book.withTransaction txDefinition(), { .. }

            People

            • Assignee:
              Jeff Scott Brown
              Reporter:
              Daniel Henrique Alves Lima
            • Votes:
              24 Vote for this issue
              Watchers:
              19 Start watching this issue

              Dates

              • Created:
                Updated:
                Last Reviewed:

                Development