I have an 2 application.One is application client,other one is EJB project.
My client has references to server side.Container Managed Bean transaction is used in beans.I can not I inject entity manager to beans using following statement.
@PersistenceContext(unitName = "DBService")
private EntityManager em;
I have a message driven bean, 2 stateless bean and a singleton bean in server side.
I have local injection of 2 stateless bean from message driven bean.According to
message type of JMS message (it is come from server or client), message driven bean calls business methods of related statefull beans.Also each one of stateless beans inject singleton bean.
I am doing some changes on entities in singleton bean, updating some fields of entities, I am sending jms messages client or server. when messages are handled by 2 stateless beans, it calls business methods of singleton bean. For example I changed a field of entity.Then singleton bean send jms message to client, then client makes some work, then he send message back to server, server consumes this message in message driven bean, then it calls business method of related Stateless bean
, then stateless mean calls buısiness method of singleton bean.Then I am doing a query from database, But I can not see the latest state of changed entity.My changes are overwritten.
I thought that EntityManager of Singleton bean does not hold state of entities correctly if it is injected by multiple beans.I thought that same singleton bean instance must be get from connection pool. I have serious problems about this.
Do you think that my detection is correct? or I am doing something wrong?
Problem: I think that when a stateless bean calls a method of singleton bean new transaction begins, then second stateless beans calls a method of singleton bean, it also starts a transaction, but previous one is not committed, 2 transactions are not aware of changes which made by each other’s. But I do not know how to solve this. Using Bean managed transaction is a good solution for singleton beans?
By default, beans annoted with
@Singletonare container managed & uses locking modeLockType.WRITE, explicitly can apply@ConcurrencyManagement(CONTAINER).If any of the method is being called by a client, all the other requests will have to wait for the previous call to return. Here, you can apply lock to the entry method of the singleton bean & then can call other methods from within.
You can also annotate at method level with
@Lock(LockType.WRITE). Therefore, the sequence of the call will pertain the order in which they are called by the clients.Edit : The concurrent access to the singleton bean is serialized and the methods by default have
REQUIREDtransaction attribute.So the container invokes the bean’s method in the client’s transactional context. Therefore they will have individual transaction spanning from stateless bean to singleton bean.
As described above, request from bean B & C will be serialized/enqueued until A haven’t finished. So all the request will be processed in isolation without affecting others, but may probably lead to performance bottleneck.
You can apply method level lock,
@Lock(LockType.WRITE)for methods altering the shared data &@Lock(LockType.READ)for methods which access them.