Fine-grained transaction configuration

classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

Fine-grained transaction configuration

Vaclav Pech
Hello guys,

I wonder whether someone could point me to more detailed documentation
on transaction management configuration in Grails. I'm struggling to
find info on how to customize tx attributes like transaction propagation
or the read-only flag for my service methods. All I could find so far in
the docs is the basics about the declarative tx flag "static
transactional=true" and a kind of tx template "withTransaction" closure.
However, I'd like to get more fine-grained control on transactions,
something comparable in functionality to the Spring's @Transactional
method annotation. Is there a way to do that in Grails?

Vaclav


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Graeme-Rocher
You can configure your services as Spring Beans in
grails-app/conf/spring/resources.xml

such as

<bean id="bookService" class="BookService" > // etc.

You can then apply whatever transactional behaviours you want

Cheers

On Jan 17, 2008 12:22 PM, Vaclav Pech <[hidden email]> wrote:

> Hello guys,
>
> I wonder whether someone could point me to more detailed documentation
> on transaction management configuration in Grails. I'm struggling to
> find info on how to customize tx attributes like transaction propagation
> or the read-only flag for my service methods. All I could find so far in
> the docs is the basics about the declarative tx flag "static
> transactional=true" and a kind of tx template "withTransaction" closure.
> However, I'd like to get more fine-grained control on transactions,
> something comparable in functionality to the Spring's @Transactional
> method annotation. Is there a way to do that in Grails?
>
> Vaclav
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Vaclav Pech
OK, I see. This will do the job, although I was hoping for a a bit
smoother way to adjust transaction settings. Anyway, thank you for quick
help.

Vaclav


Graeme Rocher wrote:

> You can configure your services as Spring Beans in
> grails-app/conf/spring/resources.xml
>
> such as
>
> <bean id="bookService" class="BookService" > // etc.
>
> You can then apply whatever transactional behaviours you want
>
> Cheers
>
> On Jan 17, 2008 12:22 PM, Vaclav Pech <[hidden email]> wrote:
>  
>> Hello guys,
>>
>> I wonder whether someone could point me to more detailed documentation
>> on transaction management configuration in Grails. I'm struggling to
>> find info on how to customize tx attributes like transaction propagation
>> or the read-only flag for my service methods. All I could find so far in
>> the docs is the basics about the declarative tx flag "static
>> transactional=true" and a kind of tx template "withTransaction" closure.
>> However, I'd like to get more fine-grained control on transactions,
>> something comparable in functionality to the Spring's @Transactional
>> method annotation. Is there a way to do that in Grails?
>>
>> Vaclav
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>    
>
>
>
>  


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Dmitriy Kopylenko-3
Just a thought: how about using @Transactional annotations (the resources.xml would then only need to contain: <tx:annotation-driven />)

Would this work in Grails?

Dmitriy.

2008/1/17, Vaclav Pech <[hidden email]>:
OK, I see. This will do the job, although I was hoping for a a bit
smoother way to adjust transaction settings. Anyway, thank you for quick
help.

Vaclav


Graeme Rocher wrote:

> You can configure your services as Spring Beans in
> grails-app/conf/spring/resources.xml
>
> such as
>
> <bean id="bookService" class="BookService" > // etc.
>
> You can then apply whatever transactional behaviours you want
>
> Cheers
>
> On Jan 17, 2008 12:22 PM, Vaclav Pech <[hidden email]> wrote:
>
>> Hello guys,
>>
>> I wonder whether someone could point me to more detailed documentation
>> on transaction management configuration in Grails. I'm struggling to
>> find info on how to customize tx attributes like transaction propagation
>> or the read-only flag for my service methods. All I could find so far in
>> the docs is the basics about the declarative tx flag "static
>> transactional=true" and a kind of tx template "withTransaction" closure.
>> However, I'd like to get more fine-grained control on transactions,
>> something comparable in functionality to the Spring's @Transactional
>> method annotation. Is there a way to do that in Grails?
>>
>> Vaclav
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Graeme-Rocher
Yes it should do, with a jdk 1.5 vm of course

Cheers

