HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Thread Safety issues in the multithreading code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theissuesthreadsafetycodemultithreading

Problem

I am working on a project in which I have two tables in a different database with different schemas. So that means I have two different connection parameters for those two tables to connect using JDBC-

Let's suppose below is the config.property file-

TABLES: table1 table2

#For Table1
table1.url: jdbc:mysql://localhost:3306/garden
table1.user: gardener
table1.password: shavel
table1.driver: jdbc-driver
table1.percentage: 80

#For Table2
table2.url: jdbc:mysql://otherhost:3306/forest
table2.user: forester
table2.password: axe
table2.driver: jdbc-driver
table2.percentage: 20


Below method will read the above config.properties file and make a ReadTableConnectionInfo object for each tables.

private static void readPropertyFile() throws IOException {

    prop.load(Read.class.getClassLoader().getResourceAsStream("config.properties"));

    tableNames = Arrays.asList(prop.getProperty("TABLES").split(" "));

    for (String arg : tableNames) {

        ReadTableConnectionInfo ci = new ReadTableConnectionInfo();

        String url = prop.getProperty(arg + ".url");
        String user = prop.getProperty(arg + ".user");
        String password = prop.getProperty(arg + ".password");
        String driver = prop.getProperty(arg + ".driver");
        double percentage = Double.parseDouble(prop.getProperty(arg + ".percentage"));

       ci.setUrl(url);
       ci.setUser(user);
       ci.setPassword(password);
       ci.setDriver(driver);
       ci.setPercentage(percentage);

        tableList.put(arg, ci);

    }
}


Below is the ReadTableConnectionInfo class that will hold all the table connection info for a particular table.

```
public class ReadTableConnectionInfo {

public String url;
public String user;
public String password;
public String driver;
public String percentage;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUser() {

Solution

In summary, you have 2 shared resources: the tableList variable and the database.

Regarding tableList, if:

  • tableList is fully populated before you submit the tasks to the executor service AND



  • it is populated in the same thread as the main thread that creates the executor and submits the tasks AND



  • it is not modified after that



THEN the access in read-only mode to that shared variable is thread-safe (thanks to the semantics of the submit method which creates a happens-before relationship.


Actions in a thread prior to the submission of a Runnable or Callable task to an ExecutorService happen-before any actions taken by that task.

Regarding the access to the database, DriverManager.getConnection() is thread safe and you don't share the connection object across threads so you are fine (although it would probably be more efficient to use a connection pool).

So your code looks thread safe to me.

Context

StackExchange Code Review Q#23147, answer score: 2

Revisions (0)

No revisions yet.