Speedo Tutorial - Speedo & J2EE

 FinalLogo.jpg
 FinalLogo.jpg
  1. Application server integration
  2. How to get the PMF?
  3. Transaction demarcation
  4. Data structure creation

Back to the Speedo tutorial home page




Application server integration

Speedo can be integrated into application servers as a resource adapter (JCA Connector SUN specification). Speedo is fully integrated in JOnAS and WebLogic.
To
use Speedo inside an application server, you need to use the rar file provided in the Speedo binary distribution.

JDO code can be integrated into a servlet as shown in the figure below:



JDO code can also be integrated into a session bean or a message driven bean as shown in the figure below:


Top


How to get the PMF?

As said before, the PersistenceManagerFactory is the main element of Speedo: it creates PersistenceManager instances.
One Speedo instance can be associated to one PMF.

In a standalone application, the PMF is created using the speedo.properties file. Properties in this file are loaded to create the PMF instance as described in the code below:

Properties p = new Properties();
p.load(new FileInputStream(porpertiesFileName));
PersistenceMnanagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p);


In a J2EE application, the creation of the PMF is performed using the JNDI name of the Speedo instance. This name can be defined in the ejb-jar xml descriptor of the application. You can also define it in the server dedicated ejb-jar xml descriptor. If it has been defined in both files, the registered name will be the server dedicated ejb-jar xml descriptor one.

Let's say you have a bank application using Speedo. You declare your Speedo resource in the bank.xml deployment descriptor as follows:

<resource-ref>
<res-ref-name>jdo/pmf</res-ref-name>
        <res-type>javax.jdo.PersistenceManagerFactory</res-type>
        <res-auth>Container</res-auth>
</resource-ref>

If you want to deploy your application into the JOnAS server, you define the JNDI resource name in the jonas-bank.xml, the JOnAS dedicated deployment descriptor, as follows:

<jonas-resource>
<res-ref-name>jdo/pmf</res-ref-name>
        <jndi-name>speedo</jndi-name>
</jonas-resource>

In the speedo_for_jonas.rar archive, you'll find two files:

  1. the jonas-ra.xml file in which the JNDI name of the PMF is defined (here, it is "speedo")
  2. the ra-xml file in which the configuration file is defined (here, it is "speedo.properties")

To finish, in your J2EE application you can get the PMF performing a lookup in the JNDI context as follows:

final String PMF_CTX_NAME = "java:comp/env/jdo/pmf";
PersistenceMnanagerFactory pmf = (PersistenceManagerFactory) new InitialContext().lookup(PMF_CTX_NAME);

Top


Transaction demarcation

    In a standalone application, the user has to write the transaction demarcation using the jdo api, as described below:
  
public void makeAgencyPersistent(Agency agency){
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(agency);
pm.currentTransaction().commit();
}
    In a J2EE application, as Speedo is integrated as a resource adapter, transactions are demarcated by the EJB container.
    The transaction manager instance (implements javax.transaction.TransactionManager) must be registered into JNDI. The JNDI name can be assigned to the property                                            org.objectweb.speedo.jca.TMName in the speedo.properties configuration file. If no name is specified, several attempts are done with classical JNDI names used in some
    application servers:
Application Server JNDI name of TransactionManager instance
JOnAS javax.transaction.UserTransaction
JBoss java:/TransactionManager
WebLogic javax.transaction.TransactionManager
WebSphere jta/usertransaction
Orion java:comp/UserTransaction
    The user does not have to take care of the transactions demarcation any more:
   
public void makeAgencyPersistent(Agency agency){
PersistenceManager pm = pmf.getPersistenceManager();
pm.makePersistent(agency);
}
    Transactions are demarcated by the EJB container if the methods have the "required" tag in the bank.xml deployment descriptor:
   
<container-transaction>
<method>
        <ejb-name>BankApplication</ejb-name>
                <method-name>*</method-name>
        </method>
        <trans-attribute>Required</trans-attribute>
</container-transaction>
    Nevertheless, it is always possible for him to perform transaction demarcation by himself. To do so, he needs to get the transaction manager instance via JNDI

  Top


Data structure creation

    Speedo is able, through Jorm, to create the data structure during the first use of a persistent class. But some data support does not allow to create data structures (SQL relations) inside XA         transactions. As a consequence, Speedo allows only the DO_NOTHING value for the property org.objectweb.speedo.mappingStructure. 
   
    Data structures should be created before launching your J2EE application. To do this Speedo provides a simple tool class named 'org.objectweb.speedo.tools.DataStructureCreation'. An ant     target 'createDataStruct' is also provided inside the build.xml of the speedo distribution. This target launches the tools class with regards to the following usage:

DataStructureCreation (<class name to initialize> | <.jdo file name>)*
[-Djavax.jdo.option.DriverClassName=<driver class name>]
[-Djavax.jdo.option.ConnectionURL=<database url>]
[-Djavax.jdo.option.ConnectionUserName=<user name>]
[-Djavax.jdo.option.ConnectionPassword=<user password>]
    Top

   

Multiple data sources

    The speedo_ra.rar and jdbc_ra.rar archives are not merged into a single archive.
   
    One speedo_ra.rar archive is associated to one and only one data source: an instance of a PMF is registered into JNDI for one and only one data source.
    If you need to access many data sources from Speedo into a J2EE server, you need to "duplicate" speedo_ra.rar archives into your J2EE servers.
   
    For instance, let's say you need to access two data sources from a single J2EE application into JOnAS.
    You have to:
        1. Put two speedo archives in the $JONAS_BASE/rars directory: speedo_for_jonas_ra.rar and speedo_2_for_jonas_ra.rar.
        2. In the speedo_for_jonas_ra.rar define the JNDI name to speedo and the PropertyFile to speedo.properties.
            In the speedo_2_for_jonas_ra.rar define the JNDI name to speedo2 and the PropertyFile to speedo2.properties.
        3. Put two properties files speedo.properties and speedo_2.properties in the $JONAS_BASE/conf directory, each with its database description.
        4. Define two speedo resources in the jonas.properties file:
            jonas.service.resource.resources    speedo_for_jonas_ra, speedo_for_jonas_2_ra     
        5. Access the first PMF for the first data source by using speedo as JNDI entry  and access the second PMF for the second data source by using speedo_2 as JNDI entry.

    The figure below describes the multiple data sources access using Speedo:
   

   Top


 Step Back