Show TOC Start of Content Area

Procedure documentation Using JDO with Session Beans  Locate the document in its SAP Library structure

Use

You can use JDO in a three-tier application, in which a session bean represents the middle (controller) tier.

JDO can be used with both stateful and stateless beans. In addition, you can choose to implement the beans with either container-managed or bean-managed transaction demarcation.

This procedure discusses the scenarios for using JDO with the following types of session beans:

·        Stateless bean with container-managed transaction demarcation (CMT)

·        CMT stateful bean

·        Stateful bean with bean-managed transaction demarcation (BMT)

The usage of a BMT stateless bean is not considered because using the BMT contract with stateless beans is not very useful. The EJB specification requirement for BMT stateless beans is to complete a transaction before the business method return since stateless beans are not supposed to keep their state between business method invocations. For more information about session beans, see Developing Session Beans.

Procedure

JDO with CMT Stateless Bean

The life cycle of a stateless session bean is relatively simple. It comprises the following phases:

...

       1.      A new instance is created.

       2.      The container invokes the bean’s setSessionContext() method followed by ejbCreate(). The instance transitions to a method-ready state and its business methods may be executed. The ejbActivate() and ejbPassivate() methods are not invoked for stateless beans; they therefore are left empty.

       3.      The container invokes ejbRemove() before destroying the instance.

In the J2EE Engine you work with JDO using a preconfigured PersistenceManagerFactory (PMF), which you look up from the naming system. To be able to look up the PMF, you should include a resource reference for it in the deployment descriptor of the bean. The PMF is looked up early in the bean’s lifecycle – typically, in the setSessionContext() method:

Example

public class MySessionBean implements SessionBean {

   SessionContext sc;

   PersistenceManagerFactory pmf;

public void setSessionContext(SessionContext sc) {

      this.sc = sc;

      try{

         Context ctx = new InitialContext();

         pmf = (PersistenceManagerFactory)ctx.lookup("java:comp/env/jdo/defaultPMF");

      }catch (Exception e){

         }

   }

...

 

The PersistenceManager instance is obtained in each business method of the bean that uses JDO. First, you must set the PM to null. Then you obtain a PM instance from the PMF, work with the JDO instances, and finally close the PM. Usually, this is done within a try - finally block:

Example

public void raiseSalary(int empId, double percent) {

   PersistenceManager pm = null;

   try {

      pm = pmf.getPersistenceManager();

      Employee emp = (Employee)pm.getObjectById(new Employee.Id(empId), false);

      double oldSalary = emp.getSalary();

      double newSalary = oldSalary + percent * oldSalary;

      emp.setSalary(newSalary);

   }

   finally {

      if (pm != null && !pm.isClosed())

      pm.close();

   }

}

 

JDO with Stateful Bean

The life cycle of a stateful session bean is slightly different from that of a stateless one. The difference stems from the fact that a stateful bean is associated to a particular client and keeps the conversational state between the invocations of its methods. In addition, the EJB container invokes the ejbActivate() and ejbPassivate() methods of the stateful bean to free resources.

The usage of JDO in a stateful bean is similar to the implementation in a stateless bean. The PMF is looked up in the setSessionContext() method.

In CMT stateful beans, the PM is obtained and closed in each business method because the methods of the bean might be invoked in different transactions.

In a BMT stateful bean, you can use a single PM instance throughout different business methods, as long as they are all invoked in a single transaction. It is recommended that you obtain the PM instance after the beginning of the transaction and close it before the transaction end. The javax.transaction.UserTransaction interface is used to enable component-managed transaction demarcation. For more information, see Using Component-Managed JTA Transactions.

In the following code, the transaction begins in the ejbCreate() method of the bean where a JDO instance is created. The instance is associated to the bean instance until the end of the transaction. The transaction spans several business methods, and finally ends either with a commit or with a rollback invocation.

Example

public class MySessionBean implements SessionBean {

   SessionContext sc;

   PersistenceManagerFactory pmf;

   PersistenceManager pm;

   UserTransaction ut;

public void setSessionContext(SessionContext sc) {

      this.sc = sc;

      try{

         Context ctx = new InitialContext();

         // get PMF

         pmf = (PersistenceManagerFactory)ctx.lookup("java:comp/env/jdo/defaultPMF");

         // get UserTransaction

         ut = (UserTransaction)sc.getUserTransaction();

      }catch (Exception e){

         }

   }

// create method

public void ejbCreate() throws CreateException {

   try{

      ut.begin();

      pm = pmf.getPersistenceManager;

      ...

      // create JDO instance

   }

}

...

// business method

public void doChange() {

   // do work

}

// business method

public void applyChange() {

   try{

      pm.close();

      ut.commit();

   }catch (Exception e){}

}

// business method

public void undoChange() {

   try{

      pm.close();

      ut.rollback();

   }catch (Exception e){}

}

...

 

See also:

Working with Persistent Objects

 

 

End of Content Area