<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Injected hibernate SessionFactory is not used by jBPM 4.3?
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/helmers">Guido Helmers</a> in <i>jBPM Development</i> - <a href="http://community.jboss.org/message/536777#536777">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hi there,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I'm using jBPM 4.3 and I want jBPM to use my own SessionFactoryImpl object, instead of creating its own. I'm doing the following to inject my SessionFactory into jBPM:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre>new Configuration().setResource("jbpm/jbpm.cfg.xml").setHibernateSessionFactory(hibernateSessionFactory).buildProcessEngine();</pre><div id=":yh"> </div><div> </div><div> </div><div>But jBPM ignores what I pass in, and creates its own SessionFactory.<br/> <br/> Does anyone know what I should do to make this work? Is there an additional jBPM configuration property that I need to set? Could it be a bug in jBPM?<br/> <br/> I've attached a Maven project that you can use to debug this problem. I'll explain how you can debug it to see what's going wrong. If you want to know about the project structure (not really required to debug the jbpm problem), read the section at the end of this mail.</div><div> </div><div>Cheers,</div><div>Guido</div><div> </div><div><em> Note: We're using Guice for dependency injection; dependencies are configured in the *Module classes. Warp-persist is used for transaction management in Guice. I'm not sure if guice and warp-persist are in the central maven repos, so I've put those libraries in the lib folder.</em></div><h3><br/> How to debug</h3><p>Import the maven project in your IDE.</p><div id=":yh">(In Eclipse: mvn clean eclipse:clean eclipse:eclipse -Declipse.useProjectReferences=true -DdownloadSources=true -DdownloadJavadocs=true<br/> You may have to install the guice libraries in your local maven repository. Do a "mvn clean compile" to check whether the project is complete).<br/> <br/> Put a break point on the following lines / methods (basically everything in the code base related to a hibernate SessionFactory):<br/><ol><li>&#160; My code:<ol><li> JbpmLayerModule.provideProcessEngine()</li></ol></li><li>&#160; jBPM code:<ol><li>&#160; ConfigurationImpl.setHibernateSessionFactory(Object)</li><li>&#160; HibernateSessionDescriptor.construct(WireContext)</li><li>&#160; HibernateSessionFactoryDescriptor.construct(wireContext)</li></ol></li></ol> <br/> Now run IssueServiceIntegrationTest.testCreate() in debug mode. On the above breakpoints, you'll see information similar to the following:<br/> <strong>[1.1]</strong> hibernateSessionFactory=org.hibernate.impl.SessionFactoryImpl@5c6c0a(id=92)<br/> <strong>[2.1]</strong> processEngineWireContext=WireContext "process-engine"(id=112)<br/> <strong>[2.2]</strong> [Indirectly called by CheckDbCmd.execute()] wireContext="transaction"(id=166)<br/>It's doing this: sessionFactory = environment.get(SessionFactory.class);<br/> <br/>When I look in the 'environment' variable, I see there are 2 contexts in there: A 'transaction' context and a 'process-engine' context. The 'transaction' context contains ZERO SessionFactory definitions. The 'process-engine' context contains TWO SessionFactory definitions:<br/><ul><li>org.hibernate.impl.SessionFactory-&gt;ProvidedObjectDescriptor (with .providedObject=the one I passed in)</li><li>org.hibernate.SessionFactory-&gt;HibernateSessionFactoryDescriptor (with .name="org.hibernate.SessionFactory", all other fields null).</li></ul></div><div id=":yh">So my SessionFactory is available in the 'process-engine' context, under key "org.hibernate.impl.SessionFactory"...<br/>==&gt; Problem: My SessionFactory is stored under key '...SessionFactoryImpl', not under '...SessionFactory'!</div><div> </div><div>That's why we end up in the following HibernateSessionFactoryDescriptor:<br/> <strong>[2.3]</strong> It's doing this:<br/>configuration = wireContext.get(Configuration.class);<br/>...<br/>SessionFactory sessionFactory = configuration.buildSessionFactory();</div><div> </div><div>In other words, loading a new hibernate Configuration from scratch, and a new SessionFactory.</div><div><div> </div><h3>Questions</h3><div>It seems that in [3] (that is HibernateSessionDescriptor.construct()) something's going wrong.<br/><ul><li>How can I have jBPM use the SessionFactory instance that was stored in the 'process-engine' WireContext, instead of using its own default mechanism to load hibernate configuration and construct a session factory?</li></ul></div><div><ul><li> Did I forget some configuration properties in the jbpm.cfg.xml?* Is it a bug in jBPM?</li><li> Isn't it scary that the 'process-engine' wireContext contain a mapping for 'SessionContext' and one for 'SessionContextImpl'. Depending on whether you're requesting the interface or implementation, you're obtaining a different session factory (I guess that's what this whole post is about...?)</li></ul></div><div><br/> I do see that in HibernateSessionDescriptor, there's a 'factoryName' property. If this property is set, it will lookup the SessionFactory in the 'transaction' wireContext under that 'factoryName'. Two questions:<br/><ol><li>Can I set the 'factoryName' somehow, from a jbpm configuration file? How?</li><li>If so, will it make any difference? Because it will then be looking up the SessionFactory in the 'transaction' wireContext, instead of the 'process-engine' wireContext (in which my SesionFactory is contained).</li></ol></div><div><h3>Background info on attached project</h3><p>The project is called 'jbpmtest'. It's a multimodule maven project, with the following (slightly simplified) dependency graph: [ service -&gt; dao -&gt; test -&gt; domain -&gt; commons ].</p><br/> (About the domain: There's only one class in the domain, called 'Issue'. We store every Issue locally, and in addition an Issue has its own jBPM process instance; This is all managed by the IssueService. But this isn't relevant to the jBPM problem really..)<br/> <br/> The Guice module structure (comparable to Spring's application<br/> context) is as follows:<br/> <br/> ServiceLayerIntegrationTestModule<br/>&#160; |<br/>&#160; |__ TestDataSourceModule<br/>&#160; |&#160;&#160;&#160; |<br/>&#160; |&#160;&#160;&#160; |__ JndiDataSourceCreator &lt;-- Makes a datasource available under Jndi name 'jbpmtestDs'<br/>&#160; |<br/>&#160; |__ DataLayerModule<br/>&#160; |&#160;&#160;&#160; |<br/>&#160; |&#160;&#160;&#160; |__ bind(Configuration).to(HibernateConfigurationProvider) (*)<br/>&#160; |&#160;&#160;&#160; |<br/>&#160; |&#160;&#160;&#160; |__ PersistenceModule<br/>&#160; |&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |<br/>&#160; |&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |__ HibernatePersistenceModule (**)<br/>&#160; |<br/>&#160; |__ ServiceLayerModule<br/>&#160;&#160;&#160;&#160;&#160; |<br/>&#160;&#160;&#160;&#160;&#160; |__ JbpmLayerModule (***)<br/> <br/> (*) Reads hibernate.cfg.xml and combines it with some other (programmatic) properties into a hibernate Configuration.<br/> (**) Created with "PersistenceService.usingHibernate().across(UnitOfWork.TRANSACTION).buildModule()",<br/> using (*).<br/> (***) The wiring of jBPM beans is done in the JbpmLayerModule. It constructs a ProcessEngine singleton, which in turn is used to provide<br/> the actual jBPM services (like RepositoryService, ExecutionService, etc.).<br/> <br/> jBPM's ProcessEngine is created like this:<br/>&#160; <pre>&#160;&#160;&#160; @Provides<br/>&#160;&#160;&#160; @Singleton<br/>&#160;&#160;&#160; ProcessEngine provideProcessEngine(SessionFactory hibernateSessionFactory) {<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return new Configuration().setResource("jbpm/jbpm.cfg.xml")<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .setHibernateSessionFactory(hibernateSessionFactory).buildProcessEngine();<br/>&#160;&#160;&#160; }<br/></pre><br/> You can see that org.jbpm.api.Configuration is passed in a hibernateSessionFactory; it's the SessionFactory created by Guice (somewhere inside the PersistenceService).<br/> <br/> Please note that the whole Guice configuration isn't really relevant here, I just explained it for you to get a better understanding of the test project. The only interesting thing here is that JbpmLayerModule.provideProcessEngine() injects a SessionFactory object (provided by Guice!) into the jBPM Configuration, and that a later stage, jBPM seems to have constructed its own SessionFactory! I was hoping the SessionFactory to be a singleton, and that passing it into the jBPM Configuration causes jBPM to *not* read the hibernate.cfg.xml again and construct a second SessionFactory...</div><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p></div></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/536777#536777">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM Development at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2035">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>