B4J Library HikariCP 5.x

This is a wrapper for HikariCP which is a database connection pool manager (https://github.com/brettwooldridge/HikariCP)

From the github page; Fast, simple, reliable. HikariCP is a "zero-overhead" production ready JDBC connection pool. At roughly 130Kb, the library is very light.

A user called "mindful" had previously wrapped this library but he hasnt been on the forum since March 2021 - see here - and his wrapper was for an older version.

A while back I wrote a semi-functional wrapper for HikariCP 5.x for some testing I was doing so I thought I should mention it here.

DOWNLOAD LINK;
Library code; https://github.com/ope-nz/HikariCP

To create a connection pool (in this case is for SQLite)

B4X:
Dim url As String = $"jdbc:sqlite:${File.DirApp}\test.db"$
'Initialize(String JdbcUrl, String user, String password, String ConnectionTestQuery)
cp.Initialize(url,Null,Null,Null)

To use the connection pool (eg in a handler class);

B4X:
'Handler class
Sub Class_Globals
    Dim DB As SQL
End Sub

Public Sub Initialize
    Dim DB As SQL
    DB = Main.cp.GetConnection
 
    'Use DB as per normal SQL connection

    ' Close the connecton to return to the pool, otherwise another connection will created until this one is deemed idle
    DB.Close
End Sub

You can adjust some settings directly via the library eg

B4X:
    cp.MaximumPoolSize = 100
    cp.MaxLifetime = 1000
    cp.MinimumIdle = 1000
 
    Log(cp.MaximumPoolSize)
    Log(cp.MaxLifetime)
    Log(cp.MinimumIdle)

To get stats use these methods below. Note that calling these methods before requesting a connection results in a null exception so the library will return -1 to avoid any errors.

B4X:
    Log(cp.ActiveConnections)
    Log(cp.IdleConnections)
    Log(cp.ThreadsAwaitingConnection)
    Log(cp.TotalConnections)

Note: There are many config options and I have only implemented the ones I needed. If there is enough interest I can implement more or you can simply use JavaObject to change the config as per mindfuls post.

Config documentation; https://github.com/brettwooldridge/HikariCP#gear-configuration-knobs-baby

If you wish to create a read only connection to a SQLite database you need to initialize this way. The connection is faster as a result.

B4X:
    Dim url As String = $"jdbc:sqlite:${File.DirApp}\db.sqlite?open_mode=1"$
    cp.Initialize(url,Null,Null,Null)
    Log(cp.IsInitialised)
 
    cp.MaximumPoolSize = 50
    cp.MinimumIdle = 10
    cp.ReadOnly = True
    cp.AutoCommit = False
 
Last edited:

tchart

Well-Known Member
Licensed User
Longtime User
Any advantages your library has over Mindfuls?
Not really. they are functionally the same, in fact I used his library code to begin with. Ive just started adding getters/setters for the config to avoid using RunMethod.

Mindful did not specify DependsOn in the library so you can use whatever version of HikariCP you want but you do need to then specify the jars via AdditionalJar as youve shown.

Also his post if from 2018 so I so anyone stumbling on it may not realise there are newer versions of the library available - or the fact that you can use newer versions of HikariCP with it (if you know how).
 

incendio

Well-Known Member
Licensed User
Longtime User
Compare to JRDC2 with one is better for performance and easy to maintain ?
 

tchart

Well-Known Member
Licensed User
Longtime User
Compare to JRDC2 with one is better for performance and easy to maintain ?
They are for different purposes.

JRDC2 provides a REST interface for access your database.

A database pool connection manager is about managing multiple database connections within an application.
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
@tchart I have been using Mindfuls library with HikariCP 5.x for quite some time now using
I tried the same approach as you a month ago but there was a bug that after a couple of days (it could range between 2 days or a week) hickari loses reference to the created connections stalling the app.

I know it was the version because when I reverted back to V4 it didn't happen at all.

I will try @tchart library and see if the bug is no longer there
 

alwaysbusy

Expert
Licensed User
Longtime User
I tried the same approach as you a month ago but there was a bug that after a couple of days (it could range between 2 days or a week) hickari loses reference to the created connections stalling the app.

I know it was the version because when I reverted back to V4 it didn't happen at all.

I will try @tchart library and see if the bug is no longer there
That is very interesting to know as of late I also got this problem with 5.x. Could you post me the exact version of V4 you are using that doesn't have this problem? Thanks!
 

prajinpraveen

Active Member
Licensed User
Longtime User
Good Day,

any ways to check active connections(getActiveConnections) count like we do with c3p0

thanks
 
Last edited:
Top