【发布时间】:2015-09-14 06:59:43
【问题描述】:
常见问题,我知道,但我想不通。 Tomcat 6.0.41 具有非常标准的设置,单主机。当我尝试从 DataSource 获取 JDBC 数据库连接时,我收到以下错误:
org.apache.commons.dbcp.SQLNestedException:无法创建 JDBC 驱动程序 连接 URL 的类“空”
... 原因:java.sql.SQLException:没有合适的驱动程序
我的 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:名称 doesNotExist 未绑定在此上下文中
...所以我通常看不到这个错误的事实一定意味着context.xml文件正在被发现并处理。所以这意味着 Tomcat 只是找不到 MySQL 驱动程序。但在/usr/share/tomcat6/lib 中肯定。我刚刚将驱动更新到最新版本(v5.1.35),但仍然报同样的错误。
我还刚刚从我的WEB-INF/lib 目录中删除了 mysql 驱动程序 jar 文件,因为有些人说在 tomcat 系统 lib 目录中和可能存在问题。但问题仍然存在。
【问题讨论】:
标签: java tomcat database-connection jndi