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]
# 1

I have been in a similar situation myself.

We're using JBoss and Tomcat together, but it should be pretty similar.

the first thing i would suggest is trying to use the following datasource.

java:/comp/env/jdbc/myDB

notice the java:/

that's the kicker!! it had me stumped for nearly 3 days once.

let me know if this works out for you!

wireframe at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
I tried this but unfortunately it didn't help.Maybe you can remember something else.Thanks anyway I am tired to be alone with this problem.Qand
qand at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

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.

wireframe at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

I put this piece of code into my JSP-page:

NamingEnumeration ne=ic.list("java:/comp/env/jdbc/");

while(ne.hasMore()) {

out.println(ne.next());

}

and the output is:

myDB: org.apache.naming.ResourceRef

so Tomcat can find myDB but it can't create DataSource object for some reason.

Qand

qand at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5
Did you solve this problem? I seem to have hit the same issue so any suggestions would be great!Ed
MerciaOnline at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6
I have the same problem.best wishesIvan
crosignani at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7

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

jmelichn at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 8

Hi,

I'm in the same situation, that Tomcat can find the database but doesn't create the DataSource. Did you find a solution to this?

I think it's a good idea when you solve a problem to go back to your post and write the answer!!!

Hope someone can help - have been stuck on this for ages!

Louise.

louiseOConnor at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...