Kshitiz Sharma Kshitiz Sharma - 1 month ago 9
Java Question

Database query hangs after some time

I have following database configuration in a Spring project -

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.jdbc.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.password}" />
<property name="maxActive" value="75" />
<property name="initialSize" value="10" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT 1" />
<property name="maxWait" value="10000"></property>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
<property name="dataSource">
<ref local="dataSource" />

The application runs for some time (30 mins to 2 hours, it varies) and after a while it starts to hang. i.e. The browser keeps on waiting for the server to respond. On checking the logs I found that the application is hung on a database query -

01/22 16:56:03 3024208 [http-bio-8080-exec-2] DEBUG DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT * FROM mytable where id = ?]

The connection to the database is proper as I'm able to interact with it and run queries from commandline or mysql workbench.

What could be the source of the problem? At this point I'm totally at a loss as to which direction I should look in, since no exception gets thrown and there is no stacktrace. What approach would you use to attack this problem?

Answer Source

Have you tried creating a thread dump at the moment that your application hangs? If you're on linux you could try to do:

$ kill -3 <pid>

The thread dump is written to the standard output. It may logged in the console or /logs/stdout or /logs/catalina.out.