开发者

How to handle Multithreaded insertions with unique constrain in the database using JPA/Hibernate?

in a message driven bean i persist entries to lookup-table with uni开发者_JS百科que entries. Basicly i create a select statement with the criteria api and if i don't find an entry, i create one.

Currently this code is running into mysql errors because in certain cases this entry creation (em.persist(entry)) would violate the unique constrain on my database. another instance of my mdb created the lookup entry after i didn't find it and before i could create it.

How can i handle this synchronization problem, so there still is only one entry?


You need to use SELECT FOR UPDATE or ensure uniqueness differently (e.g. generating unique part of the entry inside Java).

Hibernate supports pessimistic locks which I believe is just another name for the same thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