On Jan 18, 2008 1:49 PM, Dmitriy Kopylenko <[hidden email]> wrote:

> Just a thought: how about using @Transactional annotations (the
> resources.xml would then only need to contain: <tx:annotation-driven />)
>
> Would this work in Grails?
>
> Dmitriy.
>
>  2008/1/17, Vaclav Pech <[hidden email]>:
>
> > OK, I see. This will do the job, although I was hoping for a a bit
> > smoother way to adjust transaction settings. Anyway, thank you for quick
> > help.
> >
> > Vaclav
> >
> >
> > Graeme Rocher wrote:
> > > You can configure your services as Spring Beans in
> > > grails-app/conf/spring/resources.xml
> > >
> > > such as
> > >
> > > <bean id="bookService" class="BookService" > // etc.
> > >
> > > You can then apply whatever transactional behaviours you want
> > >
> > > Cheers
> > >
> > > On Jan 17, 2008 12:22 PM, Vaclav Pech <[hidden email]> wrote:
> > >
> > >> Hello guys,
> > >>
> > >> I wonder whether someone could point me to more detailed documentation
> > >> on transaction management configuration in Grails. I'm struggling to
> > >> find info on how to customize tx attributes like transaction
> propagation
> > >> or the read-only flag for my service methods. All I could find so far
> in
> > >> the docs is the basics about the declarative tx flag "static
> > >> transactional=true" and a kind of tx template "withTransaction"
> closure.
> > >> However, I'd like to get more fine-grained control on transactions,
> > >> something comparable in functionality to the Spring's @Transactional
> > >> method annotation. Is there a way to do that in Grails?
> > >>
> > >> Vaclav
> > >>
> > >>
> > >> ---------------------------------------------------------------------
> > >> To unsubscribe from this list please visit:
> > >>
> > >>     http://xircles.codehaus.org/manage_email
> > >>
> > >>
> > >>
> > >
> > >
> > >
> > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Vaclav Pech
In reply to this post by Dmitriy Kopylenko-3
This would be awesome, in my opinion.

Vaclav


Dmitriy Kopylenko wrote:

> Just a thought: how about using @Transactional annotations (the
> resources.xml would then only need to contain: <tx:annotation-driven />)
>
> Would this work in Grails?
>
> Dmitriy.
>
> 2008/1/17, Vaclav Pech <[hidden email]
> <mailto:[hidden email]>>:
>
>     OK, I see. This will do the job, although I was hoping for a a bit
>     smoother way to adjust transaction settings. Anyway, thank you for
>     quick
>     help.
>
>     Vaclav
>
>
>     Graeme Rocher wrote:
>     > You can configure your services as Spring Beans in
>     > grails-app/conf/spring/resources.xml
>     >
>     > such as
>     >
>     > <bean id="bookService" class="BookService" > // etc.
>     >
>     > You can then apply whatever transactional behaviours you want
>     >
>     > Cheers
>     >
>     > On Jan 17, 2008 12:22 PM, Vaclav Pech <[hidden email]
>     <mailto:[hidden email]>> wrote:
>     >
>     >> Hello guys,
>     >>
>     >> I wonder whether someone could point me to more detailed
>     documentation
>     >> on transaction management configuration in Grails. I'm
>     struggling to
>     >> find info on how to customize tx attributes like transaction
>     propagation
>     >> or the read-only flag for my service methods. All I could find
>     so far in
>     >> the docs is the basics about the declarative tx flag "static
>     >> transactional=true" and a kind of tx template "withTransaction"
>     closure.
>     >> However, I'd like to get more fine-grained control on
>     transactions,
>     >> something comparable in functionality to the Spring's
>     @Transactional
>     >> method annotation. Is there a way to do that in Grails?
>     >>
>     >> Vaclav
>     >>
>     >>
>     >>
>     ---------------------------------------------------------------------
>     >> To unsubscribe from this list please visit:
>     >>
>     >>     http://xircles.codehaus.org/manage_email
>     >>
>     >>
>     >>
>     >
>     >
>     >
>     >
>
>
>     ---------------------------------------------------------------------
>     To unsubscribe from this list please visit:
>
>         http://xircles.codehaus.org/manage_email
>     <http://xircles.codehaus.org/manage_email>
>
>


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Vaclav Pech
In reply to this post by Graeme-Rocher
Well, this topic is so interesting for me that I couldn't help it and
spent some time playing around with tuning transactions both with
tx:advice and annotations. Both options work nicely, provided you don't
forget, unlike me, to set the transactional attribute to false.
The support for tx annotations in services is exactly what I was looking
for. Thank you guys for pointing me to this option.

Vaclav


Graeme Rocher wrote:

> Yes it should do, with a jdk 1.5 vm of course
>
> Cheers
>
> On Jan 18, 2008 1:49 PM, Dmitriy Kopylenko <[hidden email]> wrote:
>  
>> Just a thought: how about using @Transactional annotations (the
>> resources.xml would then only need to contain: <tx:annotation-driven />)
>>
>> Would this work in Grails?
>>
>> Dmitriy.
>>
>>    


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Graeme-Rocher
Hi Vaclav

Could you show an example of your configuration? Maybe we can add it to the docs

Cheers

On Jan 18, 2008 4:17 PM, Vaclav Pech <[hidden email]> wrote:

> Well, this topic is so interesting for me that I couldn't help it and
> spent some time playing around with tuning transactions both with
> tx:advice and annotations. Both options work nicely, provided you don't
> forget, unlike me, to set the transactional attribute to false.
> The support for tx annotations in services is exactly what I was looking
> for. Thank you guys for pointing me to this option.
>
> Vaclav
>
>
> Graeme Rocher wrote:
> > Yes it should do, with a jdk 1.5 vm of course
> >
> > Cheers
> >
> > On Jan 18, 2008 1:49 PM, Dmitriy Kopylenko <[hidden email]> wrote:
> >
> >> Just a thought: how about using @Transactional annotations (the
> >> resources.xml would then only need to contain: <tx:annotation-driven />)
> >>
> >> Would this work in Grails?
> >>
> >> Dmitriy.
> >>
> >>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Vaclav Pech
Hello Graeme,

of course, here we go with the transaction configuration details. I decided to first explain a bit on what needs to be done to make the configuration actually work. I've started with Grails just recently, so I might have misunderstood something. Don't hesitate to tell me where I'm wrong and feel free to ask for more details, if needed.

Fine-grained transaction configuration

If the default transactional settings in Grails are not appropriate for a particular project or a part of it, developers can resort to the Spring way to configure transactions. This only affects the beans, which the default Grails behavior is not appropriate for. There are basically two ways to configure transactions on a finer granularity level. Let's assume we have a controller named CalculatorService with a couple of calculation methods.

class CalculatorService {
    static transactional = true
    public BigDecimal calculatePrice() {
    ...
    }
}

We need to make several minor changes before we can set transactional attributes.

1. As we will be creating transactional proxies around our service, we need an interface through which other services or controllers refer to our CalculatorService. So the first change is to extract an interface, named Calculator out of the CalculatorService and change type of all existing statically typed references from other beans to our CalculatorService to be of Calculator type.

class CalculatorService implements Calculator {
...
}

2. The next important step is to set the transactional attribute on the service to false, otherwise our transactional proxies will collide with the default Grails transaction configuration.. 

    static transactional = false

3. We also need to put the "aspectjweaver.jar" file from Spring distribution to the <project>/lib folder to enable aspectJ AOP in the project

4. Now we can start setting transaction attributes. There are two options to achieve this. We can configure transactions either using <tx:advice/> in the resources.xml file or using the @Transactional annotation directly in the Groovy or Java service code.

Using <tx:advice/> to configure transactions from within resources.xml

We need to define a transactional advice <tx:advice/>, which configures transaction attributes for methods and associate it with a pointcut using an advisor. This is nicely described in Spring documentation at http://static.springframework.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative. Notice that the calculatorService bean doesn't need to be defined explicitly in the resources.xml file.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <aop:config>
        <aop:pointcut id="calculatorMethods" expression="execution(* Calculator.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="calculatorMethods"/>
    </aop:config>

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="calculate*" propagation="REQUIRES_NEW" read-only="false" isolation="REPEATABLE_READ"/>
            <tx:method name="list*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="get*" propagation="MANDATORY" read-only="false" isolation="REPEATABLE_READ"/>
        </tx:attributes>
    </tx:advice>

