<!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.  Its a rough draft, mostly points copied from the JPA 2.0 spec.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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 bean method 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  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.  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 .  Consider using  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<T> cls) calls but should default to underlying EM for unhandled classes.</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).  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.  The properties read from the PU are passed as the second parameter.</p></li><li><p style="margin-bottom: 0in;">Also pass the “javax.persistence.validation.factory ” property if validation mode is not set to NONE.  The validator factory value appears to be org.hibernate.validator.engine.ValidatorFactoryImpl.  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></li><li>Fill in other deployment steps...</li></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.  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  (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).  Its always nice to see how long each transaction took and the units of work within the transaction.  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).  This should handle bundling a different version of Hibernate.</li><li>Peace.</li></ol></li></ol><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Illustration of how a stateful session bean (SFSB) named SFSB_XPC1 interacts with a stateless session bean (SLSB) named SLSB_PC2.  Note that the extended persistence context (XPC) is injected into SFSB_XPC1 at bean creation time.  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.  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;"> </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 returns 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>