As per the EJB Spec EJB's should not attempt to play with any threads. I have heard some folks say that this means that any entity like a data access object that the EJB calls should not spawn any threads. Some say that such objects outside of the EJB class can . The line in the specification is as shown below..
""
The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt
to start, stop, suspend, or resume a thread; or to change a thread’s priority or name. The enterprise
bean must not attempt to manage thread groups.
"".
This seems to be very open ended and is not specific. Any thoughts on what should be the right way of working with threads in a EJB world would be appreciated..
-
EJB and Threads (5 messages)
- Posted by: Vibhu Srinivasan
- Posted on: August 15 2002 17:59 EDT
Threaded Messages (5)
- EJB and Threads by William Kemp on August 15 2002 18:38 EDT
- EJB and Threads by David Jones on August 15 2002 20:38 EDT
- EJB and Threads by Vibhu Srinivasan on August 16 2002 09:48 EDT
- EJB and Threads by Dave Wolf on August 16 2002 10:36 EDT
- EJB and Threads by William Kemp on August 16 2002 10:40 EDT
- EJB and Threads by Vibhu Srinivasan on August 16 2002 09:48 EDT
-
EJB and Threads[ Go to top ]
- Posted by: William Kemp
- Posted on: August 15 2002 18:38 EDT
- in response to Vibhu Srinivasan
Hi,
You shouldn't spawn threads in an EJB or in any class that the EJB calls out to. In WLS, for example, the transaction context is associated with the thread. When you spawn a thread, that transaction context is not associated with the spawned thread which makes it useless for any activity that would need to be part of the current transaction.
I think the spec is pretty clear and I don't know why it should matter whether or not you put the thread creation code in a 'helper' class. That is still called by the ejb. The Container doesn't do something special when you make a call to another class to allow thread creation.
FWIW, spawning threads in a threaded app server is a bad idea and, imho, is indicative of a design flaw. The app server is already performing thread management for you, which is one of the reasons you use an app server in the first place.
Bill -
EJB and Threads[ Go to top ]
- Posted by: David Jones
- Posted on: August 15 2002 20:38 EDT
- in response to Vibhu Srinivasan
I have heard people say "Well I am not calling it from the EJB I am calling it from an Object the EJB calls so I am not breaking the rules" or "I am starting the thread in my Servlet not the actual EJB itself". Each time I nearly cry.
Thread management is part of the J2EE Containers job. It is that simple.
I think there are so many people that read the rules but don't understand why you should not do certain things from within the container. The spec for every rule states the reason behind the rule. Understand the reason and therefore understand the rule
"• The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt
to start, stop, suspend, or resume a thread; or to change a thread’s priority or name. The enterprise
bean must not attempt to manage thread groups.
These functions are reserved for the EJB Container. Allowing the enterprise bean to manage threads
would decrease the Container’s ability to properly manage the runtime environment."
David -
EJB and Threads[ Go to top ]
- Posted by: Vibhu Srinivasan
- Posted on: August 16 2002 09:48 EDT
- in response to David Jones
From my reading of the SPecification i do feel that threads should not be started. But arent there situations in the real world where you are forced to do so. In a situation where your EJB ends up calling three seperate EJB's one of them calls a mainframe of some sort .There is atleast no context of a spanning transactions when you are talking to a Some mainframes indirectly through an XML API / C API etc.. Then if the process is time consuming you dont want other things to wait.
I have seen both the scenarios in the real world and hence wanted to see what others are doing out there.
-
EJB and Threads[ Go to top ]
- Posted by: Dave Wolf
- Posted on: August 16 2002 10:36 EDT
- in response to Vibhu Srinivasan
This is why JMS exists. If you want a 'logical' thread of control, pass a message to the JMS bus and onto a MDB. This accomplishes the same thing as spawning an async thread and doesnt break the container.
Dont spawn threads, this is why the container is there. Use it.
Dave Wolf
The Scupper Group
dave@scuppergroup.com -
EJB and Threads[ Go to top ]
- Posted by: William Kemp
- Posted on: August 16 2002 10:40 EDT
- in response to Vibhu Srinivasan
These types of problems are best handled by JMS, MDB, and/or WebServices. Tieing up a thread in a long running transaction is the root of the problem, whether you are using J2EE, or not. This type of problem is best addressed by message based asynchronous applications, which is what JMS, MDB, and WebServices give you.
Bill