在 play 2.x 应用程序中,我有一个发布请求,用于从父表中删除子成员。如果有多个请求具有相同的请求参数,我如何要求 Play 锁定子列表以避免并发访问和重复删除同一记录?如果重复请求非常接近发送,则会引发如下异常:
javax.persistence.OptimisticLockException:数据已更改。更新了 [0] 行 sql[从 id=? 的 channel_detail 中删除 和 member_id=? 和 channel_info_id=?] 绑定[null]
@BodyParser.Of(BodyParser.FormUrlEncoded.class)
public static Result removeMemberFromChannel() {
RequestBody body = request().body();
Map<String, String[]> dict = body.asFormUrlEncoded();
final String memberId = dict.get("memberId") != null ? dict.get(Config.MEMBER_ID_PARAM)[0] : null;
ChannelInfo channelInfo = ChannelInfo.getChannelForName(channelName); //method was removed to save space
if (channelInfo != null) {
channelInfo.removeMemberId(memberId);
channelInfo.save();
}
}
@Entity
@Table(name="channel")
public class ChannelInfo extends Model {
@Id
private Long id;
@Constraints.Required
private String channelName;
@OneToMany(cascade=CascadeType.ALL, mappedBy="channelInfo")
private Set<ChannelDetailMember> members;
private int membersCount = 0;
public void removeMemberId(String memberId) {
Iterator<ChannelDetailMember> iter = this.getMembers().iterator();
while (iter.hasNext()) {
ChannelDetailMember dMember = iter.next();
if (dMember.getMemberId().equals(memberId)) {
dMember.delete();
membersCount--;
break;
}
}
}
}
@Entity
@Table(name="channel_detail")
public class ChannelDetailMember extends Model {
@Id
private Long id;
@Constraints.Required
private String memberId;
@Constraints.Required
@ManyToOne(cascade=CascadeType.PERSIST)
ChannelInfo channelInfo;
}