【发布时间】:2021-03-18 04:33:44
【问题描述】:
我有一个表 inbox_participants,其中 inbox_id 作为外键。当收件箱更新时,可以添加或删除参与者。我正在尝试删除给定 inbox_id 的所有收件箱参与者,并重新插入具有相同参与者 ID 的更新参与者。它在我的本地机器上正常运行。但在服务器中,它为 inbox_participant_id 提供了一个 Duplicate Entry 异常,该异常应该已被删除。
更新收件箱
logger.info("Update inbox. Inbox id : {}", id);
final String query = "UPDATE inbox SET subject=?, updated_at=?, type=? WHERE inbox_id=?";
Connection conn = null;
PreparedStatement pst = null;
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false);
pst = dataSource.prepareStatement(query, conn);
logger.debug(debug, QUERY, query);
pst.setString(1, inbox.getSubject());
pst.setLong(2, TreeleafDate.timestamp());
pst.setInt(3, inbox.getTypeValue());
pst.setString(4, id);
if (pst.executeUpdate() != 1) {
rollback(conn);
return false;
}
if (!removeParticipants(conn, id, debug)) {
rollback(conn);
return false;
}
if (!updateParticipants(conn, id, accountIdParticipantMap, debug)) {
rollback(conn);
return false;
}
commit(conn);
return true;
} catch (SQLException | JDBCException e) {
rollback(conn);
logger.error(debug, "Error while updating inbox", e);
return false;
} finally {
close(pst);
close(conn);
}
删除参与者
private boolean removeParticipants(final Connection conn,
final String id,
final TreeleafProto.Debug debug) {
logger.info(debug, "Deleting inbox participants. Inbox id : {}", id);
final String query = "DELETE FROM inbox_participant WHERE inbox_id=?";
try (PreparedStatement pst = dataSource.prepareStatement(query, conn)) {
logger.debug(debug, QUERY,
query);
pst.setString(1, id);
final var i = pst.executeUpdate();
logger.debug(debug, "Delete query rows updated : {}", i);
return i >= 0;
} catch (JDBCException | SQLException e) {
logger.error(debug, "Error while removing participants.", e);
return false;
}
}
插入更新的参与者
private boolean updateParticipants(final Connection conn,
final String id,
final Map<String, InboxProto.InboxParticipant> participants,
final TreeleafProto.Debug debug) {
logger.info(debug, "Updating inbox participants");
final String query = "INSERT INTO inbox_participant (inbox_participant_id, inbox_id, account_id, `role`, created_at, updated_at, notification_type, `left`) VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement pst = dataSource.prepareStatement(query, conn)) {
logger.debug(debug, QUERY,
query);
for (Map.Entry<String, InboxProto.InboxParticipant> entry : participants.entrySet()) {
final var participant = entry.getValue();
pst.setString(1, getId(participant));
pst.setString(2, id);
pst.setString(3, entry.getKey());
pst.setInt(4, participant.getRoleValue());
pst.setLong(5, TreeleafDate.timestamp());
pst.setLong(6, TreeleafDate.timestamp());
pst.setInt(7, participant.getNotificationTypeValue());
pst.setInt(8, participant.getParticipantStatusValue());
pst.addBatch();
}
int[] ints = pst.executeBatch();
return ints.length == participants.size() &&
Arrays.stream(ints).allMatch(value -> value == 1);
} catch (JDBCException | SQLException e) {
logger.error(debug, "Error while updating participants.", e);
return false;
}
}
【问题讨论】:
-
请添加 Queries.INSERT_INBOX_PARTICIPANT 以供审核。