Skip to content

Commit

Permalink
nasty switch from c3p0 to hikariCP, works, but needs real cleanup bef…
Browse files Browse the repository at this point in the history
…ore it can be merged to master
  • Loading branch information
1azyman committed Mar 1, 2018
1 parent f5fb748 commit 5b13bba
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 222 deletions.
19 changes: 9 additions & 10 deletions build-system/pom.xml
Expand Up @@ -246,21 +246,16 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
Expand Down Expand Up @@ -748,6 +743,10 @@
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
</exclusion>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down
38 changes: 15 additions & 23 deletions repo/repo-sql-impl-test/src/test/resources/ctx-test-datasource.xml
Expand Up @@ -25,39 +25,31 @@ It fakes JNDI context with simple H2 file DataSource.
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true" default-autowire="byName">

<bean id="sampleJNDIDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
lazy-init="false">
<property name="driverClass" value="org.h2.Driver"/>
<bean id="sampleJNDIDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"
lazy-init="false">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="jdbcUrl" value="jdbc:h2:file:target/midpoint-home/datasource;DB_CLOSE_ON_EXIT=FALSE;LOCK_MODE=1;LOCK_TIMEOUT=10000"/>
<property name="user" value="sa"/>
<property name="username" value="sa"/>
<property name="password" value=""/>

<property name="acquireIncrement" value="3"/>
<property name="minPoolSize" value="3"/>
<property name="maxPoolSize" value="8"/>
<property name="idleConnectionTestPeriod" value="1800"/>
<property name="connectionTesterClassName"
value="com.evolveum.midpoint.repo.sql.util.MidPointConnectionTester"/>
<property name="connectionCustomizerClassName"
value="com.evolveum.midpoint.repo.sql.util.MidPointConnectionCustomizer"/>
<property name="minimumIdle" value="3"/>
<property name="maximumPoolSize" value="8"/>
<!--<property name="connectionTesterClassName"-->
<!--value="com.evolveum.midpoint.repo.sql.util.MidPointConnectionTester"/>-->
</bean>


<bean id="sampleJNDIDataSourceFromConfig" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
<bean id="sampleJNDIDataSourceFromConfig" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"
lazy-init="false">
<property name="driverClass" value="#{testSqlRepositoryFactory.sqlConfiguration.driverClassName}"/>
<property name="driverClassName" value="#{testSqlRepositoryFactory.sqlConfiguration.driverClassName}"/>
<property name="jdbcUrl" value="#{testSqlRepositoryFactory.sqlConfiguration.jdbcUrl}"/>
<property name="user" value="#{testSqlRepositoryFactory.sqlConfiguration.jdbcUsername}"/>
<property name="username" value="#{testSqlRepositoryFactory.sqlConfiguration.jdbcUsername}"/>
<property name="password" value="#{testSqlRepositoryFactory.sqlConfiguration.jdbcPassword}"/>

<property name="acquireIncrement" value="3"/>
<property name="minPoolSize" value="3"/>
<property name="maxPoolSize" value="8"/>
<property name="idleConnectionTestPeriod" value="1800"/>
<property name="connectionTesterClassName"
value="com.evolveum.midpoint.repo.sql.util.MidPointConnectionTester"/>
<property name="connectionCustomizerClassName"
value="com.evolveum.midpoint.repo.sql.util.MidPointConnectionCustomizer"/>
<property name="minimumIdle" value="3"/>
<property name="maximumPoolSize" value="8"/>
<!--<property name="connectionTesterClassName"-->
<!--value="com.evolveum.midpoint.repo.sql.util.MidPointConnectionTester"/>-->
</bean>

<bean id="jndiMock" class="com.evolveum.midpoint.repo.sql.util.JNDIMock" lazy-init="false">
Expand Down
9 changes: 2 additions & 7 deletions repo/repo-sql-impl/pom.xml
Expand Up @@ -152,15 +152,10 @@
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>

<!-- H2 -->
Expand Down
Expand Up @@ -17,17 +17,17 @@
package com.evolveum.midpoint.repo.sql;

import com.evolveum.midpoint.repo.api.RepositoryServiceFactoryException;
import com.evolveum.midpoint.repo.sql.util.MidPointConnectionCustomizer;
import com.evolveum.midpoint.repo.sql.util.MidPointConnectionTester;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.lang.StringUtils;
import org.springframework.jndi.JndiObjectFactoryBean;

import javax.naming.NamingException;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.io.Closeable;
import java.io.IOException;

