This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line!
See this thread for details.
Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
  • salvin francis
  • Frits Walraven
  • Piet Souris

JNDI look for EntityManager

Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to JNDI lookup for EntityManager? How?

In ejb 3 in action, page-432,

return (EntityManager)ctx.lookup(EM_NAME);

but in my code also i am doing like this-\
package Factory;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.sql.DataSource;

import Pack.Car;

public class CarEAOImpl implements CarEAO{

public CarEAOImpl(){


private EntityManager getEntityManager() throws NamingException{
InitialContext ctx = null;
ctx = new InitialContext();

catch(Exception e){

return (EntityManager)ctx.lookup("jdbc/ActionBazaarDS");
public void addCar(String steering, String horn, String wheel) {
EntityManager em;
try {
em = getEntityManager();
Car c = new Car(steering, horn, wheel);

} catch (NamingException e) {
// TODO Auto-generated catch block


when it is doing lookup for DataSource, it is giving exception. As the returned value is DataSource and it cannot cast to EntityManager.

But how it was done in book.

Moreover, in Glassfish, we can set only JDBC datasources only, is it possible to set EntityManager also, so that directly i can look for it.
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amandeep
Look at the example at Sahoo's blog.He talks about 2 approaches on how to use JNDI lookup to get your EM.
The article is at Sahoo's Blog

If you're lazy to read the whole article , just look under the sub-heading "Container Managed Entity Manager"
I knew that guy would be trouble! Thanks tiny ad!
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic