Unable to pick Jdbc Datasource using JNDI
Hi,
I am using Sun ONE Application server. I am trying to access JDBC-ODBC connection through JNDI. I have performed the following steps:
1) I created a Connection Pool with the name "NewSQLServerDB". It appears in the hierarchy of the Sun ONE Application Server Administration window like this:
Application Server Instances->
Server1->
JDBC->
Connection Pools->
New SQLServerDB
It has the following settings:
General
JNDI Name:NewSQLServerDB
Datasource Classname: sun.jdbc.odbc.JdbcOdbcDriver
Pool Settings
Steady Pool Size:8
Max Pool Size:32
Pool Resize Quantity:2
Idle Timeout (secs):300
Max Wait time:60000
Properties
servername: officeware10
network protocol: jdbc:odbc:NewSQLServerDB (32 bit odbc connection configured with this name )
user: Mirza
password: Secret
database name : DbdPAMS02
2) Next, I am creating a JDBC Resource with the name jdbc/PAMSDB.
It appears in the hierarchy of the Sun ONE Application Server Administration window like this:
Application Server Instances->
Server1->
JDBC->
JDBC Resources->
jdbc/PAMSDB
It has the following settings:
JNDI Name:jdbc/PAMSDB
Pool Name:*NewSQLServerDB
Data Source Enabled:yes
3) Now in my code I am trying to access it like this:
System.out.println("Getting initial context...");
InitialContext ctx = new InitialContext();
System.out.println("- Got initi al context successfully");
System.out.println("Getting datasource...");
String dsName = "java:comp/env/jdbc/PAMSDB";
DataSource ds = (javax.sql.DataSource)ctx.lookup(dsName);
System.out.println("- Got datasource successfully");
4) I am getting the following error when I run this code:
INFO: CORE3282: stdout: Getting initial context...
INFO: CORE3282: stdout: - Got initi al context successfully
INFO: CORE3282: stdout: Getting datasource...
INFO: CORE3282: stdout: Could not get connection error:javax.naming.NameNotFound
Exception: No object bound to name java:comp/env/jdbc/PAMSDB
WARNING: CORE3283: stderr: javax.naming.NameNotFoundException: No object bound t
o name java:comp/env/jdbc/PAMSDB
Please help!!!
[2433 byte] By [
Mirza_92] at [2007-11-14]

