java - Tomcat JNDI : Data Source fails to initialize

标签 java tomcat database-connection jndi

常见问题,我知道,但我想不通。具有非常标准设置的 Tomcat 6.0.41,单主机。当我尝试从 DataSource 获取 JDBC 数据库连接时,出现以下错误:

org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
... Caused by: java.sql.SQLException: No suitable driver

我的 JDBC 驱动程序 jar 文件 (MySQL Connector/J) 位于 /usr/share/tomcat6/lib/mysql-connector-java-5.1.35-bin.jar.

这是我的 context.xml 文件:

<context>

  <Resource name="jdbc/first_db" 
        auth="Container" 
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="..." 
        password="..." 
        url="jdbc:mysql://my.host.com:3306/first_db"
        connectionProperties="verifyServerCertificate=false;useSSL=true;requireSSL=true"/>

  <Resource name="jdbc/second_db" 
        auth="Container" 
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="..." 
        password="..."
        url="jdbc:mysql://my.host.com:3306/second_db"
        connectionProperties="verifyServerCertificate=false;useSSL=true;requireSSL=true"/>

</context> 

我的 web.xml 中有这个:

<resource-ref>
    <description>First DB Connection</description>
    <res-ref-name>jdbc/first_db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

<resource-ref>
    <description>Second DB Connection</description>
    <res-ref-name>jdbc/second_db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

在我的代码中,我这样做:

Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/first_db");
Connection conn = dataSource.getConnection(); // error here

我尝试将 context.xml 放在以下位置(我的 webapp 根目录位于 /var/www):

  • /etc/tomcat6/Catalina/locahost/context.xml
  • /etc/tomcat6/Catalina/context.xml
  • /var/www/WEB-INF/context.xml
  • /var/www/WEB-INF/META-INF/context.xml
  • /var/www/META-INF/context.xml

我还尝试将上面显示的 context.xml 中的资源元素添加到位于 /etc/tomcat6/context.xml 的全局上下文配置中。我尝试过的所有操作都会导致相同的 JDBC 错误。

我刚刚将代码中的 JNDI 查找字符串更改为不存在的内容,我得到了这个:

javax.naming.NameNotFoundException: Name doesNotExist is not bound in this Context

...所以我通常不会看到此错误的事实一定意味着context.xml 文件正在 正在被发现和处理。所以这意味着 Tomcat 就是找不到 MySQL 驱动程序。但是在/usr/share/tomcat6/lib肯定是。我刚刚更新了驱动到最新版本(v5.1.35),但仍然报同样的错误。

我还刚刚从我的 WEB-INF/lib 目录中删除了 mysql 驱动程序 jar 文件,因为有些人说那里可能有问题 在tomcat系统lib目录下。问题仍然存在。

最佳答案

Tomcat lib 文件夹中是否有 mysql JDBC JAR?

查看此 related answer .可能是Tomcat无法加载驱动。

关于java - Tomcat JNDI : Data Source fails to initialize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088152/

相关文章:

java - 卡在 XPath 上

java - 确定方法是否为 `RemotableViewMethod`

sql - AZUR - 使用带有查询的 BCP 导出

java - 使用 Stripes、Guice 管理数据库连接

mysql - 外部系统:配置连接以访问 MySQL 数据库

安卓和数据库

java - 如何在Java中将一串含有不同数量空格的数字转换为一系列Int变量。

java - 泛型的细微差别

tomcat - 安全加固 : How to collect visitors' IP addresses in Grails Framework?

java - Tomcat JDBC 计数失败登录