1

The system is used by many shops, and each shop has its own invoice number, e.g.

SHOPA-0001
SHOPA-0002
SHOPA-0003

SHOPB-0001
SHOPB-0002
...

Now what I did is, select the last ID in MySQL table, and +1 to the invoice number. My problem is, 1 shop has multiple PC running this system, if 2 cashier submit the form at same time, it will has duplication.

Any suggestion to this problem?

Js Lim
  • 3,625
  • 6
  • 42
  • 80
  • maybe use a third element in your ID, ie *SHOPA-COM1-0001* and then adjust your select to filter by shop and computer – Memor-X Sep 19 '16 at 02:24
  • @Memor-X the requirement for this project is the ID base on shop, not PC – Js Lim Sep 19 '16 at 02:36

1 Answers1

2

Utilize safe Intention Locks like my answer Here. But where I have sections like Chassis and Brakes, yours would be SHOPA and SHOPB etc. You could decide whether you want client-side to handle the left padding of zeros, or if you want mysql to handle it with a column width int(4) or use LPAD() with a CONCAT.

As mentioned in that answer, it is the safe way to do it for concurrency and the shops are segmented off from one another. The lock is uber fast as in momentary if done correctly.

Community
  • 1
  • 1
Drew
  • 24,851
  • 10
  • 43
  • 78