What is a BMP bean?

Richard Monson-Haefel

BMP: Bean-Managed Persistence

A BMP bean is an entity bean that synchronizes its state with the database manually. In other words, the bean developer must code explicit database calls into the bean itself. BMP provides the bean developer with more flexibility in the how the bean reads and writes its data than a container-managed persistence (CMP) bean. CMP bean is limited to the mapping facilities provided by the EJB vendor, BMP beans are only limited by skill of the bean developer.

The ability to code an entity bean's persistence logic explicitly is important when the EJB container's CMP features are insufficient to meet the needs of the entity bean. Entity beans that need to synchronize their state with several data sources are excellent candidates for BMP. So are beans that need to access to data sources (possibly legacy systems) that are not supported by CMP. In addition, BMP bean are often employed when a vendors CMP facilities are not sophisticated enough to handle complex Object-to-Relational mapping.

The BMP bean manages its own persistence, but relies on the container to coordinate its reads and writes so that persistence is accomplished in a transactional safe manner. Coordination with the container is accomplished through two mechanisms: The persistence callback methods (ejbLoad( ) and ejbStore( )); and the Environment Naming Context (EJB).

The ejbLoad( ) and ejbStore( ) methods notify a bean that its time to read and write data to the database respectively. In a BMP entity bean the code for reading and writing data to the database is done within these methods. The ejbLoad( ) is called at the beginning of a transaction, just before any business methods are executed, and the ejbStore( ) is called at the end of a transaction just before its committed. This keeps the bean state in perfect synchronization with transactions being executed, without the bean developer having to explicitly manage the transactional operations.

When JDBC is used to for persistence, the Environment Naming Context (ENC) is a JNDI namespace that provides the bean with access to database connections (among other things). The database connections obtained from the JNDI ENC are managed by the container, so that they are automatically enrolled in transactions and pooled. (The extent to which this is supported depends largely on the EJB vendor.) The container may also manage other data source APIs, but JDBC is standard.

Below is an example of an entity bean, the Customer bean that is designed to use BMP to synchronize its own state with a CUSTOMER table in the database.


public class CustomerBean implements javax.ejb.EntityBean {
      
         // persistent bean-managed fields
         public long customerNumber;
         public String lastName;
         public String firstName;

         // non-persistent context fields
         public javax.naming.Context jndiContext;
         public javax.ejb.EntityContext ejbContext;

         // business methods
         public PersonName getName(  ){
                return new PersonName(firstName, lastName);
         }
         public void setName(PersonName name){
               firstName = name.getFirstName( );
               lastName = name.getLastName( );
         }
         // called just before a business method can be invoked at the beginning of a transaction
         public void ejbLoad( ){
            try{
               javax.sql.DataSource ds = (DataSource)jndiContext.lookup("java:comp/env/jdbc/Database");
               java.sql.Connection con = ds.getConneciton( );
               PreparedStatement ps = 
                   con.prepareStatement("select lastname, firstname from CUSTOMER where id = ?");
               CustomerKey key = (CustomerKey)ejbContext.getPrimaryKey( );
               customerNumber = key.id;
               ps.setLong(1, customerNumber);
               java.sql.ResultSet result = ps.executeQuery( );
               while(result.next()){
                      lastName = result.getString("lastname");
                      firstName = result.getString("firstname");
               } 
               con.close();

           }catch(Exception e){ 
                   throw new EJBException(e);
           }
         }

         // called at the end of a transaction just before a commit is attempted
         public void ejbStore( ){
           try{
               javax.sql.DataSource ds = (DataSource)jndiContext.lookup("java:comp/env/jdbc/Database");
               java.sql.Connection con = ds.getConneciton( );
               PreparedStatement ps = 
                    con.prepareStatement("update CUSTOMER set lastname = ?, firstname = ? where id = ?");
               ps.setString(1, lastName);
               ps.setString(2, firstName);
               ps.setLong(3, customerNumber);
               if( ps.executeUpdate( ) != 1)
                     throw new EJBException("SQL update failed. ejbStore( ) failed");
               con.close();

            }catch(Exception e){ 
                    throw new EJBException(e);
           }
        }
... 
}

Note: BMP beans use the ejbLoad( ) and ejbStore( ) callback methods differently than the Container-Managed Persistence (CMP) beans. See Java:API:EJB:EntityBean:CMP for details on how CMP beans use these methods.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact