• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Using EntityManager from a servlet?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Author
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you packaging your persistence.xml file? Can we see its contents?
 
Karl Stenerud
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
META-INF/persistence.xml:



The data source is deployed like so:


 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Karl Stenerud
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic