dtmrs-barrier
dtmrs 的子事务屏障 —— 业务服务(RM)侧接入用。
分支接口一定会被重复调用(TC 重试 + 崩溃恢复),而且可能乱序到达。
一张表 + 一条 INSERT IGNORE 同时解决三个问题:
| 难题 | 场景 |
|---|---|
| 幂等 | 同一分支被调两次 → 只执行一次 |
| 空回滚 | 正向没跑过就来了补偿 → 补偿空转 |
| 悬挂 | 补偿先到、正向后到 → 丢弃迟到的正向 |
零运行时依赖,只需要一个 DB-API 2.0 游标(psycopg2 / pymysql / sqlite3 都行)。
from dtmrs_barrier import Barrier, Decision, MYSQL
Barrier.migrate(conn, MYSQL) # 启动时一次
b = Barrier(MYSQL, trans_type, gid, branch_id, op)
cur = conn.cursor()
try:
if b.decide(cur) == Decision.EXECUTE:
# 业务 SQL —— 必须用这个 cur,跟屏障记录同一个事务
cur.execute("UPDATE account SET balance = balance - %s WHERE id = %s", (amt, uid))
conn.commit() # 原子性的来源
except Exception:
conn.rollback()
raise
NULL_COMPENSATION 和 DUPLICATED 都是正常路径,接口应返回成功。
⚠ 三条不能违反的
- 屏障表必须和业务表在同一个数据库实例 —— 不同实例没法共用本地事务,方案失效
- 业务 SQL 必须用传给
decide的那个 cursor - MySQL 上绝不能用
ON DUPLICATE KEY UPDATE—— 它重复时 rowcount 返回 1 不是 0, 整个算法依赖「冲突时必须是 0」
完整文档见 接入指南。
Apache-2.0
Metadata
Release files for dtmrs-barrier 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dtmrs_barrier-0.2.0.tar.gz | 5.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dtmrs_barrier-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.4 kB
Release files / dtmrs_barrier-0.2.0.tar.gz
| Download URL | dtmrs_barrier-0.2.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
18c28e6136dac24ff7181e004b8d3db28e829a0ae4c5e34451d64670677634fa
|
|
BLAKE2b-256 checksum How to use checksums |
a2229038aec72ba20b792ebde8adbd67e5c66f560cb71caa208264eeab289b4e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.
Transparency logRelease files / dtmrs_barrier-0.2.0-py3-none-any.whl
| Download URL | dtmrs_barrier-0.2.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d3de7634e5f7508912ee7b892184b5065fb37e273fd01ca03fd1d94c67728184
|
|
BLAKE2b-256 checksum How to use checksums |
3d1d602485824cbdab6bc22def6d6c95b59c8426254b1a7a6e07262f5f28c68d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.
Transparency log