|Oracle8i Enterprise JavaBeans and CORBA Developer's Guide
An EJB is a software component that runs in a server. This runtime environment is one factor that distinguishes an enterprise bean from a JavaBean. The JavaBean usually runs on a client system, such as a network computer, a PC, or a workstation, and it typically performs presentation tasks, such as implementing GUI widgets.
There are two kinds of EJB: session beans and entity beans. An easy way to think of the difference is that a session bean implements one or more business tasks, while an entity bean implements a business entity. A session bean might contain methods that query and update data in a relational table, while an entity bean represents business data directly. For example, an entity bean can represent a row in a relational table.
Session beans are often used to implement services. For example, an application developer might implement one or several session beans that retrieve and update inventory data in a database. You can use session beans to replace stored procedures in the database server, and gain the scalability inherent in the Oracle8i Java server.
Session beans are not inherently persistent. Be careful about this word. Persistence can refer either to a characteristic of the bean--entity beans are persistent, session beans are not inherently persistent--or it can refer to data that a bean might save, so that the data can be retrieved in a future instantiation. Persistent data is saved in the database.
So, a session bean saves its state in an Oracle8i database, if required, but it does not directly represent business data. Entity beans persist the business data either automatically (in a container-managed entity bean) or by way of methods that use JDBC or SQLJ, and are coded into the bean (bean-managed).
Implementing the synchronization interface can make data storage and retrieval automatic for session beans. See "Session Synchronization".
The version 1.0 of the EJB specification requires that the EJB server support session beans. Entity bean support is optional. In this release the Oracle8i EJB server does not support entity beans. Entity beans will be supported in a future release.
A session bean is created by a client, and is usually specific to that client. In Oracle8i more than one client can share a session bean.
Session beans are transient, in the sense that they do not survive a server crash, or a network failure. When a session bean is re-instantiated, state of previous instances is not automatically restored.
A stateful session bean maintains state between method calls. For example, a single instance of a session bean might open a JDBC database connection, and use the connection to retrieve some initial data from the database. For example, a shopping-cart application bean could load a customer profile from the database as soon as it's activated, then that profile would be available for any method in the bean to use.
A typical stateful session EJB is a relatively coarse-grained object. A single bean almost always contains more than one method, and the methods provide a unified, logical service. For example, the session EJB that implements the server side of a shopping cart on-line application, would have methods to return a list of objects that are available for purchase, place items in the customer's cart, place an order, change a customer's profile, and so on.
The state that a session bean maintains is called the "conversational state" of the bean, as the bean is maintaining a connection with a single client, much like a telephone conversation.
It is important to keep in mind that the state of a bean is still transient data, with respect to the bean itself. If the connection from the client to the bean is broken, the state can be lost. This of course depends on whether the client is unable to reconnect before timeout.
In most EJB implementations, a stateless session bean is used for short transactions with a client. In these implementations, the major difference between stateful and stateless session beans is that a stateless bean can change identity between method calls, while a stateful bean maintains identity. If the client calls Method A in a stateless bean, then calls Method B in the same stateless bean class, the second method might be called on a separate instance of the bean.
In the Oracle8i implementation, stateless and stateful beans are the same. The inherent multi-threaded nature of the Oracle8i MTS data server makes stateful session beans functionally identical to stateless beans. There is no difference between the two for Oracle8i.
For example, a typical use of stateless session beans is a server maintaining a pool of beans ready to serve clients that are performing short OLTP-like transactions. But this is not required in the Oracle8i architecture for performance. Stateful beans can serve just as well in this situation.