This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes EJBs and Pojos Concurrency Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJBs and Pojos Concurrency Problem" Watch "EJBs and Pojos Concurrency Problem" New topic
Author

EJBs and Pojos Concurrency Problem

Markus Schmider
Ranch Hand

Joined: Feb 25, 2007
Posts: 100
Hello,

We are using EJB 3.0 on Weblogic 10.3
We are in the process of porting a legacy application to EJB 3.0
We have so far ported most service classes to message driven beans or stateless session beans.
Some stateless session beans still use pojos which are essentially services: they transform data and are stateless (e.g. DataConverter)
The EJB obtains a reference of this pojo during the PostConstruct event.
Under load we have exeperienced cuncurrency problems in DataConverter: it produces jumbled data.
After converting DataConverter to a EJB these problems dissapeared.

My question is how can such concurrency problems arise in the first place?
A client calls a method on an EJB. During this method execution the EJB cannot be accessed by other threads. During this method execution a method is called on DataConverter which was instatiated by the EJB. How can possibly another thread access this DataConverter instance?

By the way is there any literature about mixing EJBs and non-EJBs in the application server? I have tried to find suitable citations to discourage colleagues from mixing EJBs and POJOs but have not found any.

Thanks,

Hans
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9915
    
158

Can you please post some code? That will give us a better idea of what's going on.


[My Blog] [JavaRanch Journal]
Markus Schmider
Ranch Hand

Joined: Feb 25, 2007
Posts: 100
Finally we found the real culprit, there was a small utitilty class with static methods.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
By the way is there any literature about mixing EJBs and non-EJBs in the application server? I have tried to find suitable citations to discourage colleagues from mixing EJBs and POJOs but have not found any.


You might be unable to find website material discussing this because it is not a suitable concept. In object-oriented applications, there typically will be many types of objects that interact with each other. Designers should try to eliminate complex dependencies that may envolve into problems or limitations in the futures. That said, an application that is using an EJB-framework should separate the business logic and the EJB. A Session EJB should not contain business logic. Ideally, it is only a facade for a POJO and the logic is coded in the POJO class. In the future, you should be able to simply drop the EJB-based parts of the application without ever having to touch any business logic classes or statements. The only way that this can be done is if the logic is never coded in an EJB Session Bean.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy!

Jimmy, that's the third time I hear about treating ejb's as an entry point for business logic and to use pojos to implement the actual logic (i.e. injected into ejb using great CDI features). I really like this approach but still: what about the transactions? It is not available in pojos or even CDI'ed beans but still is an important part from the business logic point of view, isn't it?
And what about the EntityManager - ejb brings the feature of easy injecting it to the EJBs and cooperate with EJBs (not POJOs) transactions - isn't the EM also a part of business logic processing?

I understand that EJBs should be used as an entrypoints (rest, soap ws, remote invocation, etc), but should you really implement ALL your business logic in beans which are not ejb's?

What is your opinion about it?

Cheers!

EDIT: Also something that relates to the topic of the OP: what about the thread-safety of such POJO-Business Logic Layer solution? I mean, I know that a particular instance of my SLSB will be invoked by exactly one thread at a time. If I would use my SLSB as a facade to business logic which is implemented as a POJO and injected using CDI than how can I be sure that my business logic component is thread-safe? I'm not sure at the moment, but I don't think that CDI container must obey the thread-safety rules as the EJB container.

Once again - I like a lot the idea where EJB is an entrypoint and business logic components are injected using CDI as it makes the whole solution a lot cleaner and EJB has better defined role in the whole architecture. My only concern is how I actually should use this EJB + CDI combination...

Thanks in advance!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJBs and Pojos Concurrency Problem
 
Similar Threads
Newbie Question: What's the relationship between JPA and EJB?
Why pool stateless session beans when you can get by with one instance?
EJB - Declarative operations
JSF, EJB, JPA design quandaries !!!
My SCEA Part 1Study Notes