DataSource is always null
Hi guys,
I need your help because the situation drives me crazy. I created web-application wich is connected with Oracle through DriverManager and it works well. Now I try to use DataSource but it's always null although there is no NamingException.
I simplified everything but it still doesn't work.
My simplified JSP-code looks like this:
Connection con;
String dbName = "java:comp/env/jdbc/myDB";
try {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
//con = ds.getConnection();
if(ds==null)
out.println("Datasource=null");
} catch (Exception ex) {
throw new Exception("Couldn't open connection to database: " + ex.getMessage());
}
This is my Tomcat configuration file fragment:
<Context path="/Leafs" docBase="Leafs" debug="0"
reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_Leafs_log." suffix=".txt"
timestamp="true"/>
<Environment name="maxExemptions" type="java.lang.Integer"
value="15"/>
<Parameter name="context.param.name" value="context.param.value"
override="false"/>
<Resource name="jdbc/myDB" auth="CONTAINER"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/myDB">
<parameter><name>user</name><value>sa</value></parameter>
<parameter><name>password</name><value>pw</value></parameter>
<parameter><name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value></parameter>
<parameter><name>driverName</name>
<value>jdbc:oracle:thin:@pdi.statestr.com:1521:myDB</value></parameter>
</ResourceParams>
</Context>
This is my web.xml:
<resource-ref>
<res-ref-name>jdbc/myDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Please help me I really need help.
Thank you in advance.
Qand
[2307 byte] By [
qand] at [2007-9-19]

I guess i'm not really sure how tomcat is hosting that datasource. it seems like you've got it set up right. the only thing i can think of, is if you can set your initial context with a properties file to get some more information back.
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
prop.put(Context.PROVIDER_URL, "localhost:1099");
Context ctx = new InitialContext(prop);
now, can you check to see if your context is getting set right?
you can actually loop through the context to see all of the bound values, i can't remember the code but it's out there on other threads.
i'm quite familiar with JBoss and tomcat's JNDI setup, but i haven't worked with solely tomcat.
does anyone else out there have experience with tomcat's datasources?
sorry i couldn't be of more help.
Hello,
i had the same problem. My configuration:
w2k sp3
jdk 1.4.0
tomcat 4.0
Oracle 8.1.7.3.2 EE
To solve this i hat to:
1) Expand %ORACLE_HOME%\jdbc\lib\classes12.zip to temp dir.
2) Delete the javax dir an all i's content from temp dir.
3) Jar driver in temp dir back to archiv with name classes12.jar.
4) Copy temp\classes12.jar to %CATALINA_HOME%\common\lib.
5) Copy %ORACLE_HOME%\jdbc\lib\nls_charset12.zip to %CATALINA_HOME%\common\lib.
6) Rename %CATALINA_HOME%\common\lib\nls_charset12.zip to %CATALINA_HOME%\common\lib\nls_charset12.jar
(not everybody needs nls_charset12.zip - see Oracle JDBC doc - I need it - my application is in czech language)
8) add to web.xml:
<web-app>
...
...
<resource-ref>
<description>Oracle db pro system EasyDoc</description>
<res-ref-name>jdbc/EdDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
9) modify server.xml:
<context ...>
...
...
<Resource name="jdbc/EdDS" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/EdDS">
<parameter>
<name>user</name>
<value>mydbusername</value>
</parameter>
<parameter>
<name>password</name>
<value>mypass</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>driverName</name>
<value>jdbc:oracle:thin:@dbserver:1521:sid</value>
</parameter>
</ResourceParams>
</context>
10) Using DataSource in JSP:
Context ic = new InitialContext();
Context ec = (Context) ic.lookup("java:comp/env");
DataSource ds = (DataSource) ec.lookup("jdbc/EdDS");
if (ds != null) {
....
}
That's everything. I hope that this help you.
JM