/**
* @author Viliam Repan (lazyman)
Expand All @@ -40,6 +40,10 @@ public class DataSourceFactory {

private DataSource dataSource;

public SqlRepositoryConfiguration getConfiguration() {
return configuration;
}

public void setConfiguration(SqlRepositoryConfiguration configuration) {
this.configuration = configuration;
}
Expand All @@ -57,8 +61,8 @@ public DataSource createDataSource() throws RepositoryServiceFactoryException {
return createJNDIDataSource();
}

LOGGER.info("Constructing default C3P0 datasource with connection pooling; JDBC URL: {}", configuration.getJdbcUrl());
dataSource = createC3P0DataSource();
LOGGER.info("Constructing default datasource with connection pooling; JDBC URL: {}", configuration.getJdbcUrl());
dataSource = createDataSourceInternal();
return dataSource;
} catch (Exception ex) {
throw new RepositoryServiceFactoryException("Couldn't initialize datasource, reason: " + ex.getMessage(), ex);
Expand All @@ -73,27 +77,54 @@ private DataSource createJNDIDataSource() throws IllegalArgumentException, Namin
return (DataSource) factory.getObject();
}

private DataSource createC3P0DataSource() throws PropertyVetoException {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(configuration.getDriverClassName());
ds.setJdbcUrl(configuration.getJdbcUrl());
ds.setUser(configuration.getJdbcUsername());
ds.setPassword(configuration.getJdbcPassword());

ds.setAcquireIncrement(3);
ds.setMinPoolSize(configuration.getMinPoolSize());
ds.setMaxPoolSize(configuration.getMaxPoolSize());
ds.setIdleConnectionTestPeriod(1800);
ds.setConnectionTesterClassName(MidPointConnectionTester.class.getName());
ds.setConnectionCustomizerClassName(MidPointConnectionCustomizer.class.getName());

return ds;
public HikariConfig createConfig() {
HikariConfig config = new HikariConfig();

config.setDriverClassName(configuration.getDriverClassName());
config.setJdbcUrl(configuration.getJdbcUrl());
config.setUsername(configuration.getJdbcUsername());
config.setPassword(configuration.getJdbcPassword());

config.setRegisterMbeans(true);

config.setMinimumIdle(configuration.getMinPoolSize());
config.setMaximumPoolSize(configuration.getMaxPoolSize());

TransactionIsolation ti = configuration.getTransactionIsolation();
if (ti != null) {
config.setTransactionIsolation("TRANSACTION_" + ti.name());
}

// todo fix this !!! and the same in ctx-test-datasource.xml
// config.setConnectionTesterClassName(MidPointConnectionTester.class.getName());

if (configuration.isUsingMySqlCompatible()) {
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

config.addDataSourceProperty("useServerPrepStmts", "true");
config.addDataSourceProperty("useLocalSessionState", "true");
config.addDataSourceProperty("useLocalTransactionState", "true");
config.addDataSourceProperty("rewriteBatchedStatements", "true");
config.addDataSourceProperty("cacheResultSetMetadata", "true");
config.addDataSourceProperty("cacheServerConfiguration", "true");
config.addDataSourceProperty("elideSetAutoCommits", "true");
config.addDataSourceProperty("maintainTimeStats", "false");
}

return config;
}

private DataSource createDataSourceInternal() {
HikariConfig config = createConfig();

return new HikariDataSource(config);
}

public void destroy() {
if (dataSource instanceof ComboPooledDataSource) {
ComboPooledDataSource ds = (ComboPooledDataSource) dataSource;
ds.close();
public void destroy() throws IOException {
if (dataSource instanceof Closeable) {
((Closeable) dataSource).close();
}
}
}
Expand Up @@ -600,7 +600,7 @@ public TransactionIsolation getTransactionIsolation() {

private void applyTransactionIsolation() {
// ugly hack, but I know of no way to work around
MidPointConnectionCustomizer.setTransactionIsolation(transactionIsolation);
// MidPointConnectionCustomizer.setTransactionIsolation(transactionIsolation);
}

public boolean isLockForUpdateViaHibernate() {
Expand Down

This file was deleted.

Expand Up @@ -18,7 +18,7 @@

import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.mchange.v2.c3p0.impl.DefaultConnectionTester;
//import com.mchange.v2.c3p0.impl.DefaultConnectionTester;

import java.sql.Connection;
import java.sql.SQLException;
Expand All @@ -39,31 +39,31 @@
* @author mederly
*/

public class MidPointConnectionTester extends DefaultConnectionTester {
public class MidPointConnectionTester { // extends DefaultConnectionTester {

private static final Trace LOGGER = TraceManager.getTrace(MidPointConnectionTester.class);

public static final String POSTGRESQL_PRODUCT_NAME = "PostgreSQL";

private void rollbackChecked(Connection c) {
try {
if (POSTGRESQL_PRODUCT_NAME.equals(c.getMetaData().getDatabaseProductName())) {
c.rollback();
}
} catch (SQLException e) {
LOGGER.debug("An exception got when rolling back current transaction on suspicious DB connection", e);
}
}

@Override
public int activeCheckConnection(Connection c, String query, Throwable[] rootCauseOutParamHolder) {
rollbackChecked(c);
return super.activeCheckConnection(c, query, rootCauseOutParamHolder);
}

@Override
public int statusOnException(Connection c, Throwable t, String query, Throwable[] rootCauseOutParamHolder) {
rollbackChecked(c);
return super.statusOnException(c, t, query, rootCauseOutParamHolder);
}
// private static final Trace LOGGER = TraceManager.getTrace(MidPointConnectionTester.class);
//
// public static final String POSTGRESQL_PRODUCT_NAME = "PostgreSQL";
//
// private void rollbackChecked(Connection c) {
// try {
// if (POSTGRESQL_PRODUCT_NAME.equals(c.getMetaData().getDatabaseProductName())) {
// c.rollback();
// }
// } catch (SQLException e) {
// LOGGER.debug("An exception got when rolling back current transaction on suspicious DB connection", e);
// }
// }
//
// @Override
// public int activeCheckConnection(Connection c, String query, Throwable[] rootCauseOutParamHolder) {
// rollbackChecked(c);
// return super.activeCheckConnection(c, query, rootCauseOutParamHolder);
// }
//
// @Override
// public int statusOnException(Connection c, Throwable t, String query, Throwable[] rootCauseOutParamHolder) {
// rollbackChecked(c);
// return super.statusOnException(c, t, query, rootCauseOutParamHolder);
// }
}

0 comments on commit 5b13bba

Please sign in to comment.