aspose file tools*
The moose likes JBoss/WildFly and the fly likes Using EntityManager from a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "Using EntityManager from a servlet?" Watch "Using EntityManager from a servlet?" New topic
Author

Using EntityManager from a servlet?

Karl Stenerud
Greenhorn

Joined: Feb 12, 2009
Posts: 3
I've been reading a number of web pages and blogs that show how to use an EntityManager in a servlet, and yet I have been unable to get a single example to work

In an existing J2EE application, deployed to JBoss, I can from any EJB do the following:



And it just works. However, if I try the same thing from a servlet (in the same J2EE application):



It fails with a NullPointerException because em never got injected (and JBoss never complained, either)

If I try it like this:



I get javax.naming.NameNotFoundException: persistence not bound

I've tried it with a persistence context ref in web.xml and without. It seems to make no difference whatsoever!



The whole JNDI "magical incantation" thing has always driven me up the wall, but this is sheer madness! How is one ever supposed to know what magical string value to use where?
Javid Jamae
Author
Ranch Hand

Joined: May 14, 2008
Posts: 198
Where are you packaging your persistence.xml file? Can we see its contents?


Author: JBoss in Action, javidjamae.com, @javidjamae on Twitter
Karl Stenerud
Greenhorn

Joined: Feb 12, 2009
Posts: 3
META-INF/persistence.xml:



The data source is deployed like so:


Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

STOP!!!


Please, please don't.

You are tightly coupling one technology with another. Please don't.

This is not the responsibility of a Servlet. A Servlet takes a request object, gets information from it and should immediately pass it off to some other Java class/code do to the rest of the work. Once the work is done, the data stuff that needs to be sent to a response is passed back into the Servlet from the java call, it is then the Servlets responsibility to take the data put it into the response object, and pass it off to some view, usually a JSP page.

But do not do any business logic, or EntityManager dao/data stuff in Servlet code.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Karl Stenerud
Greenhorn

Joined: Feb 12, 2009
Posts: 3
This is not for putting business code in the servlet. This is for getting servlet configuration data from the database.

I want to get an entity manager so that I can inject it into a configuration object that will do the DB magic to get me the configuration options I need to initialize this servlet.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Karl Stenerud wrote:This is not for putting business code in the servlet. This is for getting servlet configuration data from the database.

I want to get an entity manager so that I can inject it into a configuration object that will do the DB magic to get me the configuration options I need to initialize this servlet.


OK. Good glad it is not business code. Personally, I still wouldn't put servlet configuration options into a database, but into a servlet configuration file instead. But that is just my opinion, and you are free to move about the cabin.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using EntityManager from a servlet?