1

I wrote an application that is using beans for creating and storing books(titles,authors etc) to the database. The basic functions for adding and deleting books are implemented in a local stateless bean.

My problem is that I don't quite get how this would work in a concurrent way. If two users would like to get a handle on the managing bean at the same time would this work by default? And if so how exactly does it deal with concurrent requests?

Thanks

Note: Only creation and deletion should be supported.No editing! So I suppose that there are no conflicts there in terms of writes and reads!

1
  • What concerns do you have with concurrent requests? Commented Mar 9, 2011 at 11:56

3 Answers 3

3

There can only be one calling thread per EJB instance at a given time. EJBs are (most of the time) pooled by the container: if two clients call the same method (or different methods on the same EJB type) they will be given different instances by the container or one will have to wait until one instance is ready to handle its call.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply!How do I know which of the two will happen (be given different instances by the container or one will have to wait until one instance is ready to handle its call)?
That's the whole point: you don't. You're supposed to write your EJB code as if it were single threaded. This works because all threading issues are handled by the container and not your bean.
With the containers I know you can configure the EJB pool size (how many instances the container will create and keep ready to serve requests), you have to have at least as many EJBs in the pool as you want to be able to serve concurrent requests (knowing that there are other limits such as thread pool size, number of processors, etc...)
3

They would get a different instance of your managing bean. It would work by default, if your managing bean is indeed stateless.

The concurrency is handled by the database and its support for transactions. If a transaction tries to modify a book that is being deleted by another concurrent transaction (for example), one of the transactions will rollback, due to a SQLException thrown by the database driver when committing the transaction.

2 Comments

Yeah thats what I thought but does the fact that the managing bean is stateless guarantees different instances for different users?
The users each get an instance of a proxy which, in the end, calls a method of your session bean class. It's guaranteed by the spec that an instance of your session bean class may only handle one call at a time. Quote from the spec : The container serializes calls to each session bean instance. Most containers will support many instances of a session bean executing concurrently; however, each instance sees only a serialized sequence of method calls. Therefore, a session bean does not have to be coded as reentrant.
0

What kind of client technology do you use?

If you use a web interface you could use the Gateway pattern (a stateful session bean and an extended perstence context, see "Real World Java EE Patterns – Rethinking Best Practices" by Adam Bien) and just return the attached entity.

If you have a remote client, you probably want to return a DTO with a business key, which uniquely identifies the entity.

2 Comments

Thanks for the reply! I'm using a web interface but my bean is stateless and I'm injecting persistenceContext (an EntityManager that persists Book objects)
I think I got your question wrong. My answer is more about a "handle" to the entity than to the EJB. As others mentioned: stateless beans are shared (and thus should not have any states). Not much to concern about concurrency beyond some optimistic db locks, usually. But it might save you some synchronization time (re-attaching entities) using the mentioned Gateway pattern when you develop a web interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.