Hucpa

March 11, 2017 ยท View on GitHub

Code Climate Gem Version CircleCI

A JRuby wrapper to HikariCP - "zero-overhead" production ready JDBC connection pool. This is / will be a part of a bigger effort, like a dedicated library to use SQL and / or ActiveRecord integration.

Please note the project support only JRuby (tested with 9.1.8.0+) on Java 8.

The public API is subject to change before version 1.0.0.

Installation

Add this line to your application's Gemfile:

gem "hucpa"

And then execute:

$ bundle

Or install it yourself as:

$ gem install hucpa

Configuration options

See HikariCP Documentation for a detailed description. Not all HikariCP-defined configuration options are currently supported. To add support for infrequently used options please raise an issue or file a pull request.

OptionRequiredDefault valueNotes
adapterYes(1)-Symbol
auto_commitNotrueBoolean
connection_test_queryNo-String
connection_timeoutNo30_000Integer, greater than or equal to 250, in miliseconds
database_nameNo-String
idle_timeoutNo600_000Integer, 0 (disabled) or greater than or equal to 10000, in miliseconds
jdbc_urlYes(1)-String
max_lifetimeNo1_800_000Integer, 0 (disabled) or greater than or equal to 30000, in miliseconds
maximum_pool_sizeNo10Integer, greater than or equal to 1
minimum_idleNo10Integer, greater than or equal to 1
passwordYes-String
pool_nameNo-String
server_nameNo-String
usernameYes-String

(1) - either adapter or jdbc_url has to be provided.

Supported adapters and corresponding datasource class names

AdapterDatasource class name
db2com.ibm.db2.jcc.DB2SimpleDataSource
derbyorg.apache.derby.jdbc.ClientDataSource
fdbsqlcom.foundationdb.sql.jdbc.ds.FDBSimpleDataSource
firebirdorg.firebirdsql.pool.FBSimpleDataSource
h2org.h2.jdbcx.JdbcDataSource
hsqldborg.hsqldb.jdbc.JDBCDataSource
mariadborg.mariadb.jdbc.MySQLDataSource
mysqlcom.mysql.jdbc.jdbc2.optional.MysqlDataSource
oracleoracle.jdbc.pool.OracleDataSource
pgjdbc_ngcom.impossibl.postgres.jdbc.PGDataSource
postgresqlorg.postgresql.ds.PGSimpleDataSource
sqliteorg.sqlite.JDBC
sqlserver_jtdsnet.sourceforge.jtds.jdbcx.JtdsDataSource
sqlservercom.microsoft.sqlserver.jdbc.SQLServerDataSource
sybasecom.sybase.jdbcx.SybDataSource

Usage

Install the database driver, for PostgreSQL:

gem "jdbc-postgres"

Load the the database driver if needed, for PostgreSQL:

require "jdbc/postgres"
Jdbc::Postgres.load_driver

Configure the connection pool:

# Using the adapter option
options = {
  adapter: :postgresql,
  database_name: "hucpa",
  password: "hucpa",
  server_name: "postgres",
  username: "hucpa"
}

# Using the jdbc_url option
options = {
  jdbc_url: "jdbc:postgresql://postgres/hucpa",
  password: "hucpa",
  username: "hucpa"
}

connection_pool = Hucpa::ConnectionPool.new(options)

Use the connection pool with the with_connection API:

answer = connection_pool.with_connection do |connection|
  result_set =
    connection
      .create_statement
      .execute_query("SELECT 42 AS answer")

  result_set.next and result_set.get_int("answer")
end

answer
=> 42

Or use the connection pool with the "classic" API:

datasource = connection_pool.open

# Explicitly obtain the DB connection
connection = datasource.connection

result_set =
  connection
    .create_statement
    .execute_query("SELECT 42 AS answer")

answer = result_set.next and result_set.get_int("answer")

# Explicitly release the DB connection
connection.close

answer
=> 42

Close the connection pool:

connection_pool.close

Development

Build the Docker image:

$ docker-compose build

Create services:

$ docker-compose create

Run specs:

$ docker-compose run --rm app rspec spec

Run console:

$ docker-compose run --rm app irb

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tomekw/hucpa. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.