users@glassfish.java.net

Re: Database connection in GlassFish server

From: <glassfish_at_javadesktop.org>
Date: Fri, 03 Apr 2009 03:24:13 PDT

Hi,

I finally found out what my flaw is. The flaw is neither with my java code nor with the GlassFish. The flaw is my application deployment descriptor(web.xml)
When working with Tomcat, I didn't create the following but application still works fine
<resource-ref>
        <res-ref-name>jdbc/myapp</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
</resource-ref>

But with GlassFish we need to create resource-reference tag in web.xml. Since this tag is not there in web.xml, I was unable to connect to database.
Which ever code I use for connection object, it works fine
Context i = new InitialContext();
Context e = (Context) i.lookup("java:/comp/env");
DataSource d = (DataSource) e.lookup("jdbc/myapp");
con = (Connection)d.getConnection();
(OR)
Context i = new InitialContext();
DataSource d = (DataSource) i.lookup("jdbc/myapp"); //If I use this part of code in Tomcat, I get Object not bound error. I have to either use the above or below code to make it up.
con = (Connection)d.getConnection();
(OR)
Context i = new InitialContext();
d = (DataSource) i.lookup("java:comp/env/jdbc/myapp");
con = (Connection)d.getConnection();

Thanks Shalini for your inputs. Both my connection pool name and resource name is jdbc/myapp.
One more useful link I found out which was handy http://www.albeesonline.com/blog/2008/08/06/creating-and-configuring-a-mysql-datasource-in-glassfish-application-server/

Regards,
Kiran.
[Message sent by forum member 'kkiran' (kkiran)]

http://forums.java.net/jive/thread.jspa?messageID=340436