Add Hibernate 4.3 connection provider.

pull/22/head
Brett Wooldridge 11 years ago
parent 6180df59e7
commit 41d05bc8cc

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- For release: mvn release:perform -Darguments=-Dgpg.passphrase=PASSPHRASE -->
@ -135,6 +136,12 @@
<version>5.5.23</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.0.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

@ -0,0 +1,69 @@
/*
* Copyright (C) 2013 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaxxer.hikari.hibernate;
import java.util.Map;
import java.util.Properties;
import org.hibernate.cfg.AvailableSettings;
import com.zaxxer.hikari.HikariConfig;
/**
* Utility class to map Hibernate properties to HikariCP configuration properties.
*
* @author Brett Wooldridge, Luca Burgazzoli
*/
public class HikariConfigurationUtil
{
public static final String CONFIG_PREFIX = "hibernate.hikari.";
public static final String CONFIG_PREFIX_DATASOURCE = "hibernate.hikari.dataSource.";
/**
* Create/load a HikariConfig from Hibernate properties.
*
* @param props a map of Hibernate properties
* @return a HikariConfig
*/
@SuppressWarnings("rawtypes")
public static HikariConfig loadConfiguration(Map props)
{
Properties hicaryProps = new Properties();
copyProperty(AvailableSettings.ISOLATION, props, "transactionIsolation", hicaryProps);
copyProperty(AvailableSettings.AUTOCOMMIT, props, "autoCommit", hicaryProps);
for (Object keyo : props.keySet())
{
String key = (String) keyo;
if (key.startsWith(CONFIG_PREFIX))
{
hicaryProps.setProperty(key.substring(CONFIG_PREFIX.length()), (String) props.get(key));
}
}
return new HikariConfig(hicaryProps);
}
@SuppressWarnings("rawtypes")
private static void copyProperty(String srcKey, Map src, String dstKey, Properties dst)
{
if (src.containsKey(srcKey))
{
dst.setProperty(dstKey, (String) src.get(srcKey));
}
}
}

@ -0,0 +1,150 @@
/*
* Copyright (C) 2013 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaxxer.hikari.hibernate;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.UnknownUnwrapTypeException;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.Stoppable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
/**
*
* @author Brett Wooldridge
*/
public class HikariConnectionProvider implements ConnectionProvider, Configurable, Stoppable
{
private static final long serialVersionUID = -9131625057941275711L;
private static final Logger LOGGER = LoggerFactory.getLogger(HikariConnectionProvider.class);
/**
* HikariCP configuration.
*/
private HikariConfig hcfg;
/**
* HikariCP data source.
*/
private HikariDataSource hds;
// *************************************************************************
//
// *************************************************************************
/**
* c-tor
*/
public HikariConnectionProvider()
{
this.hcfg = null;
this.hds = null;
}
// *************************************************************************
// Configurable
// *************************************************************************
@SuppressWarnings("rawtypes")
@Override
public void configure(Map props) throws HibernateException
{
try
{
LOGGER.debug("Configuring HikariCP");
this.hcfg = HikariConfigurationUtil.loadConfiguration(props);
this.hds = new HikariDataSource(this.hcfg);
}
catch (Exception e)
{
throw new HibernateException(e);
}
LOGGER.debug("HikariCP Configured");
}
// *************************************************************************
// ConnectionProvider
// *************************************************************************
@Override
public Connection getConnection() throws SQLException
{
Connection conn = null;
if (this.hds != null)
{
conn = this.hds.getConnection();
}
return conn;
}
@Override
public void closeConnection(Connection conn) throws SQLException
{
conn.close();
}
@Override
public boolean supportsAggressiveRelease()
{
return false;
}
@Override
@SuppressWarnings("rawtypes")
public boolean isUnwrappableAs(Class unwrapType)
{
return ConnectionProvider.class.equals(unwrapType) || HikariConnectionProvider.class.isAssignableFrom(unwrapType);
}
@Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> unwrapType)
{
if (isUnwrappableAs(unwrapType))
{
return (T) this;
}
else
{
throw new UnknownUnwrapTypeException(unwrapType);
}
}
// *************************************************************************
// Stoppable
// *************************************************************************
@Override
public void stop()
{
this.hds.shutdown();
}
}
Loading…
Cancel
Save