Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spring's JdbcTemplate close the connection after query timeout?

Tags:

I have set query timeout (getJdbcTemplate().setQueryTimeout(5)) in method with insert statement. What will happen after query timeout, does jdbc template close my connection?

like image 251
user3073662 Avatar asked Dec 06 '13 08:12

user3073662


People also ask

What is the default timeout for JdbcTemplate?

Set the query timeout for statements that this JdbcTemplate executes. Default is 0, indicating to use the JDBC driver's default. Note: Any timeout specified here will be overridden by the remaining transaction timeout when executing within a transaction that has a timeout specified at the transaction level.

Does JdbcTemplate use connection pooling?

Spring Example JDBC Database Connection PoolJdbcTemplate requires a DataSource which is javax. sql. DataSource implementation and you can get this directly using spring bean configuration or by using JNDI if you are using the J2EE web server or application server for managing Connection Pool.

Does spring JdbcTemplate automatically clean up resources?

The Spring JDBC Template has the following advantages compared with standard JDBC. The Spring JDBC template allows to clean-up the resources automatically, e.g. release the database connections.

Which is better JdbcTemplate or hibernate?

Hibernate makes a lot of assumptions and forces you to think and code in a certain way. Using JdbcTemplate is easier because it's just a very thin wrapper around JDBC itself. The price here is that you will write thousands of lines of really boring code. Also, you will find that SQL strings are really hard to maintain.


2 Answers

In short yes it does close the connection. The long answer it depends.

When you don't have a Spring managed transaction then yes the JdbcTemplate will call the close() method on the Connection. However if there was already a connection available due to Springs transaction management closing the connection will be handled by Springs transaction support, which in turn also will call close() on the Connection.

The only difference is when the connection is closed but close() will be called.

If the connection will be actually closed depends on which DataSource is used, in general when using a connection pool the connection will be returned to the pool instead of actually closing the connection.

like image 112
M. Deinum Avatar answered Sep 23 '22 02:09

M. Deinum


Yes it does.

And if the connection was obtained from connection pool, it won't actually close the connection, rather will send it back to the pool.

like image 34
Abhishek Anand Avatar answered Sep 27 '22 02:09

Abhishek Anand