简体   繁体   中英

How to get current Connection object used by jdbcTemplate

I am looking for a way to intercept the connection that JDBCTemplate creates internally ie 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

I am facing the same issue . I want to know the current connection the template is using to update or run the queries . I tested the below two ways but both of them is returning new connection every time and not the connection that is currently being used to execute the statements?

Connection conn = DataSourceUtils.getConnection(template.getDataSource());
        Connection con=template.getDataSource().getConnection();

@Bean
public DataSource dataSource() throws NamingException {
    javax.naming.InitialContext ctx = new javax.naming.InitialContext();
    DataSource dataSource = (javax.sql.DataSource) ctx.lookup(dsnName);
    return dataSource;
}
jdbcTemplate.getDataSource().getConnection();

通过使用上面的行,我们可以获取连接对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM