java.net.SocketTimeoutException:使用jdbc连接到postgresql时连接超时

时间:2017-11-21 21:11:11

标签: postgresql jdbc socket-timeout-exception

我们计划将DB从Oracle切换到PostgreSQL。创建表后,我即将将实际数据加载到PostgreSQL表中。为了做到这一点,我使用JDBC连接到db。它给我的错误如下。

Connection Failed! Check output console
org.postgresql.util.PSQLException: The connection attempt failed.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:275)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
        at org.postgresql.Driver.makeConnection(Driver.java:450)
        at org.postgresql.Driver.connect(Driver.java:252)
        at java.sql.DriverManager.getConnection(DriverManager.java:664)
        at java.sql.DriverManager.getConnection(DriverManager.java:247)
        at JDBCExample.main(JDBCExample.java:31)
Caused by: java.net.SocketTimeoutException: connect timed out
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at org.postgresql.core.PGStream.<init>(PGStream.java:68)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
        ... 7 more

起初我认为数据加载可能需要一些时间并且可能会导致这种问题,但我测试了以下JDBC连接java。它仍然会返回与上面相同的错误。

public class JDBCExample {

        public static void main(String[] argv) {

                System.out.println("-------- PostgreSQL "
                                + "JDBC Connection Testing ------------");

                try {

                        Class.forName("org.postgresql.Driver");

                } catch (ClassNotFoundException e) {

                        System.out.println("Where is your PostgreSQL JDBC Driver? "
                                        + "Include in your library path!");
                        e.printStackTrace();
                        return;

                }

                System.out.println("PostgreSQL JDBC Driver Registered!");

                Connection connection = null;

                try {

                        connection = DriverManager.getConnection(
                                        "jdbc:postgresql://aaa.bbb.ccc.ddd:5432/test", "tom",
                                        "secret");

                } catch (SQLException e) {

                        System.out.println("Connection Failed! Check output console");
                        e.printStackTrace();
                        return;

                }
                catch (Exception e) {
                        e.printStackTrace();
                }

                if (connection != null) {
                        System.out.println("You made it, take control your database now!");
                } else {
                        System.out.println("Failed to make connection!");
                }
        }
}

在这种情况下,我应该先检查一下?在Postgresql中有一些日志文件记录这些错误?这与防火墙有关吗?我甚至不知道我们的公司有防火墙?你能否为这个问题提供一些指导?

0 个答案:

没有答案