</beans>


Using @Transactional annotation

Developers can also configure transactions in the code of services directly with annotations. The Spring documentation details the option at http://static.springframework.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative-annotations. Notice the minor change in syntax for the rollbackFor and noRollbackFor attribute list values when using annotations in Groovy in contrast with Java. The specifics of annotations in Groovy are described at http://groovy.codehaus.org/Annotations+with+Groovy.

class CalculatorService implements Calculator {
    static transactional = false

    @Transactional(readOnly = false, propagation = Propagation.MANDATORY, noRollbackFor=[RuntimeException.class])
    public BigDecimal calculatePrice() {
    ...
    }
}

The resources.xml file must be updated a bit, too, to enable evaluation of the transactional annotations and creating transactional proxies. Again, the calculatorService bean doesn't need to be defined explicitly in the resources.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <tx:annotation-driven />
</beans>


--------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Graeme-Rocher
Wow, thanks for the detailed write-up Vaclav, I'll update the user guide

Cheers

On Jan 18, 2008 7:35 PM, Vaclav Pech <[hidden email]> wrote:

>
>  Hello Graeme,
>
>  of course, here we go with the transaction configuration details. I decided
> to first explain a bit on what needs to be done to make the configuration
> actually work. I've started with Grails just recently, so I might have
> misunderstood something. Don't hesitate to tell me where I'm wrong and feel
> free to ask for more details, if needed.
>
>  Fine-grained transaction configuration
>
>  If the default transactional settings in Grails are not appropriate for a
> particular project or a part of it, developers can resort to the Spring way
> to configure transactions. This only affects the beans, which the default
> Grails behavior is not appropriate for. There are basically two ways to
> configure transactions on a finer granularity level. Let's assume we have a
> controller named CalculatorService with a couple of calculation methods.
>
>  class CalculatorService {
>      static transactional = true
>      public BigDecimal calculatePrice() {
>      ...
>      }
>  }
>
>  We need to make several minor changes before we can set transactional
> attributes.
>
>  1. As we will be creating transactional proxies around our service, we need
> an interface through which other services or controllers refer to our
> CalculatorService. So the first change is to extract an interface, named
> Calculator out of the CalculatorService and change type of all existing
> statically typed references from other beans to our CalculatorService to be
> of Calculator type.
>
>  class CalculatorService implements Calculator {
>  ...
>  }
>
>  2. The next important step is to set the transactional attribute on the
> service to false, otherwise our transactional proxies will collide with the
> default Grails transaction configuration..
>
>      static transactional = false
>
>  3. We also need to put the "aspectjweaver.jar" file from Spring
> distribution to the <project>/lib folder to enable aspectJ AOP in the
> project
>
>  4. Now we can start setting transaction attributes. There are two options
> to achieve this. We can configure transactions either using <tx:advice/> in
> the resources.xml file or using the @Transactional annotation directly in
> the Groovy or Java service code.
>
>  Using <tx:advice/> to configure transactions from within resources.xml
>
>  We need to define a transactional advice <tx:advice/>, which configures
> transaction attributes for methods and associate it with a pointcut using an
> advisor. This is nicely described in Spring documentation at
> http://static.springframework.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative.
> Notice that the calculatorService bean doesn't need to be defined explicitly
> in the resources.xml file.
>
>  <beans xmlns="http://www.springframework.org/schema/beans"
>         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>         xmlns:aop="http://www.springframework.org/schema/aop"
>         xmlns:tx="http://www.springframework.org/schema/tx"
>         xsi:schemaLocation="
>          http://www.springframework.org/schema/aop
> http://www.springframework.org/schema/aop/spring-aop.xsd
>          http://www.springframework.org/schema/tx
> http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
>          http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
>
>      <aop:config>
>          <aop:pointcut id="calculatorMethods" expression="execution(*
> Calculator.*(..))"/>
>          <aop:advisor advice-ref="txAdvice"
> pointcut-ref="calculatorMethods"/>
>      </aop:config>
>
>      <tx:advice id="txAdvice">
>          <tx:attributes>
>              <tx:method name="calculate*" propagation="REQUIRES_NEW"
> read-only="false" isolation="REPEATABLE_READ"/>
>              <tx:method name="list*" propagation="REQUIRED"
> read-only="true"/>
>              <tx:method name="get*" propagation="MANDATORY"
> read-only="false" isolation="REPEATABLE_READ"/>
>          </tx:attributes>
>      </tx:advice>
>  </beans>
>
>
>  Using @Transactional annotation
>
>  Developers can also configure transactions in the code of services directly
> with annotations. The Spring documentation details the option at
> http://static.springframework.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative-annotations.
> Notice the minor change in syntax for the rollbackFor and noRollbackFor
> attribute list values when using annotations in Groovy in contrast with
> Java. The specifics of annotations in Groovy are described at
> http://groovy.codehaus.org/Annotations+with+Groovy.
>
>  class CalculatorService implements Calculator {
>      static transactional = false
>
>      @Transactional(readOnly = false, propagation = Propagation.MANDATORY,
> noRollbackFor=[RuntimeException.class])
>      public BigDecimal calculatePrice() {
>      ...
>      }
>  }
>
>  The resources.xml file must be updated a bit, too, to enable evaluation of
> the transactional annotations and creating transactional proxies. Again, the
> calculatorService bean doesn't need to be defined explicitly in the
> resources.xml file.
>
>  <?xml version="1.0" encoding="UTF-8"?>
>  <beans xmlns="http://www.springframework.org/schema/beans"
>         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>         xmlns:tx="http://www.springframework.org/schema/tx"
>         xsi:schemaLocation="
>          http://www.springframework.org/schema/tx
> http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
>          http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
>
>      <tx:annotation-driven />
>  </beans>
>
>
>  --------------------------------------------------------------------- To
> unsubscribe from this list please visit:
> http://xircles.codehaus.org/manage_email



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

