QWANtify is excited to announce, effective May 1, 2010, their purchase by Safe Bridge Solutions, Inc. of Madison, WI. Read More »

Hibernate and Drools

Today we deployed an application to our QA server and found we were getting an unusual error with hibernate. The error was:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed

After debugging it for a while, I figured out it was occurring when the Drools rules engine was trying to access a lazily loaded hibernate field on one of the hibernate objects that was passed into it. At first I thought maybe it was some kind of multi-threading problem since that might cause the session to be unavailable in a separate thread from the main thread. After a bit a googling I found that answer. It turns out Drools uses shadow facts that results in the hibernate objects being copied in memory. Well the hibernate session for the objects cannot be copied so when the copies are used it appears the session is not set or has been closed. To fix the problem I disabled the shadow proxies with the following code:

RuleBaseConfiguration conf = new RuleBaseConfiguration();
conf.setShadowProxy(false);
RuleBase ruleBase = RuleBaseFactory.newRuleBase(conf);

Shadow facts should only be disabled, however, if you follow the following rules.

  • Your fact classes are immutable.
  • Fact changes in the rules are done only in modify() blocks.
  • Fact changes in your application are only done in modifyRetract() or modifyInsert.

You can find more information about shadow facts at this site: http://blog.athico.com/2008/02/shadow-facts-what-you-always-wanted-to.html.

Filed in: Team Member Blog Comments (0)

Comments

There are no comments for this entry.

Commenting is not available in this section entry.