Search code examples
javadatabasejdbcsybasedbconnection

Random behavior while loading jdbc driver class - No suitable driver found


I know this is a very common issue and we have similar posts in Stackoverflow for the same.

But I am facing same issue in very random order. I do not know how to fix it. Tried all solutions from this site but no luck

Here is my code

 private static Connection conn = null;
 private static String connDriverClass;
 private static String  connDbUrl;
 private static String  connDbUser;
 private static String  connDbPassword;

 private static Connection getConnection() throws  SQLException, ClassNotFoundException {
    connDriverClass = "com.sybase.jdbc3.jdbc.SybDriver";
    connDbUrl="jdbc:sybase:Tds:some.com:5000/db";
    connDbUser="user";
    connDbPassword="pass";

    if(conn !=null && !conn .isClosed()) { return conn ; }

    try {
        Class.forName(connDriverClass);
        viTechConn = DriverManager.getConnection(connDbUrl, connDbUser, connDbPassword);
    } catch (SQLException | ClassNotFoundException e) {
        LOG.error("Error in getting DB Connection "+e.getMessage());
        throw e;
    }
    return conn;
}

We run it in parallel processes.

It works fine sometimes and sometimes it gives error saying

java.sql.SQLException: No suitable driver found for jdbc:sybase:Tds:some.com:5000/db

Please tell me how to overcome this random behavior.

Thanks.


Solution

  • You can consider using a pooled data source, instead. Its a one time effort and will save a lot of debugging efforts in the future.

    Refer to the following class file implementation, on how to create and access a pooled data source.

    http://www.programcreek.com/java-api-examples/index.php?source_dir=jasperserver-master/5.5/com/jaspersoft/jasperserver/api/engine/jasperreports/service/impl/JdbcReportDataSourceServiceFactory.java