title: Thread hangs on DriverManager.getConnection
body: <p>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</p>
  
  <pre><code>"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.&lt;init&gt;(AbstractJdbc2Connection.java:123)
          at org.postgresql.jdbc3.AbstractJdbc3Connection.&lt;init&gt;(AbstractJdbc3Connection.java:28)
          at org.postgresql.jdbc3g.AbstractJdbc3gConnection.&lt;init&gt;(AbstractJdbc3gConnection.java:20)
          at org.postgresql.jdbc4.AbstractJdbc4Connection.&lt;init&gt;(AbstractJdbc4Connection.java:30)
          at org.postgresql.jdbc4.Jdbc4Connection.&lt;init&gt;(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)
  </code></pre>
  
  <p>and below is the Database method</p>
  
  <pre><code>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); //---&gt;&gt;&gt;&gt;&gt;&gt;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;
  }
  </code></pre>
  
  <p>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?</p>