diff --git a/core/pom.xml b/core/pom.xml index 092a6212..8f2ab539 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 @@ -135,6 +136,12 @@ 5.5.23 test + + org.hibernate + hibernate-core + 4.3.0.Final + compile + diff --git a/core/src/main/java/com/zaxxer/hikari/hibernate/HikariConfigurationUtil.java b/core/src/main/java/com/zaxxer/hikari/hibernate/HikariConfigurationUtil.java new file mode 100644 index 00000000..bbaf1b2a --- /dev/null +++ b/core/src/main/java/com/zaxxer/hikari/hibernate/HikariConfigurationUtil.java @@ -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)); + } + } +} \ No newline at end of file diff --git a/core/src/main/java/com/zaxxer/hikari/hibernate/HikariConnectionProvider.java b/core/src/main/java/com/zaxxer/hikari/hibernate/HikariConnectionProvider.java new file mode 100644 index 00000000..9b5114ed --- /dev/null +++ b/core/src/main/java/com/zaxxer/hikari/hibernate/HikariConnectionProvider.java @@ -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 unwrap(Class unwrapType) + { + if (isUnwrappableAs(unwrapType)) + { + return (T) this; + } + else + { + throw new UnknownUnwrapTypeException(unwrapType); + } + } + + // ************************************************************************* + // Stoppable + // ************************************************************************* + + @Override + public void stop() + { + this.hds.shutdown(); + } +}