RE: Fine-grained transaction configuration

Jay Goldman
Great write-up ...

now if there was only a more 'CoC' mechanism, say
 static transactional = [ calculatePrice: [readOnly: false,
propagation:Propagation.MANDATORY,
noRollbackFor:[RuntimeException.class]], methodX: [ ... ] ]

with some default values used for:
  static transactional = [ 'calculatePrice', 'methodX' ]

and
   def calculatorService
would just have to be set to the proxy...

just a suggesion.

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf
Of Graeme Rocher
Sent: Friday, January 18, 2008 2:45 PM
To: [hidden email]
Subject: Re: [grails-user] Fine-grained transaction configuration

Wow, thanks for the detailed write-up Vaclav, I'll update the user guide

Cheers

On Jan 18, 2008 7:35 PM, Vaclav Pech <[hidden email]> wrote:

>
>  Hello Graeme,
>
>  of course, here we go with the transaction configuration details. I
> decided to first explain a bit on what needs to be done to make the
> configuration actually work. I've started with Grails just recently,
> so I might have misunderstood something. Don't hesitate to tell me
> where I'm wrong and feel free to ask for more details, if needed.
>
>  Fine-grained transaction configuration
>
>  If the default transactional settings in Grails are not appropriate
> for a particular project or a part of it, developers can resort to the

> Spring way to configure transactions. This only affects the beans,
> which the default Grails behavior is not appropriate for. There are
> basically two ways to configure transactions on a finer granularity
> level. Let's assume we have a controller named CalculatorService with
a couple of calculation methods.
>
>  class CalculatorService {
>      static transactional = true
>      public BigDecimal calculatePrice() {
>      ...
>      }
>  }
>
>  We need to make several minor changes before we can set transactional

> attributes.
>
>  1. As we will be creating transactional proxies around our service,
> we need an interface through which other services or controllers refer

> to our CalculatorService. So the first change is to extract an
> interface, named Calculator out of the CalculatorService and change
> type of all existing statically typed references from other beans to
> our CalculatorService to be of Calculator type.
>
>  class CalculatorService implements Calculator {  ...
>  }
>
>  2. The next important step is to set the transactional attribute on
> the service to false, otherwise our transactional proxies will collide

