4

I am running tomcat 7.0.47 on my windows and there I am having the Mysql database connection to the data stored at BlueHost .When I run it locally it runs successully and makes conenction to the BlueHost database without any error .But when I try to deploy its war file at the linux environment running tomcat 7.0.42 it gives me following error:

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

Also , I have imported the tomcat.dbcp jar to the online server as linux has some issues with this jar .

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

I am not able to find out What can be the issue with pooling ?

Following is my context.xml file:

  <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();
            } 
    }
}
jmh
  • 8,856
  • 1
  • 18
  • 26
WannaBeGeek
  • 979
  • 14
  • 32
  • It sounds like your linux environment isn't configured correctly. – Elliott Frisch Dec 10 '14 at 05:24
  • @ElliottFrisch Can you specify the where exactly I should look for in the linux environment . As I have added the jar required. – WannaBeGeek Dec 10 '14 at 05:25
  • @Kanchan, is linux box's ip whitelisted for bluehost's MySQL access? – Sridhar Dec 10 '14 at 05:40
  • @Sridhar yes it is whitelisted – WannaBeGeek Dec 10 '14 at 06:01
  • @Kanchan , examining tomcat's catalina.out file in logs folder might provide you some clue. – Sridhar Dec 10 '14 at 06:12
  • @Sridhar It is showing the error that I have written in the post . – WannaBeGeek Dec 10 '14 at 06:15
  • @Kanchan at connection URL `...e=true&ch...` is `#38;` exist? or it's just a typo here in the question? – Yazan Dec 10 '14 at 10:36
  • It is there in my code @Yazan – WannaBeGeek Dec 10 '14 at 10:45
  • @Kanchan then you have to remove it :) not sure if this is the reason, but it's definitely not correct and should be removed – Yazan Dec 10 '14 at 10:46
  • But that url is working when i try to fetch the data without using connection pooling @Yazan – WannaBeGeek Dec 10 '14 at 10:53
  • well, i told you i am not sure if this is the reason, maybe the pooling rejects the URL or whatever happens, my note that #38; should not be in the URL, you want to consider this note, or not, it's up to you. – Yazan Dec 10 '14 at 10:54
  • it is giving me error if i remove it @Yazan – WannaBeGeek Dec 10 '14 at 11:01
  • Since the context.xml you provided works with the provided version of Tomcat, my suspicion is that the Bluehost environment is either using a different version of Tomcat or a different version of context.xml. My instinct is the latter. – Brandon Dec 25 '14 at 06:28
  • (1) That `&` can be safely - and more clearly - replaced with `&`. (2) Do you have command line access to that linux machine? (3) When you said your IP is whitelisted, did you mean its internal IP (the one displayed with `ifconfig`) or its external one (one displayed with services like whatismyip.com)? If you are behind a firewall doing NAT the two may not be the same. – RealSkeptic Dec 29 '14 at 10:54
  • your `context.xml` is normally deployed to `{catalina-root}/conf/Catalina/localhost/{webapp-name}.xml`. Can you check if that file contains what you expect it to contain? It may be write-protected or whatever. – geert3 Dec 29 '14 at 10:55
  • @RealSkeptic 1) ok, I will try with that . 2) I donot have command line access to the linux machine. 3) My domain name is whitelisted. Its not internal config IP . – WannaBeGeek Dec 29 '14 at 11:00
  • @geert3 the file is not being created as expected. – WannaBeGeek Dec 29 '14 at 11:03
  • @BalusC In catalina.out file , there is just : org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' sql exception:java.sql.SQLException: Error establishing connection! – WannaBeGeek Dec 29 '14 at 11:46
  • There are no " root cause " or " caused by " parts – WannaBeGeek Dec 29 '14 at 11:49
  • 1) META_INF/context.xml . 2) WEB_INF/lib/mysql-connector.jar – WannaBeGeek Dec 29 '14 at 12:20
  • @BalusC I tried it . But its giving same error . – WannaBeGeek Dec 30 '14 at 04:12
  • @BalusC Thanks for the information .I will try to figure out more information about the root cause. – WannaBeGeek Dec 30 '14 at 09:45
  • @Kanchan i just came by this again, i had this idea, what about the permissions of `context.xml`,`META_INF` and `mysql-connector.jar`? try to give 777 for those,, who knows ... and and the owner too, if it's only root maybe Tomcat process can't access?! – Yazan Dec 31 '14 at 10:05
  • @Yazan ok , I will try it . – WannaBeGeek Dec 31 '14 at 10:16
  • @Kanchan may be this will be help ful only change the db from derby to mysql [and other url changes](http://stackoverflow.com/a/11588580/2749470) – Bhargav Modi Dec 31 '14 at 11:02
  • @BhargavModi I have gone through that link already . that is not helpful to me . – WannaBeGeek Dec 31 '14 at 11:46
  • You said: "as linux has some issues with this jar" could you elaborate on what those issues were? Also, what version of this jar are you using? I suspect that other jars (maybe Apache Commons Pool) are required. How are you building this war? Either maven or gradle would help with such dependency management – JoeG Dec 31 '14 at 21:43
  • @JoeG packaging for tomcat7 lacks the tomcat-dbcp.jar file. I am using tomcat.dbcp 7.0.42 jar. I have my project in netbeans and it uses Ant to build the war . – WannaBeGeek Jan 01 '15 at 04:58
  • If you found the answer please post it here.. – Akshatha S R Jul 13 '17 at 06:08

1 Answers1

2

Based on Tomcat's JDBC Connection Pool to use connection pool you are required to configure factory attribute, so here is your updated context.xml configuration file:

<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>
Dawud Tan
  • 35
  • 1
  • 7