How to get current Connection object used by jdbcTemplate
Asked Answered
G

1

8

I am looking for a way to intercept the connection that JDBCTemplate creates internally i.e. the connection that is created when the function getConnection() is called by JDBCTemplate.

ex: if I use jdbcTemplate.update(query); I want to get the information of the connection that was used to complete this update statement. Is there a way to see the metadata of the connection mid or post execution of this statement ? I am using C3P0 connection pool.

Many people have suggested using DataSourceUtils.getConnection() , but that just fetches a new connection from the pool and does not solve my issue.

This thread also effectively asks the same question: How to get current Connection object in Spring JDBC

Gleiwitz answered 23/6, 2017 at 5:16 Comment(1)
G
6
jdbcTemplate.getDataSource().getConnection();

By using the above line we can fetch the connection object.

Gawen answered 9/7, 2019 at 2:10 Comment(2)
that would return a connection, but would that be the same connection that the jdbcTemplate uses for its next operation?Clabber
If you get a connection in this way, it will generate a connection leak, so you have to close it manually. If you use DataSourceUtils.getConnection() and you are in spring transaction context you will get a same transaction as when you call jdbcTemplate.Winterkill

© 2022 - 2024 — McMap. All rights reserved.