HHH-9639 support unwrapping HikariCPConnectionProvider as DataSource

This commit is contained in:
Brett Meyer 2015-03-04 23:02:48 -05:00
parent 27ac1dbbe4
commit 7acd1ebdf1
1 changed files with 18 additions and 13 deletions

View File

@ -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,15 +106,20 @@ 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> T unwrap(Class<T> unwrapType) {
if ( isUnwrappableAs( 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 );
}