Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does jdbcTemplate close resultsets?

I have a spring application where its home page fire multiple ajax calls which in turn fetch data from the DB and return back. The DB has been configured with connection pooling with minPoolSize as 50 and maxPoolSize as 100.

now when I open the home page, around 7 connections are established with the DB, which is expected as around 7 ajax calls are made and I assume all create their own connection. Now when I refresh the page, I see 7 more new connection are established (I see total 14 physical connections from db2 monitoring), which seems to be unexpected, as I assume jdbcTemplate do close the connection after the query from the first access and refresh in this case should reuse the connections ?

Now question here is does resultsets are also getting closed by jdbcTemplate along with connection close ? or Do i need to explicitly close the resultSet so that connection can be closed automatically. Opened resultSet may be a reason of connection not getting close ? Attaching the code for connection pooling configuration

<dataSource jdbcDriverRef="db2-driver" jndiName="jdbc/dashDB-Development" transactional="true" type="javax.sql.DataSource">
<properties.db2.jcc databaseName="BLUDB" id="db2-dashDB-Development-props" password="********" portNumber="*****" serverName="*********" sslConnection="false" user="*****"/>
<connectionManager id="db2-DashDB-Development-conMgr" maxPoolSize="100" minPoolSize="50" numConnectionsPerThreadLocal="2"/>

My initial theory was that the reuse of the connection will happen only when minPoolSize is reached and till that time it will always create new physical connection. HOwever I see this behavior even after reaching that limit. I refreshed my page 10 time and I see 70 physical connections. Now my only doubt is that connection are somehow are not getting close and spring is seeing those connection busy ? This may be because resultsets are not closed or some other reason ? Is it a way to say jdbctemplate not to wait for closing resultset beyond a time limit ?

Thanks Manoj

like image 377
Manoj K Sardana Avatar asked Jan 10 '17 10:01

Manoj K Sardana


People also ask

Is JdbcTemplate auto close connection?

In short yes it does close the connection.

Does JdbcTemplate use connection pooling?

Spring Example JDBC Database Connection Pool JdbcTemplate 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.

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 hibernate?

It's not. 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.

Does jdbctemplate call the close () method on the connection?

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.

How to read resultset from resultset in jdbctemplate?

After the execution of the query, ResultSet can be read with a ResultSetExtractor. ResultSetExtractor interface is a functional interface used by JdbcTemplate’s query method.

How does jdbctemplate handle SQLException?

Its the JdbcTemplate which is getting the connection using the DataSource provided to it, creating and executing the statement and closing the connection. If there is any SQLException thrown that is also caught by JdbcTemplate and translated to one of the DataAccessException and rethrown.

What is jdbctemplate query () used for?

Most of the cases JdbcTemplate query () is used to run the sql query and get multiple rows results from database. To run query () we need to follow 3 steps.


1 Answers

If you look at the org.springframework.jdbc.core.JdbcTemplate.query method source code you see calls to -

JdbcUtils.closeResultSet(rs);

In the finally blocks - so yes JDBCTemplate does call rs.close

The template also closes or returns the connection to the pool

like image 132
farrellmr Avatar answered Oct 07 '22 02:10

farrellmr