我有一个多线程程序应该全天候运行,有时我会看到一些线程在程序运行一段时间后才会挂起,没有异常或错误。它根本不会做任何事情。我运行了一个线程转储,看看问题是什么,这是没有做任何事情的线程的相关threaddump
"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行的位置。我有什么办法可以解决这个问题吗?所以程序不会永远等在那里吗?
答案 0 :(得分:5)
这与数据库无关。这是一个挂起的套接字连接。当套接字连接断开时会发生这种情况。阻止它挂起的唯一方法是使用套接字读取超时。看起来postgresql有一些jdbc driver config properties可以解决这个问题。