jdbc spring opentracing close connection during transaction

66 Views Asked by At

I use opentracing and all my jdbc Connections are wrapped in TracingConnection proxy. The problem arises when I use method org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate#queryForStream(java.lang.String, java.util.Map<java.lang.String,?>, org.springframework.jdbc.core.RowMapper<T>) during transaction

This method executes method org.springframework.jdbc.datasource.DataSourceUtils#doReleaseConnection and explain, if Connection is transactional or not

org.springframework.jdbc.datasource.DataSourceUtils#doReleaseConnection

To do this, the method compares Connection from ConnectionHolder and passed Connection, with org.springframework.jdbc.datasource.DataSourceUtils#connectionEquals method

private static boolean connectionEquals(ConnectionHolder conHolder, Connection passedInCon) {
    if (!conHolder.hasConnection()) {
        return false;
    }
    Connection heldCon = conHolder.getConnection();
    // Explicitly check for identity too: for Connection handles that do not implement
    // "equals" properly, such as the ones Commons DBCP exposes).
    return (heldCon == passedInCon || heldCon.equals(passedInCon) ||
            getTargetConnection(heldCon).equals(passedInCon));
}

The problem is that connection from ConnectionHolder is instance of io.opentracing.contrib.jdbc.TracingConnection and passedInCon is instance of HikariProxyConnection wrapping org.postgresql.jdbc.PgConnection. If I unwrap Connection from ConnectionHolder, I can see that it's equals to passedInCon, but passedInCon doesn't wrapping into TracingConnection.

I was figuring out why passedInCon is not an instance of TracingConnection. PassedInCon is taken from preparedStatement which in turn is created here io.opentracing.contrib.jdbc.TracingConnection#prepareStatement(java.lang.String) In that method Connection is instance of HikariProxyConnection wrapping org.postgresql.jdbc.PgConnection and further it is not wrapped in TracingConnection.

What solutions are there so that the Connection is wrapped into TracingConnection here and when compared they are the same? Or other approaches to not closing the connection during a transaction with opentracing enabled?

0

There are 0 best solutions below