3

我有一个应该 24/7 运行的多线程程序,有时我会看到一些线程在程序运行一段时间后会挂起,没有异常或错误。它只是不会做任何事情。我运行了一个线程转储以查看问题所在,这是未执行任何操作的线程的相关线程转储

"Thread2" prio=6 tid=0x0000000014814000 nid=0xbf8 runnable [0x00000000159fe000]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:143)
        at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:112)
        at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:71)
        at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:272)
        at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:269)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:106)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
        at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:123)
        at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:28)
        at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:20)
        at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
        at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:22)
        at org.postgresql.Driver.makeConnection(Driver.java:391)
        at org.postgresql.Driver.connect(Driver.java:265)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at database.Database.isItInDatabase(Database.java:1108)
        at miner.Miner.run(Miner.java:145)
        at java.lang.Thread.run(Unknown Source)

以下是数据库方法

public Boolean isItInDatabase(String userName) throws SQLException
{
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    Boolean exists = false;

    try
    {
        Class.forName("org.postgresql.Driver");
        conn = DriverManager.getConnection("jdbc:postgresql://MYSERVERIP:5432/" + database, username, password); //--->>>>>>line 1108
        pst = conn.prepareStatement("SELECT COUNT(*) FROM listing_info where listing_url = ?");
        pst.setString(1, userName);

        rs = pst.executeQuery();
        while (rs.next())
        {
            exists = rs.getBoolean(1);
        }

    }
    catch (Exception e)
    {
        System.out.println(e);
    }
    finally
    {
        pst.close();
        rs.close();
        conn.close();
    }

    return exists;
}

我已经标记了我的数据库文件中第 1108 行的位置。我能做些什么来解决这个问题吗?所以程序不会永远等在那里吗?

4

1 回答 1

5

这与数据库无关。那是一个挂起的套接字连接。它发生在套接字连接断开时。阻止它挂起的唯一方法是使用套接字读取超时。看起来有一些用于 postgresql 的jdbc 驱动程序配置属性可以解决这个问题。

于 2012-12-01T18:28:50.403 回答