Rami Del Toro
Rami Del Toro

Reputation: 1150

Timeout Database connection in Java

What options are available in Java to timeout a database connection at a specific period of time?

For example, an application creates a database connection from a data source, then it executes a SQL query which might take 5,15 or even over 60 minutes to return results. This behavior is causing other issues.

What I am asking is regarding this scenario, how can we have the application make the database call and after 15 minutes if the query is still running, then stop query, close connection and continue with the application execution?

Thank you

Upvotes: 0

Views: 2654

Answers (1)

AlexR
AlexR

Reputation: 115388

This can be achieved by setting transaction timeout. Exception will be thrown if transaction exceeds the preconfigured timeout.

The transaction timeout configuration depends on your environment but it is easy to find instraction for each application server or framework. For example in Spring this can be controlled by attribute of annotation @Transactional (documentation link).

Upvotes: 2

Related Questions