Skip to main content

Unable to get JDBC connection

2 replies [Last post]
diltsman
Offline
Joined: 2008-06-13
Points: 0

Using Glassfish v2, I'm playing with Java EE development. I'm writing a servlet that accesses a PostreSQL database. In the admin console I have the connection pool setup and pings succeed.

When I try to get to the JDBC resource through JNDI it doesn't show up. I have the JDBC Resource set up as:
JNDI Name: jdbc/bang/readOnly
Pool Name: BangReadOnly
Description a read only connection to the Bang database.
Status: Enabled

web.xml has:

Connection to the Bang database for reading.
jdbc/bang/readonly
javax.sql.DataSource
Container

sun-web.xml has:

jdbc/bang/readonly
jdbc/bang/readOnly

In the processRequest method I have the following code to get the JNDI context:
try {
Context context = new InitialContext();
Hashtable<?, ?> environment = context.getEnvironment();
Object lookup = context.lookup("java:comp/env/jdbc/bang/readonly");
lookup.toString();
} catch (NamingException ex) {
Logger.getLogger(Data.class.getName()).log(Level.SEVERE, null, ex);
}

I'm certain whatever I'm missing is rather simple. I would greatly appreciate it if someone could explain why I can't get the JDBC resource in the JNDI context and how to fix it.

Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
xsi
Offline
Joined: 2008-06-25
Points: 0

hi

have you try the jndi name in ordeer to connect
java:comp/env/jdbc/bang/readOnly

also my code in order to obtain a connection is :

Context initCtx,envCtx;
DataSource ds = null;
try {
initCtx = new InitialContext();
envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
ds = (DataSource) envCtx.lookup("jdbc/Ora_mensajes");
} catch (NamingException e)
{
e.printStackTrace();
}

i used tomcat 6.0.18 , oracle jdbc 10g

---- web.xml----

jdbc/Ora_mensajes
javax.sql.DataSource
Container
Shareable

---context.xml----
maxIdle="6" maxWait="5000" name="jdbc/Ora_mensajes" password=*********
type="javax.sql.DataSource"
url="jdbc:oracle:thin:@esbasint002.lan.eds.es:1521:EXPLODES"
username="manuales" />

i hope it helps

amyroh
Offline
Joined: 2004-05-06
Points: 0

I don't see any obvious errors in your configuration at a first glance.

You might want to make sure to follow the steps from -
http://blogs.sun.com/Glassfish_PostgreSQL/entry/introducing_support_for_...

I am assuming you used admin gui to create the jdbc connection pool, correct?

Can you send your config files including domain.xml with connection pool and your test webapp if you are still having an issue? That'll help me to figure out the issue you're experiencing.

Thanks,
Amy