Quantcast

Transactional ServiceMethod and plain SQL

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Transactional ServiceMethod and plain SQL

GrailsUser001
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hi all,

im just stuck and searched the web / forum but cant solve my problem.
I have a transaction problem combining plain sql and GORM features.

First of all i have a Service with static transactional = true.

Inside the method "save" i need to look the table and aquire the max id + 1 and afterwards save my domain class with save(flush: true)

class DomainService {
def sessionFactory

 def save(DomainObject do){
     def sql = new Sql(sessionFactory.getCurrentSession().connection())
     sql.execute("begin lock table DOMAINOBECT  in exclusive mode wait 30 select @@error end")

    // select id and add 1
     domainObject.id = newId
     domainObject.save(flush: true)
 }
}

Somehow the first execute statement for the lock fails, because the db mentions that there is no open transcation.
As the comment here (http://jira.grails.org/browse/GRAILS-7056) it should be possbile to use GORM / SQL together in one transaction.
How can i archieve that the first sql statements and the domainObject.save() are encapsulated in the same transaction?

I tried something like sql.withTransaction {} and it seems to work, but it looks very crappy.

Thx alot and best regards!
Loading...