Forums Register Login

How to create a Template for EntityManager in a WebApplication

+Pie Number of slices to send: Send
Hello
I am having a web applicaion using JPA for CRUD Operations .
I have defined a Template class for creating and closing EntityManager as shown below :




I have a servlet and want to access EntityManager in it .

So for this i am calling createEntityManager() of Controller .

Please tell me is this the right way to do it .
+Pie Number of slices to send: Send
No..It is not write way to create EntityManager Object in servlet. Its Throw Exception because em is based on EJB Container So create the EJB project used it. Its a right way...Hope you understand
+Pie Number of slices to send: Send
I think you did not understand my question .

I will write a Utitl class static methods for ceating and closing EntityManager and use those methods in my servlet ??

+Pie Number of slices to send: Send
I think you should use EntityManager in EJB..
and then we could easily inject that EJB in our Servlet..
+Pie Number of slices to send: Send
if we use EntityManager in EJB..
the instance of EntityManager managed by EJB Container, and so we don't have to deal to open/close the connection and EntityManager..

if you use Spring, then we are forced to write the Util class to deal with the instance of EntityManager..

which one do you use Spring or EJB?..
+Pie Number of slices to send: Send
Hi ,

I am using EJB .
I had written a controller class like this .



I am calling these controller static methods inside my EJB Bean class Methods .

Is there anything wrong in this approach .

+Pie Number of slices to send: Send
if you're using EJB, you could easily inject EntityManager in an EJB..

here's the example code :


hope that helps..
+Pie Number of slices to send: Send
I tried with that but the problem is that when it is a Container Managed Entity as you said then the transactions can be only of JTA .

Means i cannot have EntityTransaction etr = EntityManager.getTransaction() in my code .

+Pie Number of slices to send: Send
EntityTransaction etr = EntityManager.getTransaction();

its used in local transaction...
there are two kinds of transaction, namely Local transaction and Global Transaction..
and there are two kinds transaction management in EJB, namely Container Manage Transaction (CMT) and Bean Manage Transaction...

the Local Transaction involve only one resource (in this instance its involve 1 database)
the Global Transaction involve several resources (in this instance its involve 1/more database, JMS, and etc)..

you could inject JTA through UserTransaction in EJB for handle transactions..
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1772 times.
Similar Threads
EJB3 testing with JUnit4
Beginner needs help with Spring + JPA + Transactions
Appropiate place for creating and closing EntityManagerFactory in Application Managed EntityManager
static methods in Servlet
EntityManager managed by application
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:58:58.