2

Suppose the following situation..

  • I have declared three different datasources (datasource, datasource_b and datasource_c) in my DataSource.groovy.
  • I have three different Domain Objects which use these datasources.
  • In my Controller, I call a method from a service in which I save the objects a, b ​​and c. I need that if an error occurs, none of them is saved.

So:

DataSource.groovy

environments {
    development {
        dataSource {
            [...]
        }
        dataSource_b {
            [...]
        }
        dataSource_c {
            [...]
        }
    }
}

ObjectA.groovy

    class ObjectA{

    [...]

    static mapping = {
        //use default datasource
    }

}

ObjectB.groovy

    class ObjectB{

    [...]

    static mapping = {
        datasource 'b'
        [...]
    }

}

ObjectC.groovy

class ObjectC{

    [...]

    static mapping = {
        datasource 'c'
        [...]
    }

}

MyService.groovy

public savingObjects(ObjectA a,ObjectB b,ObjectC c){ 
    a.save()
    b.save()
    c.save()
}

I am using Grails 2.4.3 and I know that methods in services are Transaccional but, for example, if error occurs when I am saving 'c', 'a' and 'b' are saved without any flush (save flush:true).

Any idea to correct this behaviour? I want to avoid using XA datasources.

Thanks!

NachoB
  • 337
  • 4
  • 15
  • 2
    What you are describing is exactly why XA datasources exist. – Joshua Moore Aug 26 '16 at 21:48
  • 1
    Well in that case if you don't want to use XA datsource, then add your own error caching mechanism and rollback changes manually. http://stackoverflow.com/questions/8421631/are-xa-jta-transactions-still-used – Vinay Prajapati Aug 27 '16 at 06:35
  • 1
    Yeah I know that, this is THE purpose of XA datasources. I thought that perhaps Grails could offer another solution. So.. I think that I will add my own error caching mechanism. Thanks for the feedback! – NachoB Aug 30 '16 at 05:35

0 Answers0