5
votes

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

1
The connection pool will be managed separately from the JDBCTemplate - you can see this in your connectionManager config. Also the template could call close - but the underlying connectionmanager may leave the connection open for other pool users to use - farrellmr
I agree and i understand that.. But at this time I see connection not getting reused which means its not going back to pool ? if that is true, it means jdbcTemplate is not calling close ? If its getting back to pool, refresh should reuse them from the pool instead connectionManager asking for new connections. - Manoj K Sardana
You dont have control over the operation of the pool - so it can decide how best to allocate a pool strategy based on your config parameters - farrellmr
my subject line question still remain open. Does jdbcTemplate close resultsets and other objects (other than connection). If yes what is the process, algorithm ? - Manoj K Sardana

1 Answers

6
votes

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