The stuff I've done here (is used in production [shudder] but) is just "upper nonsense".
Unfortunatly I've found no time to fix it up to now!
See the comments below Vlad's answer to Find or insert based on unique key with Hibernate on Stack Overflow.
I tried the solution proposed by Vlad Mihalcea, but I think the "find part" is not really working this way.
-
to run the tests you have to create a database 'demo' with user 'demo' and password 'demo' (or alter
src/test/resources/application.properties) -
run all the tests (meanwhile there are three) with:
mvn clean test(lot's of stuff is downloaded becausespring-boot-starter-data-jpaetc. is used) -
the tests
testWithInsertOnDuplicateUpdateandtestWithInsertIgnoreshould fail -
the test
testWithTableLockshould succeed with some output similar to
Fred: no bean found by findByName("dummy")
Fred: lock table
Marcey: no bean found by findByName("dummy")
Marcey: lock table
Pamela: no bean found by findByName("dummy")
Alice: no bean found by findByName("dummy")
Pamela: lock table
Alice: lock table
Fred: lock aquired
Fred: no bean found by second findByName("dummy")
Fred: bean created!
Fred: unlock table
Marcey: lock aquired
Marcey: found bean by second findByName("dummy")
Marcey: unlock table
Pamela: lock aquired
Pamela: found bean by second findByName("dummy")
Pamela: unlock table
Alice: lock aquired
Alice: found bean by second findByName("dummy")
Alice: unlock table
Bob: found bean by findByName("dummy")
Harvey: found bean by findByName("dummy")
- Of course table locking has its price, but in a situation as described in Find or insert based on unique key with Hibernate its a viable solution, I think.