You can use OracleConnectionCacheManager to create a connection pool with a TimeToLiveTimeout so that any connection taken from the pool will be logically closed within that stipulated amount of time.
You can see his link for further reference:
This time out is usually not recommended as the implementation of this timeout will some times cause the JVM to hang.
If you want to do it completely in
java, you can use a process similar to this-
1. Get the connection and pass it to a runnable class and start the
thread.
2. In the thread, wait for two minutes and close the connection.
3. After starting the thread, call the method that executes the query. Pass the same connection to the method.
4. If in 2 mintues the method that executes the query is not completed, the connection is closed and you will get an SQLExcetption in the method. You can handle it as you like.
5. If the execution is completed, closing the connection that is already closed does not matter.
Hope it helps.