diff --git a/README.md b/README.md index 153b30eb..1719be8a 100644 --- a/README.md +++ b/README.md @@ -894,7 +894,13 @@ Postgres uses `BEGIN` with `COMMIT / ROLLBACK` for top-level transactions, and ` with `RELEASE / ROLLBACK TO name` for inner save-points. This library automatically executes all such transaction and savepoint commands, with unique -savepoint names, based on the transaction level. +savepoint names, based on the transaction level, plus index within the current level, in the +form of `sp_x_y`. + +In the name, `x` is the transaction level, starting with `1` (because `0` is the top-level +transaction that does not use savepoints). And `y` represents sub-transaction order/index +within the current level, starting with `1`. So the first savepoint on the top level will +be named `sp_1_1`. ### Configurable Transactions diff --git a/lib/context.js b/lib/context.js index d1cd247a..2d4e896e 100644 --- a/lib/context.js +++ b/lib/context.js @@ -49,6 +49,7 @@ class ConnectionContext { this.parentCtx = null; // parent context this.taskCtx = null; // task context this.start = null; // Date/Time when connected + this.txCount = 0; } connect(db) { @@ -65,9 +66,21 @@ class ConnectionContext { clone() { const obj = new ConnectionContext(this); + obj.parent = this; obj.parentCtx = this.taskCtx; return obj; } + + get nextTxCount() { + let txCurrent = this, txTop = this; + while(txCurrent.parent) { + txCurrent = txCurrent.parent; + if(txCurrent.taskCtx && txCurrent.taskCtx.isTX) { + txTop = txCurrent; + } + } + return txTop.txCount ++; + } } /** diff --git a/lib/task.js b/lib/task.js index 2646f7b9..ad96be66 100644 --- a/lib/task.js +++ b/lib/task.js @@ -256,7 +256,7 @@ const execute = (ctx, obj, isTX, config) => { if (isTX) { // executing a transaction; - spName = `level_` + ctx.txLevel; + spName = `sp_${ctx.txLevel}_${ctx.nextTxCount}`; return begin() .then(() => callback(ctx, obj, ctx.cb, config) .then(data => { diff --git a/package.json b/package.json index f281bee0..9adbc033 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-promise", - "version": "10.9.2", + "version": "10.9.3", "description": "PostgreSQL interface for Node.js", "main": "lib/index.js", "typings": "typescript/pg-promise.d.ts",