tomcat oracle 数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1916758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:41:25  来源:igfitidea点击:

tomcat oracle datasource

javaoracletomcatjndi

提问by user229432

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.

我的 Windows 机器上有 apache tomcat 5.5.28,我正在尝试部署一个在其他服务器上运行良好的 Web 应用程序 (WAR)。

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.

这是我在 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

我如何在 web.xml 中访问它,通常我在其他服务器上工作的地方是

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

回答by BalusC

If exception tells that it cannot find jdbcin the JNDI context, then it roughly means that you tried to obtain the DataSourceas follows

如果异常告诉它jdbc在JNDI上下文中找不到,那么大致意味着您尝试获取DataSource如下

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

while your server.xmlfile tells the following:

而您的server.xml文件告诉以下内容:

<Resource
     name="tomdb11"
     >

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

那些名字一样。其实你应该已经用过了:

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

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

但是,在 Tomcat 中,InitialContext不直接指向java:comp/env/,因此您需要将其替换为:

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

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

但是,通常的做法是使用jdbc前缀指定 JDBC 数据源。所以我将资源重命名为

<Resource
     name="jdbc/tomdb11"
     >

and access it by

并通过

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

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

但是,在 webapp 中,web.xml您还应该具有以下资源声明:

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

有关 Tomcat JNDI 的更多详细信息,请查看此 HOWTO:http: //tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html。希望这可以帮助。

回答by dacracot

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

首先...确保您的 $TOMCAT_HOME/common/lib 中有一个 Oracle JDBC Jar。

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

其次...确保您的 web.xml 也包含这样的块...

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

至于你的<context-param>,我不确定这是否在做任何事情,因为你已经在你的<Resource>.