简体   繁体   English

使用 JDBC 的 Postgresql 的连接问题

[英]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. 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.我们与 Oracle 有正常的 JDBC 连接,因为我们没有使用任何框架,如 Hibernate、iBATIS 等,因此我们以编程方式创建了连接、连接池并使用了它。 It works fine for Oracle DB.它适用于 Oracle DB。

But as part of migration, when I am changing with PostgreSQL details and trying to connect the DB with my application.但作为迁移的一部分,当我更改 PostgreSQL 详细信息并尝试将数据库与我的应用程序连接时。 At the first DB call only, connection is getting lost and in the logs I am seeing SQL:08003 issue.仅在第一次 DB 调用时,连接丢失,在日志中我看到 SQL:08003 问题。 Since it is web based application so we are creating war and deploying it into apache-tomcat-8.5.50 server.由于它是基于 web 的应用程序,因此我们正在创建战争并将其部署到 apache-tomcat-8.5.50 服务器中。 PostgreSQL version is postgresql-42.2.18.jar. PostgreSQL 版本是 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.这段代码显然永远无法工作——你在finally块中关闭了连接。 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.如果您想将 user 和 URL 信息添加到异常中(我不会在这里,它不太可能特别相关),请将您包装的异常作为原因( new SQLException(msg, x) ),以便您可以看到那里的数据。

SQLException contains a ton of useful info. SQLException 包含大量有用的信息。 By throwing out x , there is no way for you to see all this useful information.通过丢弃x ,您将无法看到所有这些有用的信息。

we created connection, connection pool programmatically & used that.我们以编程方式创建了连接、连接池并使用了它。

The above code isn't a connection pool.上面的代码不是连接池。

DBConnection

Java convention says you write DbConnection . Java 约定说你写DbConnection Also, that you don't hungary-notation your fields (it's connection , not mTheConnection ).此外,您不会对您的字段进行匈牙利符号(它是connection ,而不是mTheConnection )。

if (mTheConnection == null) {

This can't ever happen;这永远不可能发生; it's dead code.这是死代码。 getConnection cannot return null. getConnection 无法返回 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).当然,在不太可能发生这种情况的情况下,您的代码会抛出 SQLException,因此控制会转移到 finally 块,在 null 引用上调用close() ,这会引发 NPE,它会覆盖您的 SQLException(errMsg)。 This code is a mess.这段代码是一团糟。

My advice?我的建议? Toss it all out.全部扔掉。 Get HikariCP and use that.获取HikariCP并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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