No suitable driver found for Oracle Database connection
Asked Answered
U

12

14

I have small Java Application, which execute every day and checks for data in database using Cronj Schedular and everything works fine, but recently I have observed that, it is failing due to

java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.110.xx.xxx:1521/test

At the same time, when I run my test code to check Database connectivity that works fine without above exception. I'm unable to figure it out. Although, there was just slight code change, but that was nowhere related to Database or Database connection. Anyone, help me on this ?

dbconf.java

public class dbconf {

    private Connection connect;
    private String connstr;

    public Connection getConnection() throws SQLException {
        connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";

        try {
                String uname = "scott";
                String pass = "tiger";
                Class.forName("oracle.jdbc.OracleDriver").newInstance();
                connect = DriverManager.getConnection(connstr, uname, pass);

        } catch (Exception e) {
            System.out.println(e.toString());
        }

            return connect;
    }
}

I'm using ojdbc6.jar and Oracle11g

Edited - Application Log file

Wed Jul 01 09:25:17 IST 2015:------- Initializing -------------------
Wed Jul 01 09:25:17 IST 2015:------- Scheduling Jobs ----------------
Wed Jul 01 09:25:17 IST 2015:------- Job Started Running ----------------
Thu Jul 02 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.xxx.67.xxx:1521/test
Sat Jul 04 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2
Sun Jul 05 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.xxx.67.xxx:1521/test

So, you can see, It failed on 3rd and 6th July. But, in between it ran fine.

==Update 1==

It seems, nobody is reading my question properly, I have clearly mentioned that, It is running fine for someday, but someday it is failing. If it was classpath issue, then It shouldn't have ran any day.

===Update 2===

Many of below answers were pointless, but few were having some logical view. I have used printStracktrace and tried to debug each point and finally I got some clue. 3 Days back, I deployed new version of application on the same server (included printStackTrace and SysOut), First 2 days It ran fine, today it Failed with following error.

INFO: Illegal access: this web application instance has been stopped already.  Could not load com.schedular.job.BirthdayJob.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.quartz.simpl.LoadingLoaderClassLoadHelper.loadClass(LoadingLoaderClassLoadHelper.java:59)
    at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:99)
    at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:138)
    at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.selectJobDetail(StdJDBCDelegate.java:852)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTrigger(JobStoreSupport.java:2816)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport$40.execute(JobStoreSupport.java:2759)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport$40.execute(JobStoreSupport.java:2757)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3787)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTriggers(JobStoreSupport.java:2756)
    at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:272)

Jul 13, 2015 6:00:00 AM org.apache.catalina.loader.WebappClassLoader loadClass
INFO: Illegal access: this web application instance has been stopped already.  Could not load com.schedular.job.BirthdayJob.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.quartz.simpl.LoadingLoaderClassLoadHelper.loadClass(LoadingLoaderClassLoadHelper.java:59)
    at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:99)
    at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:138)
    at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.selectJobDetail(StdJDBCDelegate.java:852)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.retrieveJob(JobStoreSupport.java:1385)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.triggerFired(JobStoreSupport.java:2964)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport$43.execute(JobStoreSupport.java:2908)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport$43.execute(JobStoreSupport.java:2901)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3787)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.triggersFired(JobStoreSupport.java:2900)
    at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:336)
Ungula answered 25/6, 2015 at 2:52 Comment(15)
I think the question you should be asking is, what changed during the two instances where it failed. Generally I would assume that if it worked once, it will work the next ten times.Crocein
That is our assumption. But, It is not working. Even, I'm not getting any clue to track it. :(Ungula
Question :- The method which is shown in above code.. dbconf.java .. is it static ??... Suggestion :- Class.forName("oracle.jdbc.OracleDriver").newInstance() , newInstance() is not required here.. why you are using it??Complicity
@PranavManiar does it make any difference ?Ungula
@PranavManiar my code may not be efficient but not wrong. Since, It's working some days, which you can see in log, which I mentioned at the end of my question.Ungula
static method could make a difference.. if it is not properly synchronized.. and throw errors which is very hard to find in code.Complicity
@PranavManiar check my update. I responded on your edited comment. I said does it matter when you said you shouldn't have used newInstance() methodUngula
Let us continue this discussion in chat.Complicity
Did anybody notices that the IP is almost shown? xxx in the code is different from xxx in log. So IP is like 160.110.67.xxx :-)Gonorrhea
What jdk/jre are you using? Can you provider more information about "cronj schedular". Are all the log lines from the same process or does cronj launch a separate process for each task? Is anything else running concurrently?Ejective
There is only single task,which runs every day for one time (6 O'clock every morning) and I said one task means it calls one function. There are no other task running on that SERVER. This is the only jobs, which is currently running and failing for some day (Not all day).Ungula
The jdk and jre version are same 1.8Ungula
I suggest don't just print the exception's message, but log the stack trace as well. Also, I don't think you need that Class.forName("oracle.jdbc.OracleDriver").newInstance(); if you use java 1.7 or later.Oliviero
it looks like there is problem between classloader provided by JDK , which JDK version you are using also can you please check which is most compitable JDK version , also if possible check if there is any hierarchical dependency leading to another JDBC driver or another version of oracle driverSackbut
We could also refer #30861561Ungula
U
0

As someone approached me for the solution of this issue. I'm posting it now.

  1. I undeployed the application, which was having this issue and cleaned up all related files from the server.
  2. Then, I restarted the tomcat server. So that, it will flush up all temporary files and cache.
  3. Then, I deployed the same application and it started working without any issue.
Ungula answered 17/6, 2017 at 14:33 Comment(0)
P
1

Not sure if it helps, but this is the code I have to do the same thing,

    try { 
        Class.forName("oracle.jdbc.driver.OracleDriver"); 
    } catch (ClassNotFoundException e) { 
        System.out.println("Could not load the driver"); 
    } 

    Connection conn = DriverManager.getConnection                                     ("jdbc:oracle:thin:@ten10:1521:acdb", user, pass); 

So, not quite the same Class.forName, but same form for protocol.

The class for name is essential, it ensures that the class loader has loaded the Oracle jdbc driver.

What could be happening is some connectivity problem on the machine where the code is running, so that the location that contains the actual ojdbc6.jar (as pointed to on the classpath) is not always accessible (if it's not on a local disk ?).

Postmillennialism answered 7/7, 2015 at 14:23 Comment(0)
P
0

The correct format for your JDBC URL is not what you wrote:

connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";

but either

connstr = "jdbc:oracle:thin:@//160.110.xx.xxx:1521/test";

or

connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521:test";

depending on whether 'test' is a service or a SID.

The log fragment you showed doesn't show that the getConnection method worked on the 4th! It only showed there was no error thrown by it. This may just have meant that the method was never called (so no connection was attempted).

Paisa answered 7/7, 2015 at 13:59 Comment(1)
Then, what happened to 2nd and 3rd ? There was no change in code. Same method is getting invoked everyday in same manner.Ungula
E
0

It seems,The "ojdbc6.jar" is not in the CLASSPATH of your application server.

Eastertide answered 9/7, 2015 at 17:39 Comment(1)
Then.. why did it ran for some day.? It shouldn't run all day. right ? It's not at all logical answer.Ungula
G
0

When it says it can not find class it can not find the class.

From my experience, this sort of problems that sometimes work and sometime not are thread related. My assumption is ClassLoader is loading your class async, so calling connection just after loading it may be the problem. did u try loading oracle class in a static part? something like:

public class dbconf {

static {
  Class.forName("oracle.jdbc.OracleDriver");
}

public Connection getConnection() throws SQLException {
    String connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";
    try {
        String uname = "scott";
        String pass = "tiger";
        return DriverManager.getConnection(connstr, uname, pass);
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}
}

Another issue: Is your Code getting Compiled every day(By Continuous Delivery or ...)?

Gonorrhea answered 10/7, 2015 at 11:3 Comment(1)
No.It was compiled once and deployed on the server. Your point looks logical.Ungula
F
0

It seems issue is with your jar try to replace with ojdbc14.jar and add it to Class-path if you are using Eclipse follow below steps:- Eclipse --> (Select the Project)Go to Properties --> Java Build Path--> Choose Add Jar or Add External Jar.

Fourflush answered 10/7, 2015 at 13:21 Comment(0)
C
0

If possible, view the println for DriverManager.getConnection () method. You may be getting a null connection object from DB without any exception, during failures.

SQLException reason = null;
for(DriverInfo aDriver : registeredDrivers) {
    if(isDriverAllowed(aDriver.driver, callerCL)) {
        try {
            println("    trying " + aDriver.driver.getClass().getName());
            Connection con = aDriver.driver.connect(url, info);
            if (con != null) {
                println("getConnection returning " + aDriver.driver.getClass().getName());
                return (con);
            }
        } catch (SQLException ex) {
            if (reason == null) {
                reason = ex;
            }
        }

    } else {
        println("    skipping: " + aDriver.getClass().getName());
    }

}
if (reason != null)    {
    println("getConnection failed: " + reason);
    throw reason;
}
println("getConnection: no suitable driver found for "+ url);
throw new SQLException("No suitable driver found for "+ url, "08001");
Chkalov answered 11/7, 2015 at 17:52 Comment(0)
O
0

A possible solution is : Go to Event Viewer -> Windows Archives and delete Application Events and System Events.(DON'T delete Security Events!).After that restart your pc and you will be ok.

Overleap answered 12/7, 2015 at 11:16 Comment(1)
It's SERVER. I have mentioned it. I don't have privilege to restart it.Ungula
E
0

I'm unfamiliar with "schedular" but your latest update suggests that you have threads that weren't cleanly stopped from a previous undeploy/redeploy. There is a JavaSpecialists newsletter about how to shutdown threads cleanly.

I wonder if perhaps your servlet shutdown code unregisters the database driver? From your stacktrace it looks like you are running in Tomcat. Even if your code doesn't directly de-register the driver I believe Tomcat 7 and up will unregister drivers as part of Tomcat's memory leak detection/mitigation.

That might explain why the driver is sometimes present and sometimes not.

Ejective answered 16/7, 2015 at 17:58 Comment(0)
L
0

Don't keep the driver name in static way. Use the JDBC + Java API to get the driver class name like the following :

public class dbconf {

    private Connection connect;
    private String connstr;

    public Connection getConnection() throws SQLException {
        connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";

        try {
                String uname = "scott";
                String pass = "tiger";
                Class.forName(OracleDriver.class.getClass().getName().toString()).newInstance();
                connect = DriverManager.getConnection(connstr, uname, pass);

        } catch (Exception e) {
            System.out.println(e.toString());
        }

            return connect;
    }
}

It is better, if you made any typo or and you can check whether the ojdbc6.jar is set in the build path in a good way..

Hope this information helps...

Lorsung answered 27/8, 2015 at 7:1 Comment(0)
U
0

As someone approached me for the solution of this issue. I'm posting it now.

  1. I undeployed the application, which was having this issue and cleaned up all related files from the server.
  2. Then, I restarted the tomcat server. So that, it will flush up all temporary files and cache.
  3. Then, I deployed the same application and it started working without any issue.
Ungula answered 17/6, 2017 at 14:33 Comment(0)
S
0

Ensure the following things.

  • ojdbc Library is added to your project.
  • The library is added to your project class path.
  • Make sure your Connection Configuration such as URL, username and password are found properly while creating the DB connection.
Strohbehn answered 11/4, 2022 at 7:43 Comment(0)
P
-2

The error means driver you are using doesn't accept connection URL. It seems like your URL uses MySQL syntax (DB name separated with '/'). Try to use Oracle specific definition: jdbc:oracle:thin:@160.110.xx.xxx:1521:test

Pompadour answered 7/7, 2015 at 13:51 Comment(1)
both are correct for Oracle but have subtly different meaning. Use : if you have a SID, / if you have a servicename. That said, his URL is technically incorrect.Paisa

© 2022 - 2024 — McMap. All rights reserved.