> with the default Grails transaction configuration..
>
>      static transactional = false
>
>  3. We also need to put the "aspectjweaver.jar" file from Spring
> distribution to the <project>/lib folder to enable aspectJ AOP in the
> project
>
>  4. Now we can start setting transaction attributes. There are two
> options to achieve this. We can configure transactions either using
> <tx:advice/> in the resources.xml file or using the @Transactional
> annotation directly in the Groovy or Java service code.
>
>  Using <tx:advice/> to configure transactions from within
> resources.xml
>
>  We need to define a transactional advice <tx:advice/>, which
> configures transaction attributes for methods and associate it with a
> pointcut using an advisor. This is nicely described in Spring
> documentation at
http://static.springframework.org/spring/docs/2.0.x/reference/transactio
n.html#transaction-declarative.

> Notice that the calculatorService bean doesn't need to be defined
> explicitly in the resources.xml file.
>
>  <beans xmlns="http://www.springframework.org/schema/beans"
>         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>         xmlns:aop="http://www.springframework.org/schema/aop"
>         xmlns:tx="http://www.springframework.org/schema/tx"
>         xsi:schemaLocation="
>          http://www.springframework.org/schema/aop
> http://www.springframework.org/schema/aop/spring-aop.xsd
>          http://www.springframework.org/schema/tx
> http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
>          http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
>
>      <aop:config>
>          <aop:pointcut id="calculatorMethods" expression="execution(*
> Calculator.*(..))"/>
>          <aop:advisor advice-ref="txAdvice"
> pointcut-ref="calculatorMethods"/>
>      </aop:config>
>
>      <tx:advice id="txAdvice">
>          <tx:attributes>
>              <tx:method name="calculate*" propagation="REQUIRES_NEW"
> read-only="false" isolation="REPEATABLE_READ"/>
>              <tx:method name="list*" propagation="REQUIRED"
> read-only="true"/>
>              <tx:method name="get*" propagation="MANDATORY"
> read-only="false" isolation="REPEATABLE_READ"/>
>          </tx:attributes>
>      </tx:advice>
>  </beans>
>
>
>  Using @Transactional annotation
>
>  Developers can also configure transactions in the code of services
> directly with annotations. The Spring documentation details the option

> at
http://static.springframework.org/spring/docs/2.0.x/reference/transactio
n.html#transaction-declarative-annotations.

> Notice the minor change in syntax for the rollbackFor and
> noRollbackFor attribute list values when using annotations in Groovy
> in contrast with Java. The specifics of annotations in Groovy are
> described at http://groovy.codehaus.org/Annotations+with+Groovy.
>
>  class CalculatorService implements Calculator {
>      static transactional = false
>
>      @Transactional(readOnly = false, propagation =
> Propagation.MANDATORY,
> noRollbackFor=[RuntimeException.class])
>      public BigDecimal calculatePrice() {
>      ...
>      }
>  }
>
>  The resources.xml file must be updated a bit, too, to enable
> evaluation of the transactional annotations and creating transactional

> proxies. Again, the calculatorService bean doesn't need to be defined
> explicitly in the resources.xml file.
>
>  <?xml version="1.0" encoding="UTF-8"?>  <beans
> xmlns="http://www.springframework.org/schema/beans"
>         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>         xmlns:tx="http://www.springframework.org/schema/tx"
>         xsi:schemaLocation="
>          http://www.springframework.org/schema/tx
> http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
>          http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
>
>      <tx:annotation-driven />
>  </beans>
>
>
>  ---------------------------------------------------------------------

> To unsubscribe from this list please visit:
> http://xircles.codehaus.org/manage_email



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Marcos Silva Pereira
Jay,

Maybe, a more Grails approach would be:

static transactional = {
    calculatePrice(readOnly: false, propagation:MANDATORY, noRollbackFor:RuntimeException)
    methodX( ... )
}

The idiom below is based on domain constraints. Is it worth of a jira issue?

Kind Regards,


Great write-up ...

