grails/hibernate: add pessimistic locking on using creteria -
i tried add pessimistic locking in creteria shown in doc http://grails.org/doc/latest/guide/gorm.html#locking had exception:
"error util.jdbcexceptionreporter - feature not supported: "for update && join"; sql statement: ... org.hibernate.exception.genericjdbcexception: not execute query"
i tried add lock in 2 places:
def parentinstance = parent.createcriteria().get { childs { ideq(childinstance.id) lock true }
and
def parentinstance = parent.createcriteria().get { childs { ideq(childinstance.id) } lock true }
additional question: right way use pessimistic locking association?
thank you
domain
class parent{ static hasmany = [childs:child] } class child{ }
datasource.groovy
datasource { pooled = true driverclassname = "org.h2.driver" username = "sa" password = "" } hibernate { cache.use_second_level_cache = true cache.use_query_cache = false cache.region.factory_class = 'net.sf.ehcache.hibernate.ehcacheregionfactory' } // environment specific settings environments { development { datasource { dbcreate = "update" // 1 of 'create', 'create-drop', 'update', 'validate', '' url = "jdbc:h2:myapp_proddb;mvcc=true" } } test { datasource { dbcreate = "update" url = "jdbc:h2:mem:myapp_testdb;mvcc=true" } } production { datasource { dbcreate = "update" url = "jdbc:h2:myapp_proddb;mvcc=true" pooled = true properties { maxactive = -1 minevictableidletimemillis=1800000 timebetweenevictionrunsmillis=1800000 numtestsperevictionrun=3 testonborrow=true testwhileidle=true testonreturn=true validationquery="select 1" } } } }
depending on how form query, hibernate execute different queries. way query written hibernate perform join - can performance because means joined entities pre-fetched in 1 query. locking bad every joined table have locked , can have quite effect on deep hierarchies. database not allow (i'm not sure if other does).
you have perform query without joins. depending on domain class implementation simple child.parent.id
done without touching database , query becomes simple parent.lock(child.parent.id)
. it's hard without seeing actual domain classes.
what can parent
in non-locking query , call lock()
method on returned instance. suggest take @ excellent article on locking things in gorm more information.
Comments
Post a Comment