Skip to content

Commit

Permalink
Moving c3p0 to Hikari CP (#45)
Browse files Browse the repository at this point in the history
* added date format in RuletteInputProcessor and InputDateValue

* removed redundant variable

* using Joda date time formatter instead of simple date

* closing connection, Resultset, Statement

* closing connection, Resultset, Statement

* implementing custom input date value

* added default constructor for InputDateValue

* implemeted date reading till seconds value in MySql DataProvider Itself.

* removed CustomInputDateValue

* Equal method changes for rule comparison of range inputs

* moving close statements under finally block

* moving close statements under finally block

* version change

* adding comments

* MySQL data provider fixes

* PR comments

* Moving data source to use HikariCP

* PR comments

* Removed inheritance from Hikari, removed redundant file

* setting connection timeout in hikariConfig

* separated hikariConfig creation in a method

* separated hikariConfig creation in a method

* removed c3p0 dependency
  • Loading branch information
abhinav3 authored and kislayverma committed Jan 8, 2018
1 parent d57060c commit 5acaccc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion rulette-examples/lib/rulette-datasource.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ initialPoolSize=10
maxPoolSize=50
minPoolSize=10
maxStatements=20

connectionTimeout=5000
6 changes: 3 additions & 3 deletions rulette-mysql-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.4.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,40 @@
package com.github.kislayverma.rulette.mysql.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

/**
*
* @author kislay
*/
public class DataSource {

private Properties props;
private ComboPooledDataSource cpds;
private static DataSource datasource;
private HikariDataSource hikariDatasource;
private static final Logger LOGGER = LoggerFactory.getLogger(DataSource.class);

private DataSource(String fileName) throws IOException, SQLException {
// load datasource properties
props = Utils.readProperties(fileName);
cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(props.getProperty("driverClass"));
} catch (PropertyVetoException ex) {
throw new RuntimeException(ex);
}
cpds.setJdbcUrl(props.getProperty("jdbcUrl"));
cpds.setUser(props.getProperty("username"));
cpds.setPassword(props.getProperty("password"));
cpds.setInitialPoolSize(new Integer((String) props.getProperty("initialPoolSize")));
cpds.setAcquireIncrement(new Integer((String) props.getProperty("acquireIncrement")));
cpds.setMaxPoolSize(new Integer((String) props.getProperty("maxPoolSize")));
cpds.setMinPoolSize(new Integer((String) props.getProperty("minPoolSize")));
cpds.setMaxStatements(new Integer((String) props.getProperty("maxStatements")));
HikariConfig hikariConfig = getHikariConfig(fileName);
hikariDatasource = new HikariDataSource(hikariConfig);

Connection testConnection = null;
Statement testStatement = null;

// test connectivity and initialize pool
try {
LOGGER.info("Testing DB connection...");
testConnection = cpds.getConnection();
testConnection = hikariDatasource.getConnection();
testStatement = testConnection.createStatement();
testStatement.executeQuery("select 1+1 from DUAL");

LOGGER.info("DB connection tested successfully.");
} catch (SQLException e) {
throw e;
Expand Down Expand Up @@ -87,6 +73,20 @@ public static DataSource getInstance(String fileName) throws IOException, SQLExc
}

public Connection getConnection() throws SQLException {
return this.cpds.getConnection();
return this.hikariDatasource.getConnection();
}

private HikariConfig getHikariConfig(String fileName) throws IOException {

Properties props = Utils.readProperties(fileName);
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(props.getProperty("driverClass"));
hikariConfig.setJdbcUrl(props.getProperty("jdbcUrl"));
hikariConfig.setUsername(props.getProperty("username"));
hikariConfig.setPassword(props.getProperty("password"));
hikariConfig.setMaximumPoolSize(new Integer((String) props.getProperty("maxPoolSize")));
hikariConfig.setConnectionTimeout(new Long((String) props.getProperty("connectionTimeout")));

return hikariConfig;
}
}

0 comments on commit 5acaccc

Please sign in to comment.