简体   繁体   中英

Connectivity issue with Postgresql using JDBC

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();
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM