Hello and happy New Year to all of you! :) I'm working on a software (Java, backend) that has 3 components each being independent Java application (you can even consider them as separate threads): 1) DB data importer 1. 2) DB data importer 2. 3) DB data exporter.
Now, all those apps are working with the same MySql database, using same 2 InnoDB tables - "Orders" and "Items". Each order may have 0 or many items. All DB operations (data query and data insert) are done using stored procedures. This is what my app does:
Application 1 or 2 (remember, they are independent) starts importing "an order" every few seconds. "Order" is being imported into "Orders" table and each "Item" is being imported into "Items" table. a) Application takes and order, imports that order into "Orders" table and marks the order as "not ready for export". b) Then application continues importing all items (if any) for that order. c) In the end, when all items are imported, order is marked as "ready for export" (i have a dedicated column for that bit).
Application "3" every few seconds does this: a) checks for "ready for export" orders. b) selects 50 "ready for export" orders and marks them as "in export". c) exports orders and theirs items into a file. d) marks all orders that are "in export" to "exported".
Now, as you can tell, even with dedicated column which tells which order or item should be exporter and which is still being imported and shouldn't be exported yet, i'm getting some deadlocks and race conditions.
Do you know any simple and safe mechanism i can use to implement this "producer - consumer" system without locking entire tables "Orders" and "Items" (as they are actively used by other parts of the software)? I guess using 3-state column is not a good idea taking into account each table may have millions of rows and index on such 3-state column is ineffective. There has to be something better :)