web stats
Example for how to use HikariCP? - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 07-15-2015, 09:53 AM
ben ben is offline
What's HL7?
 
Join Date: Oct 2013
Posts: 3
ben is on a distinguished road
Default Example for how to use HikariCP?

Does anyone have an example code / code template they are willing to share for creating and using HikariCP (connection pool). I'm currently using a cached connection (see below) but would like to do something similar with HikariCP.

I'm also open to suggestions for corrections or improvements on the below code.

Thanks in advance!
Ben

// GetCachedMessageTrackingDBConnection. Params: recreate (boolean) - true to force a reconnect, version (int) - expected DB version, channelName (string)
Packages.java.lang.Class.forName("net.sourceforge. jtds.jdbc.Driver");
function GetCachedMessageTrackingDBConnection(recreate, version, channelName, retryIfClosed) {
var key = 'g_MessageTrackingDBConnection';
var dbConn = globalMap.get(key);

if (recreate == true) {
if (dbConn && !dbConn.isClosed()) {
logMessage('[etCachedMessageTrackingDBConnection] Connection is not closed. Closing...', 'D', channelName);
CloseMessageTrackingDBConnection(dbConn);
}
dbConn = null;
}
if (retryIfClosed != false)
retryIfClosed = true;
if (!dbConn) {
var error = '';
var connectionString = configurationMap.get('MessageTrackingDBConnectionS tring');

if (connectionString == null || connectionString == '') {
error = '[GetCachedMessageTrackingDBConnection] Could not get connection string from configurationMap, Make sure MessageTrackingDBConnectionString is set.';
logMessage(error + '; Throwing exception...', 'E', channelName);
throw(error);
}
logMessage('[GetCachedMessageTrackingDBConnection] Could not get connection from global map, creating a new.', 'I', channelName);
try {
logMessage('[GetCachedMessageTrackingDBConnection] Getting connection...', 'D', channelName);
dbConn = Packages.java.sql.DriverManager.getConnection('jdb c:jtds:sqlserver://' + connectionString);
logMessage('[GetCachedMessageTrackingDBConnection] Getting connection completed.', 'D', channelName);
try {
var cstmt = dbConn.prepareCall("{? = call spGetDBVersion()}"); // returns JtdsCallableStatement
cstmt.setInt(1, 0);
cstmt.registerOutParameter(1, Packages.java.sql.Types.INTEGER); // Return Value
logMessage('[GetCachedMessageTrackingDBConnection] Executing spGetDBVersion...', 'D', channelName);
cstmt.execute();
logMessage('[GetCachedMessageTrackingDBConnection] Executing spGetDBVersion completed.', 'D', channelName);
var dbVersion = cstmt.getInt(1);
cstmt.close();
if (dbVersion != version)
logMessage('[GetCachedMessageTrackingDBConnection] Expected DBVersion: ' + version + '. Current version is ' + dbVersion + '.', 'I', channelName);
}
catch (exception) {
error = 'spGetDBVersion Ex: ' + exception.toString();
logMessage('[GetCachedMessageTrackingDBConnection] ' + error, 'D', channelName);
logMessage('[GetCachedMessageTrackingDBConnection] Closing connection...', 'D', channelName);
CloseMessageTrackingDBConnection(dbConn);
logMessage('[GetCachedMessageTrackingDBConnection] Connection closed.', 'D', channelName);
dbConn = null;
}
}
catch (exception) {
logMessage('[GetCachedMessageTrackingDBConnection] Exception getting connection: ' + exception.toString(), 'D', channelName);
error = 'DB Connection Ex: ' + exception.toString();
dbConn = null;
}
if (dbConn == null) {
if (error == '')
error = 'getConnection returned null';
error = '[GetCachedMessageTrackingDBConnection] Error: ' + error;
logMessage(error, 'E', channelName);
globalMap.remove(key);
logMessage('[GetCachedMessageTrackingDBConnection] Throwing exception...', 'D', channelName);
throw(error);
}
globalMap.put(key, dbConn);
} else if (dbConn.isClosed()) {
if (retryIfClosed) {
logMessage('[GetCachedMessageTrackingDBConnection] Connection is closed. Retrying...', 'I', channelName);
GetCachedMessageTrackingDBConnection(true, version, channelName, false);
}
else {
error = 'Connection is closed.';
logMessage('[GetCachedMessageTrackingDBConnection] ' + error, 'E', channelName);
globalMap.remove(key);
logMessage('[GetCachedMessageTrackingDBConnection] Throwing exception...', 'D', channelName);
throw(error);
}
}
return dbConn;
}
Reply With Quote
Reply

Tags
connection pool, database, hikaricp

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 10:16 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation