mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add HibernateConnectionProvider
This commit is contained in:
@@ -24,7 +24,7 @@ public class WrapperAdapter implements Wrapper {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWrapperFor(Class<?> iface) throws SQLException {
|
||||
public boolean isWrapperFor(Class<?> iface) {
|
||||
if (iface == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -38,7 +38,7 @@ public class WrapperAdapter implements Wrapper {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T unwrap(Class<T> iface) throws SQLException {
|
||||
public <T> T unwrap(Class<T> iface) {
|
||||
if (iface == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.alibaba.druid.support.hibernate;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.service.spi.Configurable;
|
||||
import org.hibernate.service.spi.Stoppable;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
||||
|
||||
public class DruidConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
|
||||
|
||||
private static final long serialVersionUID = 1026193803901107651L;
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public boolean isUnwrappableAs(Class unwrapType) {
|
||||
return dataSource.isWrapperFor(unwrapType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T unwrap(Class<T> unwrapType) {
|
||||
return dataSource.unwrap(unwrapType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeConnection(Connection conn) throws SQLException {
|
||||
conn.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsAggressiveRelease() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public void configure(Map configurationValues) {
|
||||
try {
|
||||
DruidDataSourceFactory.config(dataSource, configurationValues);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalArgumentException("config error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user