|Welcome to the Monastery|
Re^6: Perl DBI and Foreign Keysby afoken (Canon)
|on Apr 09, 2019 at 23:24 UTC||Need Help??|
what if it's wrapped within a transaction?
Still a race. This is really a FAQ. The only clean way is to use what the database offers, either sequences or their crippled replacement, auto-generated IDs. See Re^2: Perl DBI and Foreign Keys.
Of course, you could build your own service to generate unique IDs without race conditions and add that on top. (Quite a challenge if you want it to work with high load ...) But as already explained, this service is already provided by almost any SQL database at no extra cost.
(And please don't come up with UUIDs. They come in different classes, with different properties (see UUID), and some of them are actually quite unlikely to collide. Others are more likely to collide. IDs generated by the database are guaranteed not to collide, and you don't need anything else but the existing database engine.)
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)