I have a multithreaded program that's supposed to run 24/7, sometimes I would see a few threads would just hang after some time the program runs, no exception or error. It just would not do anything. I ran a thread dump to see what the problem and this is the related threaddump for the thread which was not doing anything
"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)
and below is the Database method
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;
}
I have marked where line 1108 is in my Database file. Is there anything I can do to fix this issue? so the program does not wait there forever?
That has nothing to do with databases. that is a hung socket connection. it happens when the socket connection gets broken. the only way to stop it from hanging is to use socket read timeouts. looks like there are some jdbc driver config properties for postgresql which will fix the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With