Hi,
I have a MySQL DB where a key (a string) is read from a third pary source. However, these are not always guaranteed unique (they're movie titles). So I need to check for key uniqueness, and if not unique, amend it to make it unique - by adding an incremental count at the end of the key for example.
What's the best pattern to do this? I currently have a table which stores every key received from 3rd party and stores a count, so I do:
INSERT INTO MYTABLE(KEY) VALUES(KEY_VAlUE) ON DUPLICATE KEY UPDATE KEY_COUNT = KEY_COUNT + 1
I am using jdbcTemplate and a keyHolder to retrieve the ID of the new (or updated) row. The problem is I also need to get the count back. I can of course now do a second query (SELECT) to fetch the record with that ID. However the issue here is with concurrency. It is possible (although unlikely), another INSERT with the same third-party key occurs just before I do my SELECT. In this case the count would be incremented a second time and I'll get the wrong count back.
Any ideas?
Thanks Richard.