now if there was only a more 'CoC' mechanism, say
 static transactional = [ calculatePrice: [readOnly: false,
propagation:Propagation.MANDATORY,
noRollbackFor:[RuntimeException.class]], methodX: [ ... ] ]

with some default values used for:
 static transactional = [ 'calculatePrice', 'methodX' ]

and
  def calculatorService
would just have to be set to the proxy...

just a suggesion.




--
Marcos Silva Pereira
http://marcospereira.wordpress.com
"You only live once.", James Brown
Reply | Threaded
Open this post in threaded view
|

Re: Fine-grained transaction configuration

Graeme-Rocher
Sure raise a JIRA, its post 1.0, but worth JIRA'ing

Cheers

On Jan 18, 2008 8:36 PM, Marcos Silva Pereira <[hidden email]> wrote:

> Jay,
>
> Maybe, a more Grails approach would be:
>
> static transactional = {
>     calculatePrice(readOnly: false, propagation:MANDATORY,
> noRollbackFor:RuntimeException)
>     methodX( ... )
> }
>
> The idiom below is based on domain constraints. Is it worth of a jira issue?
>
> Kind Regards,
>
>
>
>
>
> > Great write-up ...
> >
> > now if there was only a more 'CoC' mechanism, say
> >  static transactional = [ calculatePrice: [readOnly: false,
> > propagation:Propagation.MANDATORY,
> > noRollbackFor:[RuntimeException.class]], methodX: [ ... ] ]
> >
> > with some default values used for:
> >  static transactional = [ 'calculatePrice', 'methodX' ]
> >
> > and
> >   def calculatorService
> > would just have to be set to the proxy...
> >
> > just a suggesion.
> >
> >
> >
> >
> >
>
>
>
> --
> Marcos Silva Pereira
> http://marcospereira.wordpress.com
> "You only live once.", James Brown



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

How does Grails know what env (dev or prod) it is running in?

barvinsky

Can anyone help me with the topic?

Does Grails set some internal var that I can use to figure out if I am
in dev or prod?

Also is it possible to introduce a new env like "stage" ?

Thanks
Andrew.

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: How does Grails know what env (dev or prod) it is running in?

Mike Brevoort
Andrew,

You should be able to get the environment like this:
System.getProperty("grails.env")

As for adding another environment, add another section to your Config, for example

environments {
    myenv {

    }
}

And then when you start grails use "grails - Dgrails.env=myenv run-app"

Mike

On Jan 18, 2008 1:59 PM, Andrew Barvinsky (abarvins) <[hidden email]> wrote:

Can anyone help me with the topic?

Does Grails set some internal var that I can use to figure out if I am
in dev or prod?

Also is it possible to introduce a new env like "stage" ?

Thanks
Andrew.

---------------------------------------------------------------------
To unsubscribe from this list please visit:

   http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How does Grails know what env (dev or prod) it is running in?

chris8185
In reply to this post by barvinsky
Yes.
Look into GrailsUtil.environment.

In development I am required to use a dataSource with my development
credentials and in production I have to use a JNDI dataSource with
production data access.

I use the GrailsUtil object and a switch statement to accomplish this
feat.  It looks like the following:

switch (GrailsUtil.environment){
case "development":
    return // locally created dataSource
case "production":
    return // jndi dataSource
default:
    return null
}

Hope that helps.
Cheers,
Christopher Kwiatkowski



Andrew Barvinsky (abarvins) wrote:

> Can anyone help me with the topic?
>
> Does Grails set some internal var that I can use to figure out if I am
> in dev or prod?
>
> Also is it possible to introduce a new env like "stage" ?
>
> Thanks
> Andrew.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>  

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

RE: How does Grails know what env (dev or prod) it is running in?

barvinsky
In reply to this post by Mike Brevoort
Thanks for your help Mike,
 
can I use it like this:
 
grails stage war
grails stage run-app
 
?


From: Mike Brevoort [mailto:[hidden email]]
Sent: Friday, January 18, 2008 1:24 PM
To: [hidden email]
Subject: Re: [grails-user] How does Grails know what env (dev or prod) it is running in?

Andrew,

You should be able to get the environment like this:
System.getProperty("grails.env")

