views:

632

answers:

2

I have apache tomcat 5.5.28 on my windows box and I am trying to deploy a web application (WAR) which works fine on other servers.

However I am having trouble creating a datasource. I am not sure of the format. The db is oracle.

Here is what I have in server.xml.

  <GlobalNamingResources>
    <Environment
      name="simpleValue"
      type="java.lang.Integer"
      value="30"/>
    <Resource
      name="tomdb11"
      type="oracle.jdbc.pool.OracleDataSource"
      maxActive="4"
      maxIdle="2"
      username="tomdb11"
      maxWait="5000"
      driverClassName="oracle.jdbc.driver.OracleDriver"
      validationQuery="select * from dual"
      password="remotedb11"
      url="jdbc:oracle:thin:@dbserver:1521:orcl"/>
    <Resource
      auth="Container"
      description="User database that can be updated and saved"
      name="UserDatabase"
      type="org.apache.catalina.UserDatabase"
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
      pathname="conf/tomcat-users.xml"/>
  </GlobalNamingResources>

How do I access this in the web.xml where usually what I have which works in other servers is

<context-param>
  <param-name>databaseUser</param-name>
  <param-value>tomdb11</param-value>
</context-param>

<context-param>
  <param-name>databasePassword</param-name>
  <param-value>tomdb11</param-value>
</context-param>

<context-param>
  <param-name>databaseSchema</param-name>
  <param-value>TOMDBADMINN11</param-value>
</context-param>

Also am I missing something?

Edit: I get the following exception:

javax.naming.NameNotFoundException: Name tomdb11 is not bound in this Context
    at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at com.taw.database.DatabaseService.<init>(DatabaseService.java:19)
    at com.taw.database.DatabaseServices.init(DatabaseServices.java:40)
+1  A: 

First... Make sure you have an Oracle JDBC Jar in your $TOMCAT_HOME/common/lib.

Second... Make sure your web.xml also contains a block like this...

<resource-ref>
    <description>Oracle Datasource</description>
    <res-ref-name>tomdb11</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
</resource-ref>

As for your <context-param>, I'm not sure that is doing anything as you already have those things defined in your <Resource>.

dacracot
trying this right now
i put the oralce jdbc jars int he common.lib directory and also included this resource -ref however, i don't see any exception just that the application specific exception shows up that it cannot connect to the database. Is my server.xml correct?
do i need ot sue the datasource int he format jdbc/tomdb11 instead of just tomdb11
i still see the eception javax.naming.NameNotFoundException: Name tomdb11 is not bound in this Context at org.apache.naming.NamingContext.lookup(NamingContext.java:770) at org.apache.naming.NamingContext.lookup(NamingContext.java:153) at org.apache.naming.SelectorContext.lookup(SelectorContext.java:137) at javax.naming.InitialContext.lookup(Unknown Source) at com.taw.database.DatabaseService.<init>(DatabaseService.java:19) at com.taw.database.DatabaseServices.init(DatabaseServices.java:40)
i see on the itnernet that if i use this format java:comp/env/tomdb11 it has fixed this error for other but using it does not fix it for me
in this link http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html it says that i also need to modify my context.xml?
I always use the jdbc/tomdb11 syntax, but I'm honestly not sure if it is required.
dacracot
A: 

If exception tells that it cannot find jdbc in the JNDI context, then it roughly means that you tried to obtain the DataSource as follows

dataSource = new InitialContext().lookup("jdbc/tomdb11");

while your server.xml file tells the following:

<Resource
     name="tomdb11"
     >

Those names are not the same. In fact, you should have been used:

dataSource = new InitialContext().lookup("tomdb11");

In Tomcat, however, the InitialContext doesn't directly point to java:comp/env/, so you'll need to replace it by:

dataSource = new InitialContext().lookup("java:comp/env/tomdb11");

The normal practice, however, is that you specify JDBC datasources with the jdbc prefix. So I would rename the resource as

<Resource
     name="jdbc/tomdb11"
     >

and access it by

dataSource = new InitialContext().lookup("java:comp/env/jdbc/tomdb11");

In the webapp's web.xml you should however also have the following resource declaration:

<resource-env-ref>
    <resource-env-ref-name>jdbc/tomdb11</resource-env-ref-name>
    <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>

For more details about Tomcat JNDI check this HOWTO: http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html. Hope this helps.

BalusC
after i did that change I get javax.naming.NamingException: Cannot create resource instance at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:143) at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source) at org.apache.naming.NamingContext.lookup(NamingContext.java:793)
You're one step further! Your code has finally found the resource, but the creation of the resource itself failed. Try looking a bit further in the stacktrace, don't you see a root cause? Possible causes are malformed XML syntax (one `"`, `<` or `>` missing or too much) or there's a typo in type, factory or pathname (all are case sensitive!). Let know if you found it. Use the linked HOWTO as reference.
BalusC