From 9aebcf64e71d27540be5164894876ce0b79b6024 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Wed, 4 Mar 2015 23:02:48 -0500 Subject: [PATCH] HHH-9639 support unwrapping HikariCPConnectionProvider as DataSource --- .../internal/HikariCPConnectionProvider.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/hibernate-hikaricp/src/main/java/org/hibernate/hikaricp/internal/HikariCPConnectionProvider.java b/hibernate-hikaricp/src/main/java/org/hibernate/hikaricp/internal/HikariCPConnectionProvider.java index e3dd642202..3dab3ad002 100644 --- a/hibernate-hikaricp/src/main/java/org/hibernate/hikaricp/internal/HikariCPConnectionProvider.java +++ b/hibernate-hikaricp/src/main/java/org/hibernate/hikaricp/internal/HikariCPConnectionProvider.java @@ -21,10 +21,8 @@ package org.hibernate.hikaricp.internal; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.Map; - +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; import org.hibernate.HibernateException; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.service.UnknownUnwrapTypeException; @@ -32,8 +30,10 @@ import org.hibernate.service.spi.Configurable; import org.hibernate.service.spi.Stoppable; import org.jboss.logging.Logger; -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Map; /** * HikariCP Connection provider for Hibernate. @@ -106,18 +106,23 @@ public class HikariCPConnectionProvider implements ConnectionProvider, Configura @SuppressWarnings("rawtypes") public boolean isUnwrappableAs(Class unwrapType) { return ConnectionProvider.class.equals( unwrapType ) - || HikariCPConnectionProvider.class.isAssignableFrom( unwrapType ); + || HikariCPConnectionProvider.class.isAssignableFrom( unwrapType ) + || DataSource.class.isAssignableFrom( unwrapType ); } @Override @SuppressWarnings("unchecked") public T unwrap(Class unwrapType) { - if ( isUnwrappableAs( unwrapType ) ) { - return (T) this; - } - else { - throw new UnknownUnwrapTypeException( unwrapType ); - } + if ( ConnectionProvider.class.equals( unwrapType ) || + HikariCPConnectionProvider.class.isAssignableFrom( unwrapType ) ) { + return (T) this; + } + else if ( DataSource.class.isAssignableFrom( unwrapType ) ) { + return (T) hds; + } + else { + throw new UnknownUnwrapTypeException( unwrapType ); + } } // *************************************************************************