Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwrap Oracle connection if it can #157

Merged
merged 3 commits into from
May 8, 2023

Conversation

FrenkelS
Copy link
Contributor

@FrenkelS FrenkelS commented May 4, 2023

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.

if (con.isWrapperFor(ORACLE_CONNECTION_INTERFACE)) {
return (Connection) con.unwrap(ORACLE_CONNECTION_INTERFACE);
}
} catch (SQLException e1) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

25% of developers fix this issue

EmptyCatch: Caught exceptions should not be ignored


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

@GisMarsch
Copy link
Contributor

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
Copy link
Contributor Author

FrenkelS commented May 5, 2023

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
Copy link
Contributor

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 maesenka merged commit 4ab5d56 into GeoLatte:master May 8, 2023
@maesenka
Copy link
Member

maesenka commented May 8, 2023

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

Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants