简体   繁体   中英

Grails 'withTransaction' with alternate dataSource

Trying to figure out how to open a transaction with an alternate dataSource in grails. I have the following dataSources defined...

dataSource {
    dbCreate = "update"
    url = "jdbc:h2:mem:testDb;MVCC=TRUE"
}

dataSource_ALT {
   dbCreate = "update"
   url = "jdbc:h2:mem:altDb;MVCC=TRUE"
}

I'm able to do this with the default dataSource...

Foo.withTransaction { status ->
    //...
}

But when I try and do it with the ALT dataSource, I get an exception - 'No transactionManager bean configured'...

Foo.ALT.withTransaction { status ->
   //...
}

Is there a way to start a transaction using a different dataSource? I've done some digging around and haven't been able to find much.

Thanks!

@Raphael Your suggestion does work. I was able to go to the next step and get withTransaction working by assigning a transaction manager. They seem to be created, just not attached. They exist as Spring beans named something like transactionManager_ALT (in this example). Not sure what actually creates them, and why they aren't attached to the GormStaticApi.

ie here you'd do:

Foo["ALT"].transactionManager = transactionManager_ALT

Probably will try to pick one domain class and assign all the different transaction managers, see if that works, then use that one domain class everywhere to start transactions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM