0

I have a legacy Java based web application which used Oracle 7. Now I'm migrating Oracle 7 to PostgreSQL. We had normal JDBC connectivity with Oracle as we didn't use any framework like Hibernate, iBATIS etc so we created connection, connection pool programmatically and used that. It works fine for Oracle DB.

But as part of migration, when I am changing with PostgreSQL details and trying to connect the DB with my application. At the first DB call only, connection is getting lost and in the logs I am seeing SQL:08003 issue. Since it is web based application so we are creating war and deploying it into apache-tomcat-8.5.50 server. PostgreSQL version is postgresql-42.2.18.jar.

I checked many blogs, but I'm not able to figure it out why this issue is occurring.

Code:

public DBConnection(String dbUrl, String user, char[] pwd) throws SQLException {
    String errMsg = "Failed to get connection for login: " + user + " in URL = " + dbUrl;           
    try {
        // Connect to database
      mTheConnection = DriverManager.getConnection(dbUrl, user, new String(pwd));
        if (mTheConnection == null) {
            throw new SQLException (errMsg);
        }   
        mTheConnection.setAutoCommit(false);
    } catch (SQLException x) {
        throw new SQLException(errMsg);
    }
    finally{
        mTheConnection.close();
    }
}
2
  • 2
    Without the Java code, the corresponding complete error message and ideally the stacktrace this is impossible to answer. Commented Jan 5, 2021 at 13:47
  • Here is the code: public DBConnection(String dbUrl, String user, char[] pwd) throws SQLException { String errMsg = "Failed to get connection for login: " + user + " in URL = " + dbUrl; try { // Connect to database mTheConnection = DriverManager.getConnection(dbUrl, user, new String(pwd)); if (mTheConnection == null) { throw new SQLException (errMsg); } mTheConnection.setAutoCommit(false); } catch (SQLException x) { throw new SQLException(errMsg); } finally{ mTheConnection.close(); } } Commented Jan 5, 2021 at 13:58

1 Answer 1

2

This code obviously can never work - you close the connection in the finally block. This constructor cannot possibly complete with a functioning connection.

Note also that something like this:

} catch (SQLException x) {
    throw new SQLException(errMsg);
}

is in a word silly. You're taking perfectly useful information and throwing it out, replacing it with completely useless information. If you want to add user and URL info to an exception (I wouldn't here, it's unlikely to be particularly pertinent), include the exception you are wrapping as a cause (new SQLException(msg, x)), so that you can see the data there.

SQLException contains a ton of useful info. By throwing out x, there is no way for you to see all this useful information.

we created connection, connection pool programmatically & used that.

The above code isn't a connection pool.

DBConnection

Java convention says you write DbConnection. Also, that you don't hungary-notation your fields (it's connection, not mTheConnection).

if (mTheConnection == null) {

This can't ever happen; it's dead code. getConnection cannot return null. Of course, in the unlikely case that this does happen, your code throws that SQLException, thus control moves to the finally block, where close() is invoked on a null reference, which throws an NPE, which overwrites your SQLException(errMsg). This code is a mess.

My advice? Toss it all out. Get HikariCP and use that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.