I have a single process that queries a table for records where PROCESS_IND
= 'N', does some processing, and then updates the PROCESS_IND
to 'Y'.
I'd like to allow for multiple instances of this process to run, but don't know what the best practices are for avoiding concurrency problems.
Where should I start?
All Db2 database system applications are multithreaded by default, and are capable of using multiple contexts. You can use the following Db2 APIs to use multiple contexts.
SQL servers are designed to handle multiple connections but every entry in to the transaction log has to be handled sequentially. In short, many people can on multiple threads be connected to the server, but only 1 transaction can occur at any given point in time.
Multithreading is a model of program execution that allows for multiple threads to be created within a process, executing independently but concurrently sharing process resources. Depending on the hardware, threads can run fully parallel if they are distributed to their own CPU core.
In addition to running user queries on multiple processors SQL Server can also use multiple threads to build indexes.
The pattern I'd use is as follows:
Each task would do a query such as:
UPDATE taskstable SET lockedby=(my id), locktime=now() WHERE lockedby IS NULL ORDER BY ID LIMIT 10
Where 10 is the "batch size".
The LIMIT 10 is MySQL specific but other databases have equivalents. The ORDER BY is import to avoid the query being nondeterministic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With