[rules-users] Problems using shadow proxies and Hibernate lazy initialization

Keith Bennett forthwind at gmail.com
Thu Apr 3 10:19:02 EDT 2008


Edson,

Thanks for the reply.  I took a look at your post regarding how to
retract then modify the fact, but I have a different situation.  I
need to modify the fact via a function call.  This is because inside
the function I have access to Spring-managed services that I use to
retrieve domain objects from the data access layer that I can then set
(i.e., modify) on the domain object (or fact, as referred to in
Drools).  So, right now, my consequence looks like the following:

drools.modifyRetract($loan);
functionNameThatModifiesLoan($loan);
drools.modifyInsert($loan);

Does this, in effect, do the same thing that your posting shows how to
do.  In other words, can I retract the fact, modify it, then insert it
back into the Drools engine this way?

BTW, I will be more than happy to open a JIRA.  I just want to make
sure that what I am doing in my consequence is correct first since the
modifyRetract() and modifyAssert(), which will be changed to
modifyInsert() via the JIRA, is how the user guide currently describes
how this should be done, not the way your posting describes.

On 4/2/08, Edson Tirelli <tirelli at post.com> wrote:
>
>    It seems the docs need to be updated. Would you plz open a JIRA to fix
> the place you are seeing this info?
>
>    For more current info, take a look at this blog post, specially the end
> on "how to disable shadow facts":
>
> http://blog.athico.com/2008/02/shadow-facts-what-you-always-wanted-to.html
>
>    []s
>    Edson
>
> 2008/4/2, Keith Bennett <forthwind at gmail.com>:
> >
> > I am currently using Drools to validate an extensive object graph.
> > When I validate a transient object graph (i.e., not persisted via
> > Hibernate) inside of a JUnit test method, validation occurs
> > successfully.  However, when I retrieve the same object graph from
> > Hibernate and subsequently validate it, I get the stack trace that
> > I've included below, but only when I use shadow proxies.  When I turn
> > shadow proxies off, validation runs successfully against the persisted
> > object graph again.  Of course, I'm utilizing lazy association
> > fetching to improve performance and want to continue utilizing it.
> > Has anyone run into this same problem, and if so, were you able to
> > work around it without turning shadow proxies off?  Before you ask, my
> > Hibernate session is open before and after I invoke Drools to perform
> > validation in the same unit of work (i.e., transaction), so it's not
> > an issue of not having an active Hibernate session available (I've
> > verified this through testing).
> >
> > One more thing.  The user guide says that if shadow proxies are turned
> > off that modifyRetract() should be called before an object is modified
> > and modifyAssert() should be called after it is modifed.  First, what
> > is the method parameter to these two overloaded methods, Object or
> > FactHandle (both exist)?  Second, I don't see modifyAssert() available
> > in the library, just modifyInsert().  Should the documentation change
> > to read modifyInsert() instead of modifyAssert()?  If so, again with
> > which parameter?
> >
> > I appreciate any insight you may have!
> >
> > Here's the stack trace:
> >
> > org.hibernate.LazyInitializationException: failed to
> lazily initialize
> > a collection, no session or session was closed
> >         at
> org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
> >         at
> org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
> >         at
> org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
> >         at
> org.hibernate.collection.PersistentSet.addAll(PersistentSet.java:244)
> >         at
> org.drools.util.ShadowProxyUtils.cloneObject(ShadowProxyUtils.java:76)
> >         at
> com.cbi.domain.loan.indirect.IndirectLoanShadowProxy.getCustomers(Unknown
> > Source)
> >         at
> org.drools.base.com.cbi.domain.loan.indirect.IndirectLoan28409161$getCustomers.getValue(Unknown
> > Source)
> >         at
> org.drools.base.ClassFieldExtractor.getValue(ClassFieldExtractor.java:127)
> >         at
> org.drools.rule.Declaration.getValue(Declaration.java:198)
> >         at
> LoanValidationBusinessRules.Rule_A_Loan_must_have_at_least_one_Customer_0Eval0Invoker.evaluate(Rule_A_Loan_must_have_at_least_one_Customer_0Eval0Invoker.java:12)
> >         at
> org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:72)
> >         at
> org.drools.reteoo.EvalConditionNode.assertTuple(EvalConditionNode.java:145)
> >         at
> org.drools.reteoo.CompositeTupleSinkAdapter.createAndPropagateAssertTuple(CompositeTupleSinkAdapter.java:73)
> >         at
> org.drools.reteoo.LeftInputAdapterNode.assertObject(LeftInputAdapterNode.java:116)
> >         at
> org.drools.reteoo.CompositeObjectSinkAdapter.propagateAssertObject(CompositeObjectSinkAdapter.java:318)
> >         at
> org.drools.reteoo.ObjectTypeNode.assertObject(ObjectTypeNode.java:153)
> >         at
> org.drools.reteoo.Rete.assertObject(Rete.java:175)
> >         at
> org.drools.reteoo.ReteooRuleBase.assertObject(ReteooRuleBase.java:192)
> >         at
> org.drools.reteoo.ReteooWorkingMemory.doInsert(ReteooWorkingMemory.java:71)
> >         at
> org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:909)
> >         at
> org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:881)
> >         at
> org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:682)
> >         at
> com.cbi.service.loan.indirect.impl.DroolsBusinessRulesServiceHelper.executeRules(DroolsBusinessRulesServiceHelper.java:77)
> >         at
> com.cbi.service.loan.indirect.impl.DroolsBusinessRulesServiceHelper.validate(DroolsBusinessRulesServiceHelper.java:66)
> >         at
> com.cbi.service.loan.indirect.impl.DroolsBusinessRulesServiceImpl.validate(DroolsBusinessRulesServiceImpl.java:97)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
> >         at
> org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
> >         at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
> >         at $Proxy4.validate(Unknown Source)
> >         at
> com.cbi.service.loan.indirect.impl.IndirectLoanManagementServiceImpl.submitForBooking(IndirectLoanManagementServiceImpl.java:680)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
> >         at
> org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
> >         at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
> >         at $Proxy6.submitForBooking(Unknown Source)
> >         at
> com.cbi.service.loan.indirect.impl.LoanManagementServiceImplTestCase.testSubmitLoanForBooking(LoanManagementServiceImplTestCase.java:116)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> junit.framework.TestCase.runTest(TestCase.java:164)
> >         at
> junit.framework.TestCase.runBare(TestCase.java:130)
> >         at
> org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
> >         at
> junit.framework.TestResult$1.protect(TestResult.java:106)
> >         at
> junit.framework.TestResult.runProtected(TestResult.java:124)
> >         at
> junit.framework.TestResult.run(TestResult.java:109)
> >         at
> junit.framework.TestCase.run(TestCase.java:120)
> >         at
> junit.framework.TestSuite.runTest(TestSuite.java:230)
> >         at
> junit.framework.TestSuite.run(TestSuite.java:225)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
> >         at
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
> >         at
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
> >         at
> org.apache.maven.surefire.Surefire.run(Surefire.java:177)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
> >         at
> org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
> > 2008-04-02 16:48:32,691 ERROR
> > org.hibernate.LazyInitializationException - failed to
> lazily
> > initialize a collection, no session or session was closed
> > org.hibernate.LazyInitializationException: failed to
> lazily initialize
> > a collection, no session or session was closed
> >         at
> org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
> >         at
> org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
> >         at
> org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
> >         at
> org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:146)
> >         at
> LoanValidationBusinessRules.Rule_A_Loan_must_have_at_least_one_Customer_0.eval0(Rule_A_Loan_must_have_at_least_one_Customer_0.java:38)
> >         at
> LoanValidationBusinessRules.Rule_A_Loan_must_have_at_least_one_Customer_0Eval0Invoker.evaluate(Rule_A_Loan_must_have_at_least_one_Customer_0Eval0Invoker.java:16)
> >         at
> org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:72)
> >         at
> org.drools.reteoo.EvalConditionNode.assertTuple(EvalConditionNode.java:145)
> >         at
> org.drools.reteoo.CompositeTupleSinkAdapter.createAndPropagateAssertTuple(CompositeTupleSinkAdapter.java:73)
> >         at
> org.drools.reteoo.LeftInputAdapterNode.assertObject(LeftInputAdapterNode.java:116)
> >         at
> org.drools.reteoo.CompositeObjectSinkAdapter.propagateAssertObject(CompositeObjectSinkAdapter.java:318)
> >         at
> org.drools.reteoo.ObjectTypeNode.assertObject(ObjectTypeNode.java:153)
> >         at
> org.drools.reteoo.Rete.assertObject(Rete.java:175)
> >         at
> org.drools.reteoo.ReteooRuleBase.assertObject(ReteooRuleBase.java:192)
> >         at
> org.drools.reteoo.ReteooWorkingMemory.doInsert(ReteooWorkingMemory.java:71)
> >         at
> org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:909)
> >         at
> org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:881)
> >         at
> org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:682)
> >         at
> com.cbi.service.loan.indirect.impl.DroolsBusinessRulesServiceHelper.executeRules(DroolsBusinessRulesServiceHelper.java:77)
> >         at
> com.cbi.service.loan.indirect.impl.DroolsBusinessRulesServiceHelper.validate(DroolsBusinessRulesServiceHelper.java:66)
> >         at
> com.cbi.service.loan.indirect.impl.DroolsBusinessRulesServiceImpl.validate(DroolsBusinessRulesServiceImpl.java:97)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
> >         at
> org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
> >         at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
> >         at $Proxy4.validate(Unknown Source)
> >         at
> com.cbi.service.loan.indirect.impl.IndirectLoanManagementServiceImpl.submitForBooking(IndirectLoanManagementServiceImpl.java:680)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
> >         at
> org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
> >         at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
> >         at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
> >         at $Proxy6.submitForBooking(Unknown Source)
> >         at
> com.cbi.service.loan.indirect.impl.LoanManagementServiceImplTestCase.testSubmitLoanForBooking(LoanManagementServiceImplTestCase.java:116)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> junit.framework.TestCase.runTest(TestCase.java:164)
> >         at
> junit.framework.TestCase.runBare(TestCase.java:130)
> >         at
> org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
> >         at
> junit.framework.TestResult$1.protect(TestResult.java:106)
> >         at
> junit.framework.TestResult.runProtected(TestResult.java:124)
> >         at
> junit.framework.TestResult.run(TestResult.java:109)
> >         at
> junit.framework.TestCase.run(TestCase.java:120)
> >         at
> junit.framework.TestSuite.runTest(TestSuite.java:230)
> >         at
> junit.framework.TestSuite.run(TestSuite.java:225)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
> >         at
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
> >         at
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
> >         at
> org.apache.maven.surefire.Surefire.run(Surefire.java:177)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >         at
> java.lang.reflect.Method.invoke(Method.java:585)
> >         at
> org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
> >         at
> org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
> > _______________________________________________
> > rules-users mailing list
> > rules-users at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/rules-users
> >
>
>
>
> --
>   Edson Tirelli
>   JBoss Drools Core Development
>   Office: +55 11 3529-6000
>   Mobile: +55 11 9287-5646
>   JBoss, a division of Red Hat @ www.jboss.com
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>



More information about the rules-users mailing list