In my below snippet, `tableList` is the map which will contain number of tables and there properties and should look something like this.
`{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}}`
Now that means I need to make two database connections for each thread in a run method as JDBC url's are different for each table. So I have made Connection as list here, and depending on tableList map it will make the conenction,
if we have only one table, then it will make only one connection, if we have two tables, then it will make two connections.
Something like `dbConnection[0]`, `dbConnection[1]` etc.
And for each table, I am calling `getRequiredMethods(suffix)`. So I need to make this as the list also. Because if we have two tables, then it will be having different methods for two tables as well.
class Task implements Runnable {
private Connection[] dbConnection = null;
private CallableStatement[] callableStatement = null;
public Task(ConcurrentHashMap<String, ConcurrentHashMap<String, String>> tableList) {
this.id = nextId;
this.noOfTasks = noOfTasks;
this.tableLists = tableList;
}
@Override
public void run() {
try {
ArrayList<Method> methods[];
for( int i=0; i<tableLists.size(); i++) {
dbConnection[i] = getDBConnection(tableLists.get(i).get("URL"), tableLists.get(i).get("USERNAME"), tableLists.get(i).get("PASSWORD"), tableLists.get(i).get("DRIVER"));
callableStatement[i] = dbConnection[i].prepareCall(tableLists.get(i).get("SQL"));
methods[i] = getRequiredMethods(tableLists.get(i).get("SUFFIX"));
}
}
}
private ArrayList<Method> getRequiredMethods(String suffix) {
Class<ConstantsTest> consClass = ConstantsTest.class;
Method[] methods = consClass.getDeclaredMethods();
ArrayList<Method> requiredMethods = new ArrayList<Method>();
for (int i = 0; i < methods.length; i++) {
String sName = methods[i].getName();
if (sName.endsWith(suffix)) {
requiredMethods.add(methods[i]);
}
}
return requiredMethods;
}
While looking at this code, it doesn't looks to me it's a thread safe code. There are thread safety issue here as multiple threads will be doing get from the tableLists Map. Is there any way I can make this thread safe here?

New Topic/Question
Reply




MultiQuote




|