The ejbLoad( ) and ejbStore( ) Methods

Throughout the life of an entity, its data will be changed by client applications. In the ShipBean, we provide accessor methods to change the name, capacity, and tonnage of the Ship EJB after it has been created. Invoking any of these accessor methods changes the state of the ShipBean instance, and these changes must be reflected in the database.

In container-managed persistence, synchronization between the entity bean and the database takes place automatically; the container handles it for you. With bean-managed persistence, you are responsible for synchronization: the entity bean must read from and write to the database directly. The container works closely with the BMP entities by advising them when to synchronize their state through the use of two callback methods: ejbStore( ) and ejbLoad( ).

The ejbStore( ) method is called when the container decides that it is a good time to write the entity bean’s data to the database. The container makes these decisions based on all the activities it is managing, including transactions, concurrency, and resource management. Vendor implementations may differ slightly as to when the ejbStore( ) method is called, but this is not the bean developer’s concern. In most cases, the ejbStore( ) method will be called after one or more business methods have been invoked or at the end of a transaction.

Here is the ejbStore( ) method for the ShipBean:

public void ejbStore( ) { Connection con = null; PreparedStatement ...

Get Enterprise JavaBeans, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.