Skip to content
Advertisement

Concurrency – Two threads querying multiple databases from different servers and compare each record synchronously

I want to have two threads querying (JDBC) two tables (from different servers/databases but related) for an ordered output then compare them or apply some logic record by record.

The table size can be very large so I was thinking using thread would be the most efficient way to get this done with the least footprint.

Example:

Thread1 – query table server1.database1.schema1.tableA ordered by 1;

Thread2 – query table server2.database2.schema2.tableB where [conditions/logics related to A] order by 1;

Synchronized on each record in the ResultSet in both thread and apply the comparison or data logic.

For example: ResultSet from Thread1 = [1,2,3,4,5]

ResultSet from Thread2 = [2,4,6,8,10]

I want to be able to synchronize at each index (0…4), and compare them. Say Thread1.ResultSet[0] = Thread2.ResultSet[0]/2.

That means:

1 = 2/2

2 = 4/2

etc…

This is what I have so far, base on another answer i got while researching. I am using AtomicInteger to synchronize the ResultSet iteration.

JavaScript

The above code is able to execute the query concurrently on the tables and print out the resultset for each. However, the locking/holding logic is not working. I am trying to put the threads on hold when the AtomicInteger variable are the same in both thread. What this means in my code is that it will iterate through the results set one by one. For each one the AtomicInteger variable is incremented and will wait until the other thread’s AtomicInteger variable to get to the same value. Then the comparison logic happens then both threads are release to move on.

I am unsure about if AtomicInteger is the correct usage here.

Any suggestion is much appreciate.

Advertisement

Answer

A good solution can be to use two ArrayBlockingQueue as buffers

ArrayBlockingQueue db1Buf=new ArrayBlockingQueue<>(BUF_SIZE); ArrayBlockingQueue db2Buf=new ArrayBlockingQueue<>(BUF_SIZE);

the reading threads simply offer lines to the buffer

JavaScript

the third thread processes data

JavaScript

No synchronisation is needed because ArrayBlockingQueue is already synchronised.

the reading thread will feed the buffers and block if they are full, the third thread will consume the data waiting the other threads to read data if the buffer was empty. The class MyData is a simple bean with the fields you need.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement