Forums Register Login

Threads in EJB ??

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
okay I will try that
+Pie Number of slices to send: Send
do you have one link that explain this section of Thread with EJB ? explain what is do, execute in the background?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
ahh..great. This is what I need!

Thanks:-)
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2204 times.
Similar Threads
Customizing EntityManager injection for multi-tenancy
[EJB3] Stateless Session Bean & Inheritance
Entity manager contains() method
Persit method strange behaviour
Confusion in accessing ENTITY bean
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:23:55.