I'm a newcomer in EJBs & rather confused about some aspects of Entity beans.
I understand that Session beans can be stateful or stateless, which makes perfect sense. But I'm not sure how Entity beans can be stateless ?? I also read that Entity beans are shared among different Cliens/Session-beans. If Entity beans are stateless & if the same instance of Entity bean is shared, who is responsible for data integrity of an Entity bean. Is it the Container/Server, which magically keeps an Entity bean & its corresp. database record in sync ??
For e.g. --
1. Lets start with an Entity bean Z with value of 'balance' field = 100.
2. Then lets say that a Session bean A changes the 'balance' field of Entity bean Z to 200.
3. Another Session bean B now reads the value of Entity bean Z's balance field. What will it be ???????? will it be 100, or 200 ???
Your help/input will be appreciated ! Thanks.
Entity beans are inherently stateful since they model underlying database data, which is state. However, multiple entity bean Java object instances can represent the same row in the database.
The trick to data integrity is the application server must ejbLoad() the database data on every transaction begin, and ejbStore() the database data on every transaction commit. By loading database data with proper isolation, you are locking the row which delays other entity bean Java object instances from ejbLoad()'ing that row until the first instance is finished.
I think you are wrong in one place.. The entity beans represents the persistence data in dbms.. So, it's somewhat stateful.. There, there is only one and only one instance of entity instance that represents the row in the database table.. So, in the case, where A modify the instance of entity bean that presents the a row, it actually gets modified and reloaded.. The B now tries to read, and it reads the value of 200 in this case.. The trick is, there is only instance of enity bean, but there can be many references to that..
Entity EJBs can be accessed by multiple clients, but only in a serial fashion. If two clients attempt to access the same entity EJB instance (an instance having the same primary key), the second client blocks until the EJB is available.
WebLogic Server locks a cached instance of an entity EJB only for the duration of a transaction. If the EJB is not involved in a transaction, the instance is locked only for the duration of each method invoke.
This means that multiple clients can access the same entity EJB in a serial fashion, but only if the bean is not involved in a transaction.
You might also want to understand the TRANSACTION_ISOLATION LEVELS that can be specified for each transaction.
Let me know if this solved your dillema.