1

I am fetching the connection in jdbctemplate in below fashion:-

getJdbcTemplate().getDataSource().getConnection()

Is it necessary to close the connection fetched in the above manner? The spring JDBCTemplate API states that connection closures will be handled automatically , so I am not sure if this is happening correctly.

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html

Ashoka
  • 935
  • 7
  • 20
Abhishek K
  • 645
  • 1
  • 11
  • 23
  • That code is wrong. Why are you doing that, when using `JdbcTemplate` you shouldn't be messing around with the connection yourself, use the template, you are working against it instead of using it. – M. Deinum Jan 07 '16 at 08:50
  • This is the old code and it has connection leakage issues which I need to debug. – Abhishek K Jan 07 '16 at 09:02
  • As stated the code is wrong. If you are messing around with the connection yourself then you need to close it. Either rewrite the code to correctly use the `JdbcTemplate` or do the old try/catch/finally for resource management which in turn beats the whole purpose of having a `JdbcTemplate` present at all. You aren't using the template ... – M. Deinum Jan 07 '16 at 09:17
  • For connection leaks it's the datasource , connection pool configurations that need to be looked into. Suspect the problem could be in that, as far closing connection programmatically via jdbcTemplate that may not be necessary. – Ashoka Jan 07 '16 at 09:20
  • No he isn't using the `JdbcTemplate`... He is having a highly complex way of obtaining the datasource to open a connection. Basically pening connections and thus you must manage it. – M. Deinum Jan 07 '16 at 09:51
  • Hi Deinum, I agree that the process used is complex and not correct, and after debugging i find your statement correct that if the connection is used in this fashion then we need to close it explicitly. – Abhishek K Jan 07 '16 at 10:21
  • Well after 12 years of spring, teaching and consulting spring and written books about it I kind of know what I'm saying :). But as stated your code is wrong and you shouldn't use it like that. Instead of getting the connection, wrap the code in a `ConnectionCallback` and call the `execute` method on the `JdbcTemplate` that way the template will take care of all complexity for you. – M. Deinum Jan 07 '16 at 10:38

1 Answers1

2

When you are obtaining the DataSource from the JdbcTemplate and use that to obtain a Connection you are basically completely bypassing the JdbcTemplate. You now have a very complex way of obtaining a new Connection. Because this connection isn't managed by Spring but yourself you also need to close it and apply exception handling.

It is better to use the ConnectionCallback instead to get a Connection. The JdbcTemplate will then manage the Connection and do all resource handling.

getJdbcTemplate().execute(new ConnectionCallback<Void>() {
    public Void doInConnection(Connection conn) {
        // Your JDBC code here.
    }
});

It would even better to use one of the other JdbcTemplate methods and write proper code which would save you from messing with plain JDBC code at all.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224