GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes Threads in EJB ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Threads in EJB ??" Watch "Threads in EJB ??" New topic
Author

Threads in EJB ??

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Can I invoke Threads in EJB-Layers?

I want to do something like:




in the view:



Saving in the same Thread (without making a new Thread) works, but when I want to save the entities in a new Thread via a new PersistenceContext, then it does not work.

Is this scenario possible?

How can I do that?
Adrien Ruffie
Ranch Hand

Joined: Jan 14, 2009
Posts: 90
Hello nimo, I think all threads share the same EntityManager, but each thread have their Peristent Context.

But I think you can try this:


in your thread:

Context ctx = new InitialContext();
EntityManager em = (EntityManager) ctx.lookup("yourpersistence/em");

synchronized(em){
em.persist();
}


or try to declare private static EntityManager em;

Because each instance variable of you thread try to use many entitymanager, and each entitymanager use one unique persistent context.

Try do declare one entity manager for one peristent context, for all threads and no one em per thread.

It's just an idea

Best regards

Adrien


SCJP 5, SCDJWS 5
http://adrien-ruffie.blogspot.com
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Do I need a new Context ctx?

How can I inject the PersistenceContext to this ctx via Annotations?

I have tried it and found out, that my thread starts well and a select-statement is executed in this thread but it cannot be loaded!:

Look at my console:






(I do not have any ScrollableResults/Iterator open nor do I created one!)

Any suggestions?
Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
You had problem with the transaction commit, because of the multiple threads. So, you cannot use threads in the EJB.
If you want asynchronous processment, you can use Message Driven Bean.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay I will try that
Adrien Ruffie
Ranch Hand

Joined: Jan 14, 2009
Posts: 90
do you have one link that explain this section of Thread with EJB ? explain what is do, execute in the background?
Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
ejb 3 specification, page 546

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.


I think you can get a detailed explanation verifying the source of any container implementation. But, in my opinion, that level of detail is not necessary, just follow the specification.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Imagin a User using a Web-Client to start a batch-update on records of the binded Database.

The User clicks the button "Submit" and now, the Web-GUI frozes and waits till the batch-process is done.

The User can switch to another side without problem,
but when the user visits the page in which the process is submitted, the user has to wait to see the page until the batch is ended.

I want to avoid that. The User should submit the Button and then it should only be noticed that this process is done without letting the web-gui frozen.

Indeed, I need asynchron processes (EJB-Timers, Quartz or the like..), am I right?
Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
Nimo,

As I said in the previous post, 'if you want asynchronous processment, you can use Message Driven Bean'.

MDB example
http://java.sun.com/javaee/5/docs/tutorial/doc/bnbpl.html
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
ahh..great. This is what I need!

Thanks:-)
 
GeeCON Prague 2014
 
subject: Threads in EJB ??