We have a bad performing business method in a StatelessSessionBean. To improve the performance we would like to split this business method up into several asynchronous methods calls.
The problem is that these asynchronous methods have to run in the same transaction (they have to use the same JPA entity manager). As far as I understood the Java EE specification this is not possible using container managed transactions. Is it possible to realize this requirement using bean managed transactions or some other mechanism?
You can't do this, neither with standard EJB nor with any extension. A transaction is tied to a database connection which means you would have to share that connection between several threads. This is not safe.
Also asynchronous code may for some reason be executed with large delay, e.g. under high CPU load. This would keep your transaction open for unnecessarily long time.
What are your options? First of all parallelizing SQL queries (especially writes - you need to have them in one transaction) won't give you much. Database are typically I/O bound. Parallelizing is only worth your time when you have CPU-intensive tasks or you are waiting for something.
In both cases redesign your code to perform parallel code in several threads and have only one writer thread, handling the database and transactions. For example if you need to call 10 servers, collect the responses and store them - spawn 10 threads/use a pool, wait for results and store all of them in one thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With