开发者

Grails Pessimistic locking with executeUpdate()

开发者 https://www.devze.com 2023-03-08 23:23 出处:网络
I can\'t find any info on how to use pessimistic locking with an executeUpdate() command in grails. Is this possible?

I can't find any info on how to use pessimistic locking with an executeUpdate() command in grails. Is this possible?

[Update] As per Burt's suggestion, this is the resulting query I have. Note that I have used the deprecated UPGRADE instead PESSIMISTIC_WRITE since this option is not开发者_开发问答 there.

def session = sessionFactory.currentSession
def query = session.createQuery("UPDATE SensorProcessed s SET s.batch=:batch WHERE s.device.id=:deviceId AND s.batch.id=:batchId AND s.sensor.id=:sensorId AND s.rollupKey=:rollupKey")
    query.setLockMode ("s", LockMode.UPGRADE)
    query.setParameter("batch",ignored)
    query.setParameter("deviceId",device.id)
    query.setParameter("batchId",batch.id)
    query.setParameter("sensorId",sensor.id)
    query.setParameter("rollupKey",rollupKey)
    def updatedRows = query.executeUpdate()

Thanks,

Abraham.


It's not supported, but it's simple enough to get access to the Hibernate API and use it there. Add a dependency injection for the session factory to your service or controller:

def sessionFactory

and then in your method replace the executeUpdate call with a regular Hibernate update:

def session = sessionFactory.currentSession
def query = session.createQuery("your HQL")
query.setLockMode alias, LockMode.PESSIMISTIC_WRITE
// set query variable values
query.executeUpdate()


I just saw this:

http://grails.org/doc/latest/guide/single.html#5.3.5 Pessimistic and Optimistic Locking

note that you will have to make sure your DB supports Pessimistic locking.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号