Sunday, 15 February 2015

java - Thread Safety issues of ConcurrentHashMap while trying to retrieve from it -



java - Thread Safety issues of ConcurrentHashMap while trying to retrieve from it -

i trying create new connection each database different jdbc url's within single thread.

in below snippet, tablenames map contain name of tables , there properties connect them. meaning table1 in different database , table2 in different database

sample illustration of tablenames map-

{table1={driver=oracle.jdbc.driver.oracledriver, password=stage_cs_user, url=jdbc_url, suffix=xt1, sql=sql, user=user}, table2={driver=driver_name, password=pass, url=jdbc_url2, suffix=xt2, sql=sql2, user=user}}

below code have-

class task implements runnable { private connection[] dbconnection = null; private callablestatement[] callablestatement = null; private final arraylist<method> methods[] = null; private final concurrenthashmap<string, concurrenthashmap<string, string>> tablelists; public task(concurrenthashmap<string, concurrenthashmap<string, string>> tablenames) { this.tablelists = tablenames; } @override public void run() { seek { int i=0; (map<string, string> map : tablelists.values()) { dbconnection[i] = getdbconnection(map.get("url"), map.get("user"), map.get("password"), map.get("driver")); callablestatement[i] = dbconnection[i].preparecall(map.get("sql")); methods[i] = getrequiredmethods(map.get("suffix")); i++; } } } }

while looking @ code, doesn't looks me it's thread safe code.

i believe there thread safety issues here multiple threads doing tablelists map. there way can create thread safe here?

so question concurrenthashmap thread safe while retrieving info it? , there way can create thread safe here , improve better?

updated code more details:-

class task implements runnable { private connection[] dbconnection = null; private callablestatement[] callablestatement = null; private final arraylist<method> methods[] = null; private final concurrenthashmap<string, concurrenthashmap<string, string>> tablelists; public task(concurrenthashmap<string, concurrenthashmap<string, string>> tablenames) { this.tablelists = tablenames; } @override public void run() { seek { int j=0; dbconnection = new connection[tablelists.size()]; callablestatement = new callablestatement[tablelists.size()]; methods = new arraylist[tablelists.size()]; (map<string, string> map : tablelists.values()) { dbconnection[j] = getdbconnection(map.get("url"), map.get("user"), map.get("password"), map.get("driver")); callablestatement[j] = dbconnection[j].preparecall(map.get("sql")); methods[j] = getrequiredmethods(map.get("suffix")); j++; } (int userid = id; userid < id + nooftasks; userid++) { (int spot = 0; spot < dbconnection.length; spot++) { callablestatement[spot].setstring(1, string.valueof(userid)); (int = 0; < methods.length; i++) { methods[spot].get(i).invoke(null, callablestatement[spot], userid); } long start = system.nanotime(); callablestatement[spot].executeupdate(); // flush records. long end = system.nanotime() - start; final atomiclong before = histogram.putifabsent(end / 1000000, new atomiclong(1l)); if (before != null) { before.incrementandget(); } } } } { closeconnection(callablestatement, dbconnection); } } }

java multithreading concurrency concurrenthashmap

No comments:

Post a Comment