发生此异常后,所有其他请求都失败 - “具有相同标识符值的另一个对象已与会话关联”。
- javax.persistence.EntityExistsException:具有相同标识符值的另一个对象已与会话关联:[com.dventus.common.user.User#rp_test_user1] 2019-03-21 10:56:44,370 19749 [http-nio- 8080-exec-24] INFO com.atomikos.jdbc.AtomikosConnectionProxy - 用于com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper@7fc183ea的atomikos连接代理:调用rollback ... 2019-03-21 10:56:44,370 19749 [ http-nio-8080-exec-24] INFO com.atomikos.jdbc.AtomikosConnectionProxy - 用于com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper@7fc183ea的atomikos连接代理:调用setAutoCommit ...
添加我的实体的代码:
public MDMResponse process(MDMRequest mdmRequest) throws Exception {
MDMResponse mdmResponse = null;
AddProcessor.logger.info("\n Request recieved "+mdmRequest.toString());
final EntityManager entityManager = this.getContext().getEntityManager();
try {
final Object entity = this.getContext().getMapper().toEntity(mdmRequest);
AddProcessor.logger.debug("The entity found is "+entity.getClass().getName());
entityManager.persist(entity);
mdmResponse = new MDMResponse(ResponseStatus.OK, Messages.SAVE_SUCCESS);
AddProcessor.logger.debug(mdmResponse.toString());
}
catch (final Exception e) {
AddProcessor.logger.error(e);
mdmResponse = new MDMErrorResponse(ResponseStatus.ERROR, Messages.SAVE_ERROR,Catagory.SAVE,Code.ONE,Severity.normal);
}
return mdmResponse;
}
在catch和check之后添加此代码
finally {
entityManager.close();
}