Question

I'm developing an information system and there is something I don't know how to deal with. Let's say my website allows admin to edit users. When two administrators want to edit the same user at the same time, the administrator who clicked edit later then the first one shouldn't be allowed to.

So I want a transaction to be opened when the administrator clicks on the edit button and committed when they send the edited form.

One solution that I can think of is to put EntityManager in a Named ConversationScoped CDI bean. But I don't think this is a good solution since the presentation tier sould be separated from the business tier and database access belongs to the business tier.

Another solution could be to put EntityManager in a Stateful bean and manage transaction from a Named ConversationScoped CDI bean that would call methods on the Stateful bean. But is managing transactions in the web tier a good idea? What if I decided to have my business tier deployed on another application server, would managing transaction on a remote EJB work? And what If the EJB went passivated, would the transaction be lost?

This is also related to another problem, when you retrieve an object from the database with EntityManager, it gets detached as soon as the transaction is closed. If the object have some lazy initialized fields, the web tier cannot access them because when the object is returned to the web tier, it's detached. So you can either manage transactions in the web tier or iterate through the lazy initialized fields in the business tier. The former solution seems to be incorrect and the later can get very verbose and complicated.

I know I'm asking too many questions but I really want to know how to do this properly.

I have available these technologies: CDI, Seam 3 and EJB.

Of course if you know a better solution, I'd be happy to hear it.

Thank's in advance.

Was it helpful?

Solution

JPA optimistic locking mechanism solves this (see @davidfrancis comment).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top