Problems in getting database connection from data source registerd with JNDI
Dear Forum Members,
I have created JDBC Connection pool using Admin Console tool of Sun Java System Application Server. I have done using the menu path Common Tasks->Resources->JDBC->Connection Pools. I am using JavaDB as back-end database for my JDBC application.
The JNDI name of the connection pool I have created is SalesDBPool.
The resource type is javax.sql.DataSource.
Datasource class name is org.apache.derby.jdbc.ClientDataSource.
With reference to the newly created connection pool SalesDBPool, I have also created JDBC Resource using the menu path Common Tasks->Resources->JDBC->JDBC Resources. The JNDI name of the created JDBC resource is : jdbc/SalesDB.
I have checked whether the settings I have made are correct or not by pressing the Ping push button of the Connection Pool SalesDBPool and the system responded with the message "Ping Succeeded".
With this background, that is, keeping the Sun Java System Application Server running in the background, I tried to create a Java Class using NetBeans IDE. The source code of the program is given below:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class DataSourceApp {
public static void main(String[] args) {
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/SalesDB");
Connection con = ds.getConnection();
DatabaseMetaData dbmd = con.getMetaData();
System.out.println("Database Product Name: "+dbmd.getDatabaseProductName());
} catch (NamingException ex) {
Logger.getLogger(DataSourceApp.class.getName()).lo g(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DataSourceApp.class.getName()).lo g(Level.SEVERE, null, ex);
}
}
}
When I compiled the above program, no errors were reported and the compilation was successful. When I tried to execute the above program, I received the following runtime error message:
SEVERE: null
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(N amingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(Init ialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx (InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext. java:392)
at DataSourceApp.main(DataSourceApp.java:21)
BUILD SUCCESSFUL (total time: 0 seconds)
I also tried changing the string in the lookup() method by putting "java:comp/env/jdbc/salesDB"; still I got the same error method.
Then I tried adding the following two statements before calling the InitalContext() method:
java.util.Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.j ndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL,"java:comp/env/jdbc/SalesDB");
Context ctx = new InitialContext(env);
But, even this did not work; I got the different run-time error message as stated below:
javax.naming.InvalidNameException: java:comp/env/jdbc/SAPDEV [Root exception is java.net.MalformedURLException: unknown protocol: java]
at com.sun.jndi.fscontext.FSContextFactory.getFileNam eFromURLString(FSContextFactory.java:119)
at com.sun.jndi.fscontext.RefFSContextFactory.createC ontext(RefFSContextFactory.java:41)
at com.sun.jndi.fscontext.RefFSContextFactory.createC ontextAux(RefFSContextFactory.java:47)
at com.sun.jndi.fscontext.FSContextFactory.getInitial Context(FSContextFactory.java:49)
at javax.naming.spi.NamingManager.getInitialContext(N amingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(Init ialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.ja va:223)
at javax.naming.InitialContext.<init>(InitialContext. java:197)
at DataSourceApp.main(DataSourceApp.java:23)
Caused by: java.net.MalformedURLException: unknown protocol: java
at java.net.URL.<init>(URL.java:574)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at com.sun.jndi.fscontext.FSContextFactory.getFileNam eFromURLString(FSContextFactory.java:117)
I request any of our forum members to kindly provide a solution to solve this issue.
Re: Problems in getting database connection from data source registerd with JNDI
This is how we define JNDI datasources.
In the application context XML file.
Code :
<Resource name="jdbc/someName" auth="Container" type="javax.sql.DataSource"
url="jdbc:mysql://127.0.0.1/databaseNameHere"
driverClassName="com.mysql.jdbc.Driver"
username="dbUsername" password="dbPassword"
maxActive="20" maxIdle="10" maxWait="100"/>
After this we use the following code to get the datasource in Java.
Code :
try {
final Context initialContext = new InitialContext();
dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/someName");
} catch (NamingException e) {
// Log something here
}
Thats about it. Hope any of that helps.
// Json
Re: Problems in getting database connection from data source registerd with JNDI
Dear Sir,
I tried the solution offered by you. Still, I am getting same run-time error. Let me clarify what I did with respect to the solution you offered.
I created an XML file web.xml which is placed in the same classpath as my Java source file (.java) and complied .class file. The listing of the XML is pasted here:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : web.xml
Created on : 19 September, 2009, 9:18 AM
Author : Admin
Description:
Purpose of the document follows.
-->
<Resource name="jdbc/__sapdev" auth="Container"
type="javax.sql.DataSource" driverClassName="org.apache.derby.jdbc.ClientDrive r"
url="jdbc:derby://localhost:1527/F:/RANGA/SAPDEV;create=true"
username="rangarajan" password="dellpc" maxActive="20" maxIdle="10"
maxWait="-1"/>
<resource-ref>
<description>Connection Pool for database stored directory path F:/RANGA/SAPDEV</description>
<res-ref-name>jdbc/__sapdev</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
My java source code is pasted here:
package learnjava;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.*;
import javax.sql.DataSource;
/**
*
* @author Admin
*/
public class SAPConnection {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
Context ctx = new InitialContext(props);
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/__sapdev");
Connection con = ds.getConnection();
} catch (NamingException ex) {
Logger.getLogger(SAPConnection.class.getName()).lo g(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(SAPConnection.class.getName()).lo g(Level.SEVERE, null, ex);
}
}
}
I have created JBDC Resource in Sun GlassFish Enerprise Server v2.1 with JNDI name as "jdbc/__sapdev".
The poolname for this JDBC Resource is "sapdev". For the JNDI name "sapdev",
the Resource Type is: javax.sql.DataSource
the Datasource class name is: org.apache.derby.jdbc.ClientDataSource
I have made sure that the GlassFish Enterprise server is running in the background.
Now, what I am supposed to do to avoid the run time error.
I thank you so much for your sincere efforts to offer solution for the problems I posted in this forum.
With best wishes and regards,
K. Rangarajan.
Re: Problems in getting database connection from data source registerd with JNDI
No, the resource does not go in your web.xml, it goes in your applications context.xml file. So in $CATALINA_HOME/conf/Catalina/localhost/myApplicationName.xml
// Json
Re: Problems in getting database connection from data source registerd with JNDI
Thanks a lot Sir! I will revert back I encounter any problem