简体   繁体   中英

hibernate and sqlserver concurrency issues?

I have a webservice exposed, which will save some data into a certain table. i am using sqlserver database. multiple systems can consume the webservice at the same time.

The problem is that two systems are checking the existence of the record and reporting false, inserting a new record at the same time, which is leading to concurrency exception due to duplicate key.

I am using hibernate and the systems whichever are calling the webservice supplies the primarykey also.

Here can i make the method as synchronized ? will there be disadvantages or is there any elegant way to do this?

Thanks!

I would not recommend using synchronised or even using Java/Hibernate to avoid duplicates; if you had to scale your existing application or added another application that modified the data you would still have this problem.

The best way to do this is to set a unique constraint on the table to avoid duplicates; this allows you to handle the unique constraint violation when you insert the duplicate and do what ever you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM