开发者

tomcat oracle datasource

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) 


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.


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>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