2 Replies - 627 Views - Last Post: 13 February 2013 - 10:18 AM Rate Topic: -----

#1 raihan26  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 47
  • Joined: 04-February 10

Thread Safety issues while doing get from the ConcurrentHashMap

Posted 12 February 2013 - 04:49 PM

I am trying to make a different connection for each database with different JDBC URL's within a single thread.

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Thread Safety issues while doing get from the ConcurrentHashMap

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Thread Safety issues while doing get from the ConcurrentHashMap

Posted 13 February 2013 - 03:27 AM

Quote

There are thread safety issue here as multiple threads will be doing get from the tableLists Map.
Why would that be a problem - is the Map being altered while your code is running?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Thread Safety issues while doing get from the ConcurrentHashMap

Posted 13 February 2013 - 10:18 AM

View Postraihan26, on 12 February 2013 - 06:49 PM, said:

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.

Strange design, I would have made an ArrayList of Map rather than an instance per Table

Have a synchronized method to update your Map where is the Thread safety issue ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1