Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBCTemplate hangs

I am using JDBCTemplate. For one particular query JDBCTemplate hangs after calling more than 3-4 times, sometimes after 15-20 attempts. I haven't found any bug in my code.

private String getXXX(String table)
{
    System.out.println("Test 1");
    final List<String> data = getJdbcTemplate().queryForList(SELECT_TBL_NME,
                new Object[] { table }, String.class);
    System.out.println("Test 2");
    ret = data.size() > 0 ? data.get(0) : null;
    return ret;
}

Console hangs at "Test 1" without any error if the function is called more than 15 times or sometimes even after the 3rd or 4th call.

like image 889
Sanket Avatar asked May 26 '26 02:05

Sanket


1 Answers

The problem seems to be that you aren't closing the connections.

final List data = getJdbcTemplate().queryForList(SELECT_TBL_NME, new Object[] { table}, String.class);

The line above will never end because the JdbcTemplate is waiting to get a connection from your db.

So if your db cannot give you a connection your application is waiting, waiting, waiting and it does nothing with the time.

possible fix: Since you are using Spring, check that you aren't doing operation outside your queryForList method.

I had the same problem few months ago because I was calling the ResultSet.getMetaData() outside my queryForList method and that was't releasing/closing the connection.

like image 134
German Attanasio Avatar answered May 27 '26 14:05

German Attanasio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!