【问题标题】:Getting Null for JDBC connection为 JDBC 连接获取 Null
【发布时间】:2015-02-08 05:18:31
【问题描述】:

我在我的 Windows 上运行 tomcat 7.0.47 并且我有 Mysql 数据库连接到存储在 BlueHost 中的数据。当我在本地运行它时,它成功运行并连接到 BlueHost 数据库而没有任何错误。但是当我尝试在运行 tomcat 7.0.42 的 linux 环境中部署它的 war 文件,它给了我以下错误:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' 

另外,我已将 tomcat.dbcp jar 导入到在线服务器,因为 linux 对此 jar 有一些问题。

Also, Server is working if I donot follow connection pooling approach .

我无法找出池化可能有什么问题?

以下是我的context.xml 文件:

  <Context antiJARLocking="true" path="/web_app">
   <Resource 
              name="jdbc/DB" 
              auth="Container" 
              type="javax.sql.DataSource" 
              maxActive="100" 
              maxIdle="30"  
              maxWait="10000"
              username="DBUser" 
              password="DBPassword"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://xx.xx.xx.xx:xx/DB_app?useUnicode=true&#38;characterEncoding=UTF-8"/>
     </Context>

web.xml:

<resource-ref>
    <description>MySQL Datasource</description>
    <res-ref-name>jdbc/DB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

DatabaseConnection.java:

 public class Get_Database_Connection 
{

     static DataSource ds;
     public static Connection con=null;
     public static Connection get_DB_Connection() throws NamingException,SQLException
     {
           if(ds==null)
            {

            Context ctx = new InitialContext();
            ds = (DataSource) ctx.lookup("java:comp/env/jdbc/DB");  

           return ds.getConnection();
          }
            else
            {
            return ds.getConnection();
            } 
    }
}

【问题讨论】:

  • 听起来你的linux环境配置不正确。
  • @ElliottFrisch 你能指定我应该在 linux 环境中查找的确切位置吗?因为我已经添加了所需的罐子。
  • @Kanchan,linux box 的 ip 是否被列入 bluehost 的 MySQL 访问白名单?
  • @Sridhar 是的,它已被列入白名单
  • @Kanchan ,检查日志文件夹中tomcat的catalina.out文件可能会为您提供一些线索。

标签: java mysql tomcat jdbc


【解决方案1】:

基于 Tomcat 的JDBC Connection Pool 使用连接池,你需要配置工厂属性,所以这里是你更新的 context.xml 配置文件:

<Context antiJARLocking="true" path="/web_app">
 <Resource name="jdbc/DB"
      auth="Container"
      type="javax.sql.DataSource"
      factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
      maxActive="100"
      maxIdle="30"
      maxWait="10000"
      username="yourUsername"
      password="password"
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://xx.xx.xx.xx:xx/DB_app?useUnicode=true&#38;characterEncoding=UTF-8"/>
</Context>

【讨论】:

  • 嗨。我已经尝试过了,但是 linux 环境下的 tomcat 忽略了 context.xml 文件。
猜你喜欢
  • 2012-07-27
  • 2017-11-09
  • 2015-03-25
  • 1970-01-01
  • 2012-11-04
  • 2017-08-02
  • 2019-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多