GeoLatte / geolatte-geom

A geometry model that conforms to the OGC Simple Features for SQL specification.
Other
132 stars 63 forks source link

Unwrap Oracle connection if it can #157

Closed FrenkelS closed 1 year ago

FrenkelS commented 1 year ago

In my application I have a io.agroal.pool.wrapper.ConnectionWrapper which wraps an Oracle connection. The DefaultConnectionFinder can't find the Oracle connection because the wrapper doesn't have a getter that returns the Oracle connection. It does however have the unwrap() method which return the Oracle connection. So try to use that before falling back on reflection.

GisMarsch commented 1 year ago

Why not writing your own implementation and use that? I had the same problem concerning HikariCP. Here is my solution:

First I wrote a ConnectionFinder:

import java.sql.Connection;
import java.sql.SQLException;
import org.geolatte.geom.codec.db.oracle.ConnectionFinder;
import org.geolatte.geom.codec.db.oracle.DefaultConnectionFinder;
import com.zaxxer.hikari.pool.HikariProxyConnection;
import oracle.jdbc.driver.OracleConnection;

/**
 * Implementation of {@link ConnectionFinder} needed by hibernate spatial on ORACLE only.
 * 
 * @author Christian Marsch
 */
public class HikariConnectionFinder extends DefaultConnectionFinder {

    /** */
    private static final long serialVersionUID = 6225703364781158873L;

    /**
     * 
     */
    public HikariConnectionFinder() {
    }

    @Override
    public Connection find(final Connection con) {
        if (HikariProxyConnection.class.isInstance(con)) {
            try {
                return HikariProxyConnection.class.cast(con).unwrap(OracleConnection.class);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else {
            return super.find(con);
        }
    }

}

Important is the NoArgs Constructor. In Hibernate configuration I added it as a property:

hibernate.spatial.connection_finder=package.of.HikariConnectionFinder.class

FrenkelS commented 1 year ago

I'm using Quarkus. To use your own implementation of a ConnectionFinder in Quarkus you need to add to your application.properties: quarkus.hibernate-orm.unsupported-properties."hibernate.spatial.connection_finder" = package.of.MyConnectionFinder

Because you're now using unsupported properties, you get a big warning from Quarkus every time you start up your application.

GisMarsch commented 1 year ago

I'm using Quarkus. To use your own implementation of a ConnectionFinder in Quarkus you need to add to your application.properties: quarkus.hibernate-orm.unsupported-properties."hibernate.spatial.connection_finder" = package.of.MyConnectionFinder

Because you're now using unsupported properties, you get a big warning from Quarkus every time you start up your application.

I see. Did not know that. Thx for explaining me this.

maesenka commented 1 year ago

The code is sufficiently general and doesn't introduce additional dependencies so I have no problem merging this.

Thanks for the PR!