fix: 修复自增主键并发线程问题

This commit is contained in:
lhc
2026-03-06 15:38:02 +08:00
parent 7eed24c829
commit eb83178701
2 changed files with 11 additions and 11 deletions

View File

@@ -56,13 +56,13 @@ public interface TableMapper {
int updateBatchByPk(@Param("sql")String sql);
void lockSequence(@Param("tableName")String tableName);
int lockSequence(@Param("tableName")String tableName);
void unlockSequence(@Param("tableName") String tableName);
int unlockSequence(@Param("tableName") String tableName);
void lockSequenceDm(@Param("tableName") String tableName);
int lockSequenceDm(@Param("tableName") String tableName);
void unlockSequenceDm(@Param("tableName") String tableName);
int unlockSequenceDm(@Param("tableName") String tableName);
void createSequenceSafeDm(@Param("tableName") String tableName, @Param("pkName") String pkName);
int createSequenceSafeDm(@Param("tableName") String tableName, @Param("pkName") String pkName);
}

View File

@@ -178,18 +178,18 @@
INCREMENT BY 1
CACHE 1;
</insert>
<select id="setValForHighGoSequence">
<update id="setValForHighGoSequence">
SELECT setval( '${tableName}_seq', (SELECT COALESCE(MAX(${pkName}),0) + 1 FROM ${tableName}), false);
</select>
</update>
<!-- 获取锁 -->
<select id="lockSequence">
<update id="lockSequence">
SELECT pg_advisory_lock(hashtext('${tableName}_seq'));
</select>
</update>
<!-- 释放锁 -->
<select id="unlockSequence" >
<update id="unlockSequence" >
SELECT pg_advisory_unlock(hashtext('${tableName}_seq'));
</select>
</update>
<update id="lockSequenceDm">
DECLARE
result INT;