As for adding another environment, add another section to your Config, for example

environments {
    myenv {

    }
}

And then when you start grails use "grails - Dgrails.env=myenv run-app"

Mike

On Jan 18, 2008 1:59 PM, Andrew Barvinsky (abarvins) <[hidden email]> wrote:

Can anyone help me with the topic?

Does Grails set some internal var that I can use to figure out if I am
in dev or prod?

Also is it possible to introduce a new env like "stage" ?

Thanks
Andrew.

---------------------------------------------------------------------
To unsubscribe from this list please visit:

   http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

RE: How does Grails know what env (dev or prod) it is running in?

barvinsky
In reply to this post by chris8185
Thanks Christopher, it does !
 

-----Original Message-----
From: Christopher Kwiatkowski [mailto:[hidden email]]
Sent: Friday, January 18, 2008 1:29 PM
To: [hidden email]
Subject: Re: [grails-user] How does Grails know what env (dev or prod)
it is running in?

Yes.
Look into GrailsUtil.environment.

In development I am required to use a dataSource with my development
credentials and in production I have to use a JNDI dataSource with
production data access.

I use the GrailsUtil object and a switch statement to accomplish this
feat.  It looks like the following:

switch (GrailsUtil.environment){
case "development":
    return // locally created dataSource case "production":
    return // jndi dataSource
default:
    return null
}

Hope that helps.
Cheers,
Christopher Kwiatkowski



Andrew Barvinsky (abarvins) wrote:
> Can anyone help me with the topic?
>
> Does Grails set some internal var that I can use to figure out if I am

> in dev or prod?
>
> Also is it possible to introduce a new env like "stage" ?
>
> Thanks
> Andrew.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>  

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: How does Grails know what env (dev or prod) it is running in?

Mike Brevoort
In reply to this post by barvinsky
I don't think so.  I "think" the dev, test and prod keywords are the only ones that will work to specify the corresponding default environments.   You have to specify it like this: -Dgrails.env=myenv .  The best thing to do is give it a try though.  You can probably have a look through the groovy scripts as well to find out exactly what's going on.

Mike

On Jan 18, 2008 4:16 PM, Andrew Barvinsky (abarvins) <[hidden email]> wrote:
Thanks for your help Mike,
 
can I use it like this:
 
grails stage war
grails stage run-app
 
?


From: Mike Brevoort [mailto:[hidden email]]
Sent: Friday, January 18, 2008 1:24 PM
To: [hidden email]
Subject: Re: [grails-user] How does Grails know what env (dev or prod) it is running in?

Andrew,

You should be able to get the environment like this:
System.getProperty("grails.env")

As for adding another environment, add another section to your Config, for example

environments {
    myenv {

    }
}

And then when you start grails use "grails - Dgrails.env=myenv run-app"

Mike

On Jan 18, 2008 1:59 PM, Andrew Barvinsky (abarvins) <[hidden email]> wrote:

Can anyone help me with the topic?

Does Grails set some internal var that I can use to figure out if I am
in dev or prod?

Also is it possible to introduce a new env like "stage" ?

Thanks
Andrew.

---------------------------------------------------------------------
To unsubscribe from this list please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

RE: How does Grails know what env (dev or prod) it is running in?

Matt Lachman

http://grails.org/doc/1.0.x/guide/3.%20Configuration.html#3.2%20Environments

 

The above link describes this pretty succinctly.

 

Matt

 


From: Mike Brevoort [mailto:[hidden email]]
Sent: Saturday, January 19, 2008 10:54 AM
To: [hidden email]
Subject: Re: [grails-user] How does Grails know what env (dev or prod) it is running in?

 

I don't think so.  I "think" the dev, test and prod keywords are the only ones that will work to specify the corresponding default environments.   You have to specify it like this: -Dgrails.env=myenv .  The best thing to do is give it a try though.  You can probably have a look through the groovy scripts as well to find out exactly what's going on.

Mike

On Jan 18, 2008 4:16 PM, Andrew Barvinsky (abarvins) <[hidden email]> wrote:

Thanks for your help Mike,

 

can I use it like this:

 

grails stage war

grails stage run-app

 

?