java - Is connection pooling with JDBC still improving performance nowadays? -
my application not using form of connection pooling, i'm working directly connections. application short, simple queries. log can see opens , closes connections, performing single select of 1 or few rows in between. these take typically ~100ms (including opening , closing connection).
there countless articles , blog entries on how connection pooling improves application performance, seem rather old (5 or more years).
does connection pooling still provide reasonable performance benefit or had become obsolete. i'm using sqlserver 2008 microsofts jdbc driver version 3.0, if matters.
results/update: many things happened since have asked question (we switches jdbc driver , lots of other stuff). @ time did lots of refactorings , other stuff , @ apportunity added connection pooling application. connections pooling queries execute faster log timestamp granularity can measure (less 16ms believe).
so in conclusion, yes connection pooling still worth effort if need connect/disconnect frequently.
if 100 ms per query fine you, don't need connection pool. if need queries less 20 ms, reusing connections essential.
if driver supports own connection pool, suggest use (in case doesn't already). if want greater control on how connections pooled can use additional library (never found use 1 myself)
note: need not use pool re-use connections.
one simple way reuse connections have 1 persistent connection (which has appropriate thread safety guards in place) if queries infrequent, may need.
if want able perform queries concurrently , have few threads perform queries, can store connection in threadlocal field.
if want multiple connections , have more threads perform query want have in connections, use pool.
for threadlocal model can do
public static final threadlocal<connection> connection = new threadlocal<connection>() { public connection initialvalue() { log.info(thread.currentthread()+": created connection."); return createconnection(); } };
if want control how connections cleaned up.
private static final map<thread, connection> connections = new concurrenthashmap(); public static final threadlocal<connection> connection = new threadlocal<connection>() { public connection initialvalue() { log.info(thread.currentthread()+": created connection."); connection conn = createconnection(); connections.put(thread.currentthread(), conn); return conn; } }; public static void cleanup() { for(map.entry<thread, connection> entry: connections.entryset()) { thread t = entry.getkey(); if (!t.isalive()) { log.info(t+": closed connection."); connections.remove(t); entry.getvalue().close(); } } }
if concerned getting dead connection, can override get() of threadlocal test connection before returning.
Comments
Post a Comment