[{"data":1,"prerenderedAt":105},["ShallowReactive",2],{"post:hibernate-logger-concurrent-exception":3},{"id":4,"title":5,"body":6,"date":91,"description":12,"draft":92,"extension":93,"meta":94,"navigation":95,"path":96,"seo":97,"stem":98,"tags":99,"__hash__":104},"posts\u002Fposts\u002Fhibernate-logger-concurrent-exception.md","Resolving Concurrent Exceptions in Hibernate Logger",{"type":7,"value":8,"toc":88},"minimark",[9,13,24,30,33,39,50,60,66,77],[10,11,12],"p",{},"Recently while working on a Hibernate ORM project, I ran into an interesting issue when an entity with relationships is saved inside a transaction.",[14,15,20],"pre",{"className":16,"code":18,"language":19},[17],"language-text","    transaction{\n      var myEntity = entityLoadByPK( \"Person\", theEntityID );\n    \n      myEntity.setAddress( otherEntity );\n      myEntity.save();\n    \n      \u002F\u002F throws \"ConcurrentModificationException\" on transaction end.\n    }\n","text",[21,22,18],"code",{"__ignoreMap":23},"",[10,25,26],{},[27,28,29],"em",{},"Note this is not a complete test case - simply an example of when the issue occurs.",[10,31,32],{},"When the transaction completes, Lucee tries to flush the entity modifications, but this fails with the following error:",[14,34,37],{"className":35,"code":36,"language":19},[17],"    lucee.runtime.exp.NativeException: java.util.ConcurrentModificationException\n    at java.base\u002Fjava.util.HashMap$HashIterator.nextNode(HashMap.java:1493)\n    at java.base\u002Fjava.util.HashMap$EntryIterator.next(HashMap.java:1526)\n    at java.base\u002Fjava.util.HashMap$EntryIterator.next(HashMap.java:1524)\n    at org.hibernate.internal.util.EntityPrinter.toString(EntityPrinter.java:112)\n    at org.hibernate.event.internal.AbstractFlushingEventListener.logFlushResults(AbstractFlushingEventListener.java:128)\n    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:104)\n    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)\n    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:93)\n    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1362)\n    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1349)\n    at org.lucee.extension.orm.hibernate.HibernateORMTransaction.end(HibernateORMTransaction.java:57)\n    at lucee.runtime.orm.ORMConnection.setAutoCommit(ORMConnection.java:213)\n    at lucee.runtime.orm.ORMDatasourceConnection.setAutoCommit(ORMDatasourceConnection.java:336)\n    at lucee.runtime.db.DatasourceManagerImpl.end(DatasourceManagerImpl.java:350)\n    at lucee.runtime.db.DatasourceManagerImpl.end(DatasourceManagerImpl.java:330)\n    at lucee.runtime.tag.Transaction.doFinally(Transaction.java:160)\n",[21,38,36],{"__ignoreMap":23},[10,40,41,42,45,46,49],{},"It turns out this issue is related to the Hibernate debug logger. In Lucee, Hibernate's log4j logger is set to a ",[21,43,44],{},"DEBUG"," level by default. That's fine, but it seems the logger actually ",[27,47,48],{},"mutates"," the logged entities during logging. Thus we get a concurrent modification exception.",[10,51,52,53,55,56,59],{},"To resolve this, all we have to do is de-escalate the logging level from ",[21,54,44],{}," to ",[21,57,58],{},"WARN",".",[14,61,64],{"className":62,"code":63,"language":19},[17],"    \u002F**\n     * Resolves Hibernate ConcurrentModificationException when flushing an entity save with one-to-many relationships.\n     * \n     * @see https:\u002F\u002Faccess.redhat.com\u002Fsolutions\u002F29774\n     *\u002F\n    var Logger = createObject( \"java\", \"org.apache.log4j.Logger\" );\n    var level = createObject( \"java\", \"org.apache.log4j.Level\" );\n    var log = Logger.getLogger( \"org.hibernate\" );\n    log.setLevel( level.WARN );\n",[21,65,63],{"__ignoreMap":23},[10,67,68,69,72,73,76],{},"This should be done in the ",[21,70,71],{},"Application.cfc"," before ORM initializes - say, just below the ",[21,74,75],{},"this.ormSettings{}"," configuration block.",[10,78,79,80,87],{},"All thanks to ",[81,82,86],"a",{"href":83,"rel":84},"https:\u002F\u002Ftwitter.com\u002Fjclausen",[85],"nofollow","@jclausen"," for this awesome find and easy fix!",{"title":23,"searchDepth":89,"depth":89,"links":90},2,[],"2021-08-19T16:13:57-04:00",false,"md",{},true,"\u002Fposts\u002Fhibernate-logger-concurrent-exception",{"title":5,"description":12},"posts\u002Fhibernate-logger-concurrent-exception",[100,101,102,103],"cfml","coldfusion","hibernate","orm","VqdexXx9HzDptaHnO-1tpADVbq9C3n6TU1TPP-H6hXA",1790471527455]