Reputation: 1036
In my application, we are trying to get java.sql.Connection from Spring,
Connection conn = DataSourceUtils.getConnection(this.getJdbcTemplate().getDataSource());
I use this connection object to set the prepare statment and execute query to get result set.
I am aware of the concept that when we are using jdbcTemplate, the burden of opening the connection, closing the connection etc is taken by spring itself and we don't need to handle in the code explicitly.
But I am little bit confused in this case, Since we get the connection object explicitly from the jdbcTemplate, do we need to handle the connection closing also in the code explicitly ? We are using a connection pool.
Thanks in advance.
BD
Upvotes: 0
Views: 2706
Reputation: 5209
As stated above depending on how the connection is obtained you will either close the connection or return it to the pool.
However as you are using JbbcTemplate there is no need for you to be dealing with connections at all as Spring will handle this for you.
You should define a row mapper to handle all the result set processing, and then just call the appropriate method on JdbcTemplate to excecute the statement for you. The Spring code will then
See Spring documentation for JdbcTemplate which explains in detail what you have to do and what Spring does for you.
Upvotes: 1
Reputation: 1685
It depends on what kind of datasource you have configured (in your case its a datasource configured with connection pooling) so the same datasource is used by DatasourceUtils getConnection and hence the Connection object which you create will be pooled meaning if you call close on this connection object the connection wont be actually closed but instead will be returned to the connection pool
Upvotes: 1