Can you check on the admin gui, using the JNDI viewer, if the object is available in the JNDI tree? There is a JNDI browser that's available with the app server.
- login to admin GUI on the browser. Assuming you know your admin port. Typically, this is http://localhost:4848 (PE) or https://localhost:4849 (EE)
- Search for JNDI Browsing Button -- It is available on right hand side when your server config is selected in the tree on the left hand side.
Thanks,
Kedar
Do you have jdbc/PAMSDB as a resource-ref entry in your webapp's WEB-INF/web.xml and WEB-INF/sun-web.xml files? S1AS is typically quite fussy about having resource-ref entries declared before they can be looked up with JNDI. Other webservers like Tomcat are less strict, allowing definition of ResourceRef entries in the server configuration rather than in the webapp:
I believe you will need something like:
-- sun-web.xml -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC
'-//Sun Microsystems, Inc.//DTD Sun ONE Application Server 7.0 Servlet 2.3//EN'
'http://www.sun.com/software/sunone/appserver/dtds/sun-web-app_2_3-0.dtd'>
<sun-web-app>
<session-config>
<session-manager/>
</session-config>
<resource-ref>
<res-ref-name>jdbc/PAMSDB</res-ref-name>
<jndi-name>jdbc/PAMSDB</jndi-name>
</resource-ref>
<jsp-config/>
</sun-web-app>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- . . . other config . . . -->
<resource-ref>
<res-ref-name>jdbc/PAMSDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
Then in your code, do the JNDI lookup on "jdbc/PAMSDB" rather than the fully-qualified "java:comp/env/jdbc/PAMSDB". You can try either way but I have had a bit more success with the shorter/relative version.
HTH,
-=- D. J.
Hi I have done as you said, now it is able to load the data source, however, when I call getConnection over the obtained data source it gives me the exception, the stack trace is given at the end. Here is my code
CODE
try
{
System.out.println("Getting initial context...");
InitialContext ctx = new InitialContext();
System.out.println("- Got initial context successfully");
System.out.println("Getting datasource...Plain");
String dsName = "jdbc/PAMSDB";
DataSource ds = (javax.sql.DataSource)ctx.lookup(dsName);
System.out.println("- Got datasource successfully");
System.out.println("Getting connection...");
conn = ds.getConnection();
System.out.println("- Got connection successfully");
}
catch( SQLException exception )
{
System.out.println("Could not get connection error:"+exception);
exception.printStackTrace();
throw new DbConnectionException( exception.getMessage() );
}
catch( NamingException exception )
{
System.out.println("Could not get connection error:"+exception);
exception.printStackTrace();
throw new DbConnectionException( exception.getMessage() );
}
catch(Exception exception )
{
System.out.println("Exception occurred JNDI getConnection() "+exception.getMessage() );
exception.printStackTrace( System.out );
throw new DbConnectionException( exception.getMessage() );
}
STACK TRACE:
INFO: CORE3282: stdout: Getting initial context...
INFO: CORE3282: stdout: - Got initial context successfully
INFO: CORE3282: stdout: Getting datasource...Plain
INFO: CORE3282: stdout: - Got datasource successfully
INFO: CORE3282: stdout: Getting connection...
INFO: CORE3282: stdout: Exception occurred JNDI getConnection() null
INFO: CORE3282: stdout: java.lang.ClassCastException
INFO: CORE3282: stdout: at com.sun.enterprise.resource.IASNonSharedResou
rcePool.setConfigurationParameters(IASNonSharedResourcePool.java:268)
INFO: CORE3282: stdout: at com.sun.enterprise.resource.IASNonSharedResou
rcePool.<init>(IASNonSharedResourcePool.java:250)
INFO: CORE3282: stdout: at com.sun.enterprise.resource.PoolManagerImpl.g
etResourceFromPool(PoolManagerImpl.java:221)
INFO: CORE3282: stdout: at com.sun.enterprise.resource.PoolManagerImpl.g
etResource(PoolManagerImpl.java:170)
INFO: CORE3282: stdout: at com.sun.enterprise.resource.JdbcDataSource.in
ternalGetConnection(JdbcDataSource.java:241)
INFO: CORE3282: stdout: at com.sun.enterprise.resource.JdbcDataSource.ge
tConnection(JdbcDataSource.java:124)
INFO: CORE3282: stdout: at sa.com.aramco.mso.pams.jndiconnectionprovider
.JndiConnectionProvider.getConnection(JndiConnectionProvider.java:41)
INFO: CORE3282: stdout: at sa.com.futureware.fastwork.fastdbconnectionma
nagement.objects.DbConnectionManager.getConnection(DbConnectionManager.java:36)
INFO: CORE3282: stdout: at sa.com.aramco.ms.pams.utilities.RDBProvider.g
1) You have specified DataSource class name as
sun.jdbc.odbc.JdbcOdbcDriver which is not correct.
2) Can you post the connection pool configuration.
3) Documentation to create a connection pool against specific drivers (just to give an idea of essential properties that need to be configured)
http://docs.sun.com/app/docs/doc/819-2556/6n4rap8sl?a=view#beana
4) Documentation on Jdbc-Odbc Data Source :
sun.jdbc.odbc.ee.DataSource, sun.jdbc.odbc.ee.ConnectionPoolDataSource
http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/bridge.html
Choose your data source from (4) and create the connection pool.
Please post the server.log and pool configuration that you are trying to create if you still face the problems.
Thanks,
-Jagadish