<!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">JBoss 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;">
    EJB3/JPA 2.0 support for AS7
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="http://community.jboss.org/people/smarlow%40redhat.com">Scott Marlow</a> in <i>JBoss AS7 Development</i> - <a href="http://community.jboss.org/docs/DOC-16271">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p style="margin-bottom: 0in;">This is about the AS7 JPA layer and related concerns.&#160; Its a rough draft, mostly points copied from the JPA 2.0 spec.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><ol><li><p style="margin-bottom: 0in;">Container-managed persistence context</p><ol><li><p style="margin-bottom: 0in;">Transaction scope (JPA 7.6.1)</p><ol><li><p style="margin-bottom: 0in;">Transaction active invocation</p><ol><li><p style="margin-bottom: 0in;">Persistence context is created if none already associated with transaction</p></li><li><p style="margin-bottom: 0in;">Created persistence context ends when transaction ends</p></li><li><p style="margin-bottom: 0in;">An extended persistence context can also be used, which survives when transaction ends.</p></li><li>Managed entities are detached at transaction end time.</li></ol></li><li><p style="margin-bottom: 0in;">No transaction invocation</p><ol><li><p style="margin-bottom: 0in;">Managed entities are detached at end of <span style="text-decoration: line-through;">bean method call</span> EntityManager call.</p></li></ol></li></ol></li><li><p style="margin-bottom: 0in;">Extended scope (JPA 7.6.2)</p><ol><li><p style="margin-bottom: 0in;">Only supported for stateful session beans.</p></li><li><p style="margin-bottom: 0in;">Entity modifications can be made outside of a transaction and are applied on the next joined transaction (anywhere on the cluster).</p></li><li><p style="margin-bottom: 0in;">Extended persistence context (XPC) is created when stateful bean that depends on a XPC is created.</p></li><li>Managed entities continue to be managed at transaction commit time.</li><li>Managed entities are detached at transaction rollback time.</li><li><p style="margin-bottom: 0in;">XPC is closed when dependent session bean(s) are closed (via @remove method).</p></li><li><p style="margin-bottom: 0in;">Inheritance</p><ol><li><p style="margin-bottom: 0in;">Stateful beans that create other (local) stateful beans, share the same XPC.</p></li></ol></li></ol></li><li><p style="margin-bottom: 0in;">Persistence context propagation (JPA 7.6.3)</p><ol><li><p style="margin-bottom: 0in;">A persistence context will be propagated to multiple entity managers (instances) within the same local transaction.</p></li><li><p style="margin-bottom: 0in;">Remote invocations do not&#160; propagate the persistence context.</p></li></ol></li><li><p style="margin-bottom: 0in;">Persistence context propagation requirements for component invocations (JPA 7.6.3.1)</p><ol><li><p style="margin-bottom: 0in;">no propagation if component is invoked without a JTA transaction or without the JTA transaction propagating:</p><ol><li><p style="margin-bottom: 0in;">Transaction scoped entity manager used within the invoked component, will create a new persistence context.</p></li><li><p style="margin-bottom: 0in;">Extended scoped entity manager used within the invoked component, will use the XPC already bound to the (invoked stateful) bean.</p></li><li><p style="margin-bottom: 0in;">If the entity manager is invoked within a JTA transaction, the persistence context is bound to the JTA transaction.</p></li></ol></li><li><p style="margin-bottom: 0in;">If a component is invoked and the JTA transaction is propagated into the component:</p><ol><li><p style="margin-bottom: 0in;">If (SFSB) component has a XPC and the transaction already has a different persistence context associated with it, an EJBException is thrown by the container.</p></li><li><p style="margin-bottom: 0in;">If a persistence context is bound to the JTA transaction, it is propagated into any entity managers used in the invocation.</p></li></ol></li></ol></li></ol></li><li><p style="margin-bottom: 0in;">Container requirements review</p><ol><li><p style="margin-bottom: 0in;">Application-managed persistence context (JPA 7.8.1)</p><ol><li><p style="margin-bottom: 0in;">Container needs to inject entity manager factory into jndi for application use.</p></li><li><p style="margin-bottom: 0in;">Must use PersistenceProvider.createContainerEntityManagerFactory method for 3<sup>rd</sup> party support.&#160; Internal APIs are fine for our persistence provider.</p></li><li><p style="margin-bottom: 0in;">Must use EntityManagerFactory.close method to close the entity manager factory prior to shutdown (again for 3<sup>rd</sup> party support).</p></li></ol></li><li><p style="margin-bottom: 0in;">Container-managed persistence context (JPA 7.9.1)</p><ol><li><p style="margin-bottom: 0in;">For 3<sup>rd</sup> party support, use EntityManagerFactory.createEntityManager .&#160; Consider using&#160; for our persistence provider as well.</p></li><li><p style="margin-bottom: 0in;">May pass (<code class="western"><span style="font-size: 10pt;"><a class="jive-link-external-small" href="http://download.oracle.com/javaee/6/api/javax/persistence/PersistenceProperty.html">PersistenceProperty</a>[]</span></code>) PersistenceContext.properties to EntityManagerFactory.createEntityManager(Map).</p></li><li><p style="margin-bottom: 0in;">Container EM wrapper could implement some EntityManager.unwrap(Class&lt;T&gt; cls) calls but should default to underlying EM for unhandled classes.</p></li><li><p>If invoked without a JTA transaction and a transaction scoped persistence context is used, will throw TransactionRequiredException for any calls to entity manager remove/merge/persist/refresh. </p></li></ol></li></ol></li><li><p style="margin-bottom: 0in;">Deployment</p><ol><li><p style="margin-bottom: 0in;">Extract persistence metadata from persistence.xml</p></li><li><p style="margin-bottom: 0in;">Determine the PersistenceProvider classname for each persistence unit (PU).&#160; The default class is currently org.hibernate.ejb.HibernatePersistence.</p></li><li><p style="margin-bottom: 0in;">Invoke the PersistenceProvider.createContainerEntityManagerFactory(PersistenceUnitInfo, Map) to create the EMF that will be used to create all EntityManager's for the PU.&#160; The properties read from the PU are passed as the second parameter.</p></li><li><p style="margin-bottom: 0in;">Also pass the &ldquo;javax.persistence.validation.factory &#8221; property if validation mode is not set to NONE.&#160; The validator factory value appears to be org.hibernate.validator.engine.ValidatorFactoryImpl.&#160; Consult ValidatorFactoryProvider which is currently used to bootstrap the validator factory in AS6.</p></li><li><p style="margin-bottom: 0in;">The PU classes shouldn't be loaded until after the EMF is created.</p></li><li><p style="margin-bottom: 0in;">The EntityManagerFactory is closed at undeploy time, which also closes all EntityManager's opened by each factory.</p></li><li><p style="margin-bottom: 0in;">Switchboard changes for PU + PC???</p><ol><li>Determine the available persistence providers following JPA 9.3&#160; <ol><li><a class="jive-link-external-small" href="http://download.oracle.com/javaee/6/api/javax/persistence/spi/PersistenceProviderResolver.html">PersistenceProviderResolver</a></li><li><a class="jive-link-external-small" href="http://download.oracle.com/javaee/6/api/javax/persistence/spi/PersistenceProviderResolverHolder.html">PersistenceProviderResolverHolder</a></li></ol></li></ol></li></ol><ol></ol></li><li><p style="margin-bottom: 0in;">Clustering</p><ol><li><p style="margin-bottom: 0in;">Determine impact on <a class="jive-link-wiki-small" href="http://community.jboss.org/docs/DOC-13822">http://community.jboss.org/wiki/OptimizingentityandXPCreplication</a> which may need to be tweaked for clustering other persistence providers.&#160; Judging by the jira status, this optimization is not in place yet (although Hibernate persistence provider HHH-2762 is done).</p></li><li><p style="margin-bottom: 0in;">Determine impact on <a class="jive-link-wiki-small" href="http://community.jboss.org/docs/DOC-9565">http://community.jboss.org/wiki/DevEJB3NewSFSBCache</a> (consider support for other persistence providers).</p></li><li>Other changes?</li></ol></li><li><p style="margin-bottom: 0in;">Weld integration&#160; (JpaInjectionServices implementation is wired in at boot time).</p></li><li>Determine Hibernate release to target integration with.</li><li><p style="margin-bottom: 0in;">EJB Container interaction</p><ol><li>TBD</li></ol></li><li>Desires<ol><li>Maintainable code (a ten minute fix will take ten minutes <span> :) </span></li><li>Would like to do a better job of showing what is going on at the EM level (e.g. logging the EM units of work grouped by transaction).</li><li>Consider any EM (container) extensions that make it easier to measure performance (perhaps based on Hibernate statistics).&#160; Its always nice to see how long each transaction took and the units of work within the transaction.&#160; It would be nice if we could do this with any persistence provider (for performance comparison).</li><li>Deal with the Hibernate persistence provider via the same way as we would interact with other persistence providers (via PersistenceProvider mentioned above).</li><li>It should be easy to switch to a new persistence provider.</li><li>If an application bundles a persistence provider that is referenced from their persistence unit(s), use the bundled persistence provider (even if its a different version of a JPA 2.0 provider that is also available to all applications).&#160; This should handle bundling a different version of Hibernate.</li><li>Peace.</li></ol></li><li>Questions<ol><li>The JPA specification section 7.9.1 (Container Responsibilities) implies that only one (container managed) entity manager will be associated with the JTA transaction.&#160; However, what if someone injects multiple persistence contexts (different persistent units) in separate entity managers for the same bean.&#160; Should that be considered an application error or allowed behaviour (e.g. each separate entity manager is joined to the JTA transaction). <strong>ANSWER:&#160; we will allow it and design for it.</strong></li><li>For a few different cases, we will need to know which EntityManager was injected into a component.&#160; One such case is for XPC inheritance.&#160; For example, SFSB1&#160; (with extended persistence context) gets injected into a web component and SFSB2 (with extended persistence context) gets injected into SFSB1.&#160; SFSB2 should inherit the XPC from SFSB1.&#160; Open question is how we want to maintain the bookkeeping for this.&#160; The JPA layer could expose a SPI like <span style="color: #008000;">BeanToEntityManager{ List&lt;EntityManager&gt; getEntityManagers(); }</span> that can be passed in on other SPI calls (basically depending on the EJB container to track injected EntityMangers via other magic).&#160; Another solution would be to track the mapping in the JPA layer (might be a pretty big map with a lot of weak references that need to be considerate of GC/undeploy concerns).&#160; Or perhaps there will some other solution that will appear for this.&#160; <span> :) </span></li><li>What about injecting a Hibernate session or session factory as mentioned <a class="jive-link-external-small" href="http://bill.burkecentral.com/2007/07/06/co-existence-with-hibernate-jpa-and-ejb3/">here</a> and <a class="jive-link-external-small" href="http://docs.jboss.org/ejb3/docs/reference/build/reference/en/html/hibernate.html">here</a>.&#160; Do we want to carry this into AS7?&#160; Is there another way to do this that we would want to implement for AS7? [Emmanuel] I think this is valuable and quite natural. An alternative approach would be to let all injection logic be handle by Seam 3 which does Session/SessionFactory injection already (it also inject FullTextEntityManager and FullTextSession form Hibernate Search for that matter).</li><li>Should the jboss-jpa project continue to have its own release cycle or should it live in the AS7 source repo?&#160; If anything outside AS7 wants to consume the JPA integration project, then jboss-jpa will need to have its own release cycle.&#160; One obvious case is the EJB container, if that continues lives in its own project, jboss-jpa will have to follow suit.&#160; I'm not aware of any other projects that desire that.</li><li>For the JPA EE container integration, should we integrate with a component level invocation context or purely with the containers directly?&#160; At this point, I'm assuming direct container integration but we might be able to refactor some of that to be more generic.&#160; This will make more sense soon.&#160; <a class="jive-link-external-small" href="http://docs.jboss.org/ejb3/docs/reference/build/reference/en/html/entityconfig.html"><span> :) </span></a></li><li><a class="jive-link-external-small" href="http://docs.jboss.org/ejb3/docs/reference/build/reference/en/html/entityconfig.html">Keep support for jboss.entity.manager.* properties?</a>&#160; How about securing entity beans from same doc (4.5)?</li><li><span style="color: #551a8b;">What about existing code that might attempt casting a javax.persistence.Query to a Hibernate QueryImpl class (<a class="jive-link-external-small" href="http://http://docs.jboss.org/ejb3/docs/reference/build/reference/en/html/hibernate.html">described here</a>).</span></li></ol></li></ol><p>Illustration of how a stateful session bean (SFSB) named SFSB_XPC1 interacts with a stateless session bean (SLSB) named SLSB_PC2.&#160; Note that the extended persistence context (XPC) is injected into SFSB_XPC1 at bean creation time.&#160; Also note that the SFSB_XPC1 persistence context is bound to the JTA transaction and will be injected into the SLSB_PC2 by the EJB container as part of each invocation.&#160; The XPC1 will retain changes made by each invocation to SLSB_PC2 until the transaction commits.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><table border="1" cellpadding="3" cellspacing="0" class="jiveBorder" style="width: 70%; border: 1px solid #000000;"><tbody><tr><th align="center" style="background-color: #6690bc;; border: 1px solid #000000;background-color: #6690bc;" valign="middle"><span style="color: #ffffff;"><strong>Action</strong></span></th><th align="center" style="background-color: #6690bc;; border: 1px solid #000000;background-color: #6690bc;" valign="middle"><span style="color: #ffffff;"><strong>Transaction</strong></span></th><th align="center" style="background-color: #6690bc;; border: 1px solid #000000;background-color: #6690bc;" valign="middle"><span style="color: #ffffff;"><strong>PersistenceContext<br/></strong></span></th></tr><tr><td style="border: 1px solid #000000;">SFSB_XPC1 created</td><td style="border: 1px solid #000000;"><br/></td><td style="border: 1px solid #000000;">XPC1 injected into SFSB_XPC1</td></tr><tr><td style="border: 1px solid #000000;">SFSB_XPC1.updateInventory</td><td style="border: 1px solid #000000;">T1 started</td><td style="border: 1px solid #000000;">XPC1 bound to T1</td></tr><tr><td style="border: 1px solid #000000;">SFSB_XPC1 calls SLSB_PC2</td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;"><br/></td></tr><tr><td style="border: 1px solid #000000;">SLSB_PC2.dowork</td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;">XPC1 injected into SLSB_PC2 EM instance</td></tr><tr><td style="border: 1px solid #000000;">SLSB_PC2 loads entity item1</td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;">item1 is loaded</td></tr><tr><td style="border: 1px solid #000000;">SLSB_PC2 persists new entity order1 </td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;">order1 will be saved at commit time</td></tr><tr><td style="border: 1px solid #000000;">SLSB_PC2 returns</td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;"><br/></td></tr><tr><td style="border: 1px solid #000000;">SFSB_XPC1 calls SLB_PC2 again</td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;"><br/></td></tr><tr><td style="border: 1px solid #000000;">SLSB_PC2.morework</td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;">XPC1 injected into SLSB_PC2 EM instance</td></tr><tr><td style="border: 1px solid #000000;">SLSB_PC2 loads entity order1</td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;">order1 is returned from XPC1</td></tr><tr><td style="border: 1px solid #000000;">SLSB_PC2 calls order1.increase(4)</td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;">change made in XPC1 held entity</td></tr><tr><td style="border: 1px solid #000000;">SLSB_PC2 returns</td><td style="border: 1px solid #000000;">T1</td><td style="border: 1px solid #000000;"><br/></td></tr><tr><td style="border: 1px solid #000000;">SFSB_XPC1 returns</td><td style="border: 1px solid #000000;">T1 commit</td><td style="border: 1px solid #000000;">XPC1 changes are saved, item1 + order1 remain in XPC1</td></tr><tr><td style="border: 1px solid #000000;">SFSB_XPC1 removed</td><td style="border: 1px solid #000000;"><br/></td><td style="border: 1px solid #000000;">XPC1 is closed</td></tr></tbody></table></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="http://community.jboss.org/docs/DOC-16271">going to Community</a></p>

        <p style="margin: 0;">Create a new document in JBoss AS7 Development at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2225">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


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

</div>

</body>
</html>