简体   繁体   English

Java中的线程同步问题

[英]Thread Synchronization Issue in java

I am working on Soap WebServices application using hibernate. 我正在使用休眠模式开发Soap WebServices应用程序。 In that I am generating orderNumbers for each order. 在这种情况下,我正在为每个订单生成orderNumbers。 To do so I have following table with two fields 为此,我有两个字段的下表

ORDER_SERIES LAST_ORDER_COUNT ORDER_SERIES LAST_ORDER_COUNT


ORD 250 ORD 250

To generate ORDER_NO first I get the record from above table and increament LAST_ORDER_COUNT by 1 and append with ORDER_SERIES 首先要生成ORDER_NO,我需要从上表中获取记录,并且将LAST_ORDER_COUNT减为1,并附加ORDER_SERIES

so my ORDER_NO would be ORD251 所以我的ORDER_NO是ORD251

after that I am updating above table with new increamented LAST_ORDER_COUNT 之后,我要用新的LAST_ORDER_COUNT更新新表

ORDER_SERIES LAST_ORDER_COUNT ORDER_SERIES LAST_ORDER_COUNT


ORD 251 ORD 251

I have written one Method to do this operation means get record from table, increament LAST_ORDER_COUNT append both and return order no and update LAST_ORDER_COUNT. 我编写了一个方法来执行此操作,这意味着从表中获取记录,无提示的LAST_ORDER_COUNT都追加并返回订单号并更新LAST_ORDER_COUNT。 I made this method as synchronized. 我将此方法设为同步。

but in production environment I am getting duplicate ORDER_NOs 但是在生产环境中,我得到了重复的ORDER_NO

my soap webservices are getting Called through android apk. 我的Soap Web服务正在通过Android APK调用。

any idea why ORDER_NO are getting duplicate 不知道为什么ORDER_NO重复

Id should be auto increment in the database and it should be a primary/Unique key as well. ID应该是数据库中的自动增量,也应该是主键/唯一键。 if you will not do so then in future, you would face such type of issue when you will have more request simultaneously. 如果您不这样做,那么将来,当您同时有更多请求时,您将面临此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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