我为H2数据库创建了一个触发器,用于自动更新一个时间戳列"updated_ts"。但是,当执行UPDATE SQL语句时,该触发器陷入了无限循环。
UPDATE main.doc doc
SET value = 100
WHERE doc.id = 1;
触发器无限循环问题
@Override
public void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException {
log.debug("🔥🔥 触发了UpdateTimestamp表的触发器 {}",
this.schemaName.concat(".").concat(this.tableName));
if(Objects.isNull(newRow))
return;
final Long id = (Long)newRow[0];
log.debug("🔥🔥 触发了id {} 的触发器", id);
final LocalDate localDate = LocalDate.now();
final LocalDateTime dateTime = LocalDateTime
.of(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth(), 0, 0, 0);
final String update = new StringBuilder("UPDATE ")
.append(this.schemaName).append(".")
.append(this.tableName).append(" doc SET ")
.append(" update_ts = ? ")
.append(" where doc.id = ? ")
.toString();
final PreparedStatement statement = conn.prepareStatement(update);
statement.setTimestamp(1, Timestamp.valueOf(dateTime));
statement.setLong(2, id);
final boolean ok = statement.execute();
log.debug("Update status {} for UpdateTimestampShippingcalcDoc id {} table {}",
ok , id, this.schemaName.concat(".").concat(this.tableName));
}
我认为这可能是由于H2数据库的行为导致的问题。