Skip to content

Commit

Permalink
ARTIF-683 automatically use HikariCP if a simple connection URL is pr…
Browse files Browse the repository at this point in the history
…ovided
  • Loading branch information
brmeyer committed Jun 8, 2015
1 parent 2174f5f commit d254c06
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
9 changes: 4 additions & 5 deletions doc/guide/en-US/GuideArtificerServerConfiguration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ hibernate.connection.password = sa
...
----

Note that a datasource is *not* required (although recommended). Plain JDBC connections, including external instances,
are also supported (use `hibernate.connection.url`). However,
we strongly recommend using an adequate connection pool (HikariCP, C3P0, Proxool, etc.). Without this, since Artificer does not
configure one out of the box, you'd effectively be using the Hibernate-provided connection pool (not recommended for
production environments).
Note that a datasource is *not* required, although we typically recommend them. Plain JDBC connection URLs, including
external instances, are also fully supported (use `hibernate.connection.url`, `hibernate.connection.username`,
and `hibernate.connection.password`). If a connection URL is used, Artificer will automatically wrap it with
HikariCP, a lightweight and extremely performant connection pool library.

Also note that Artificer ships with JDBC drivers for H2, MySQL, and Postgres. With proper values in artificer.properties,
these databases should simply work out-of-the-box. However, due to licensing, we cannot include the drivers for
Expand Down
2 changes: 0 additions & 2 deletions installer/src/main/resources/updates/artificer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ hibernate.show_sql = false
hibernate.dialect = org.hibernate.dialect.H2Dialect
hibernate.connection.driver_class = org.h2.Driver
hibernate.connection.datasource = java:jboss/datasources/artificerH2
hibernate.connection.username = sa
hibernate.connection.password = sa
hibernate.cache.use_second_level_cache = false
#hibernate.cache.region.factory_class =
#hibernate.cache.default_cache_concurrency_strategy = transactional
Expand Down
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,11 @@
<artifactId>lucene-analyzers</artifactId>
<version>${version.org.apache.lucene}</version>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId>
<version>2.3.8</version>
</dependency>

<dependency>
<groupId>jstl</groupId>
Expand Down
4 changes: 4 additions & 0 deletions repository/hibernate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
<artifactId>javassist</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId>
</dependency>

<!-- Hibernate Search -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.artificer.repository.hibernate;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.artificer.common.ArtificerConfig;
import org.artificer.common.ArtificerException;
import org.artificer.common.error.ArtificerNotFoundException;
Expand All @@ -29,7 +31,9 @@
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Properties;

/**
* @author Brett Meyer.
Expand Down Expand Up @@ -88,6 +92,36 @@ private static EntityManager entityManager() {
if (entityManagerFactory == null) {
// Pass in all hibernate.* settings from artificer.properties
Map<String, Object> properties = ArtificerConfig.getConfigProperties("hibernate");

// If a connection is used, we *cannot* rely on Hibernate's built-in connection pool. Instead,
// automatically set up HikariCP.
if (properties.containsKey("hibernate.connection.url")) {
String connectionUrl = (String) properties.remove("hibernate.connection.url");
String username = (String) properties.remove("hibernate.connection.username");
String password = (String) properties.remove("hibernate.connection.password");

HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(connectionUrl);
hikariConfig.setUsername(username);
hikariConfig.setPassword(password);

// In case we're using MySQL, these settings are recommended by HikariCP:
hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hikariConfig.addDataSourceProperty("useServerPrepStmts", "true");

String dialect = (String) properties.get("hibernate.dialect");
if (dialect != null && dialect.contains("PostgreSQL")) {
// The JDBC jar verion in the IP BOM does not support Connection.isValid(), so need to use this:
hikariConfig.setConnectionTestQuery("SELECT 1");
}

HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig);

properties.put("hibernate.connection.datasource", hikariDataSource);
}

entityManagerFactory = new HibernatePersistence().createEntityManagerFactory(persistenceUnit, properties);
}
return entityManagerFactory.createEntityManager();
Expand Down
13 changes: 10 additions & 3 deletions repository/test/src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@
<!--<property name="hibernate.show_sql" value="true"/>-->
<property name="hibernate.format_sql" value="true"/>

<!-- Don't allow WF/EAP to manage the persistence unit - vital since we're dynamically setting most of the
properties *after* WF/EAP has started! -->
<property name="jboss.as.jpa.managed" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.url" value="jdbc:h2:mem:dbHibernateTest;DB_CLOSE_DELAY=-1;MVCC=true"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.cache.use_query_cache" value="false"/>

<property name="hibernate.search.default.directory_provider" value="ram"/>

<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
2 changes: 0 additions & 2 deletions test/src/test/resources/artificer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ hibernate.show_sql = false
hibernate.dialect = org.hibernate.dialect.H2Dialect
hibernate.connection.driver_class = org.h2.Driver
hibernate.connection.datasource = java:jboss/datasources/artificerH2
hibernate.connection.username = sa
hibernate.connection.password = sa
hibernate.cache.use_second_level_cache = false
#hibernate.cache.region.factory_class =
#hibernate.cache.default_cache_concurrency_strategy = transactional
Expand Down

0 comments on commit d254c06

Please sign in to comment.