Pass JPOS Database Connection Using Context

55 views
Skip to first unread message

ombon...@gmail.com

unread,
Apr 23, 2023, 2:31:11 PM4/23/23
to jPOS Users
Hi,
i have a question about pass connection in jpos project, here's my code for reference

public class HostServer implements ISORequestListener, Configurable{
long timeout;
private String queue;
private Space<String, Context> sp;
   
public static void main(String[] args){
Q2 q2 = new Q2();
q2.start();
}
 
@SuppressWarnings("unchecked")
@Override
public void setConfiguration(Configuration cfg) throws ConfigurationException {
timeout = cfg.getLong ("timeout");
sp = (Space<String,Context>) SpaceFactory.getSpace (cfg.get ("space"));
queue = cfg.get ("queue");
if (queue == null){
throw new ConfigurationException ("queue property not specified");
}
}

@Override
public boolean process(ISOSource isoSrc, ISOMsg isoMsg) {
try {
Logger msgLog = LogManager.getLogger(HostServer.class);
UUID hashUniqId =UUID.randomUUID();
DBConnectionPool conn = (DBConnectionPool) NameRegistrar.getIfExists("connection.pool.dbpool");
Connection cn = conn.getConnection();


Context ctx  = new Context ();
        ctx.put (Constant.ISOSOURCE, isoSrc);
        ctx.put (Constant.REQUEST, isoMsg);
        ctx.put (Constant.UUID, hashUniqId);
        ctx.put (Constant.Connection, cn);
        sp.out(queue, ctx, timeout);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}


it works well, but have you recommend it?maybe it can potentially cause a connection leak or decrease the performance. 
FYI, every time i end a request, i close the connection

source.send(resMsg);
GlobalFunction.closeConnection(st, rs, con);


need some expert advice :)
Thanks.

murtuza chhil

unread,
Apr 23, 2023, 8:24:41 PM4/23/23
to jPOS Users
ISORequestlistener is single threaded and will block if anything inside it consumes time. I understand you are just opening a connection, but there are times getting a connection can take time if for any reason the connections aren't free.

The ideal way to do this is to have participants in the transaction manager do any DB related stuff.

JPOS provides a Open and Close participant that can be used in a transaction manager.

https://github.com/jpos/jPOS-EE/blob/master/modules/txn/src/main/java/org/jpos/transaction/Close.java. notice this one implements AbortParticipant so it's always called).

-chhil
Reply all
Reply to author
Forward
0 new messages