java Tomcat 中 JNDI 数据源的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1826181/
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-10-29 18:08:37  来源:igfitidea点击:

trouble with JNDI Data Source in Tomcat

javatomcatdatasourcejndi

提问by mcgyver5

I've been having a hard time making a JNDI data source work. Following instructions at http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.htmlI'm connecting to oracle with Tomcat5.5 I can connect fine if I use straight JDBC connection in code.

我一直很难让 JNDI 数据源工作。按照http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html 上的说明 我正在使用 Tomcat5.5 连接到 oracle 如果我在代码中使用直接 JDBC 连接,我可以正常连接。

Here is what I have: in my META-INF/context.xml:

这是我所拥有的:在我的 META-INF/context.xml 中:

<Resource name="jdbc/mydb" auth="Container"
          type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
          url="jdbc:oracle:thin:theserver:1521/mydb"
          username="user" password="password" maxActive="20" maxIdle="10"
/>

here is what is in web.xml:

这是 web.xml 中的内容:

<resource-ref>
  <description>please work</description>
  <res-ref-name>jdbc/mydb</res-ref-name>
    <res-type>
    javax.sql.DataSource
    </res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

here is code:

这是代码:

   Connection conn = null;
    try{
    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/mydb");
    conn = ds.getConnection();
    } catch ....... etc.

I've tried many different configurations and started a new, simple project to ensure that no extra jar files conflicted or anything like that, but .

我尝试了许多不同的配置并开始了一个新的简单项目,以确保没有额外的 jar 文件冲突或类似的东西,但是 .

can anyone see anything that doesn't look right?

任何人都可以看到任何看起来不正确的东西吗?

the error on the server indicates a NullPointerException when I attempt to use the conn object. excuse me, it first offers: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Io exception: The Network Adapter could not establish the connection)

当我尝试使用 conn 对象时,服务器上的错误指示 NullPointerException。对不起,它首先提供:org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法创建 PoolableConnectionFactory(Io 异常:网络适配器无法建立连接)

回答by Sylar

The database url seems broken to me.

数据库 url 对我来说似乎坏了。

try:

尝试:

jdbc:oracle:thin:@theserver:1521/mydb
jdbc:oracle:thin:@theserver:1521/mydb

回答by Sean Owen

"The Network Adapter could not establish the connection"

“网络适配器无法建立连接”

This is your clue. It cannot reach the database. Check your server and port are correct, check they are reachable from your machine.

这是你的线索。它无法访问数据库。检查您的服务器和端口是否正确,检查它们是否可以从您的机器访问。

回答by prof401

I'm responding to comments in Sebastian answer.

我正在回应塞巴斯蒂安回答中的评论。

When I see org.apache.commons.dbcp, it means Tomcat is using the built-in Apache Commons Database Connection Pooling library instead of the library in the Oracle JDBC driver.

当我看到 org.apache.commons.dbcp 时,这意味着 Tomcat 正在使用内置的 Apache Commons 数据库连接池库,而不是 Oracle JDBC 驱动程序中的库。

The Oracle JDBC driver not being in the common directory is usually the first problem I try to solve. It appears this is not your problem.

Oracle JDBC 驱动程序不在公共目录中通常是我尝试解决的第一个问题。看来这不是你的问题。

Second, when the application starts, if there was a problem creating the JNDI datasource Tomcat may use the Commons library. This may be your case because of your wrong database URL. Correcting context.xml or the copied/renamed file in the conf/Catalina/localhost directory may not fix the problem without stopping and restarting Tomcat. So, I'd recommend stopping and restarting Tomcat and seeing if this corrects the problem.

其次,当应用程序启动时,如果在创建 JNDI 数据源时出现问题,Tomcat 可能会使用 Commons 库。这可能是您的情况,因为您的数据库 URL 错误。在不停止并重新启动 Tomcat 的情况下,更正 conf/Catalina/localhost 目录中的 context.xml 或复制/重命名的文件可能无法解决问题。所以,我建议停止并重新启动 Tomcat,看看这是否能解决问题。

One final note, the link you provide is for setting up the Common's library. It took me a while to figure this out. Below is an example of one of my JNDI Oracle datasource definitions.

最后一点,您提供的链接用于设置 Common 的库。我花了一段时间才弄清楚这一点。下面是我的 JNDI Oracle 数据源定义之一的示例。

  <Resource auth="Container" name="jdbc/mydb" 
    type="oracle.jdbc.xa.client.OracleXADataSource"
    driverClassName="oracle.jdbc.driver.OracleDriver"
    factory="oracle.jdbc.pool.OracleDataSourceFactory"
    url="jdbc:oracle:thin:@theserver:1521:mydb" 
    connectionCachingEnabled="true"
    connectionCacheProperties="{InactivityTimeout=1800,PropertyCheckInterval=300,MaxStatementsLimit=125,ValidateConnection=true}"
    implicitCachingEnabled="true"/>

I am not sure if the type matters in your case and I believe the user and password are still attributes of the Resource tag. What I want to point out is the attribute connectionCacheProperties. Here is where specify you specify the maximum and minimum connections. Go to Connection Cache Propertiesfor more info on this attribute.

我不确定类型对您的情况是否重要,我相信用户和密码仍然是 Resource 标签的属性。我想指出的是属性 connectionCacheProperties。这里是指定您指定最大和最小连接的地方。有关此属性的更多信息,请转至连接缓存属性。

Hope this helps.

希望这可以帮助。