wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict'
 
const debug = require('debug')('mssql:tedi')
const BaseTransaction = require('../base/transaction')
const { IDS } = require('../utils')
const TransactionError = require('../error/transaction-error')
 
class Transaction extends BaseTransaction {
  constructor (parent) {
    super(parent)
 
    this._abort = () => {
      if (!this._rollbackRequested) {
        // transaction interrupted because of XACT_ABORT
 
        const pc = this._acquiredConnection
 
        // defer releasing so connection can switch from SentClientRequest to LoggedIn state
        setImmediate(this.parent.release.bind(this.parent), pc)
 
        this._acquiredConnection.removeListener('rollbackTransaction', this._abort)
        this._acquiredConnection = null
        this._acquiredConfig = null
        this._aborted = true
 
        this.emit('rollback', true)
      }
    }
  }
 
  _begin (isolationLevel, callback) {
    super._begin(isolationLevel, err => {
      if (err) return callback(err)
 
      debug('transaction(%d): begin', IDS.get(this))
 
      this.parent.acquire(this, (err, connection, config) => {
        if (err) return callback(err)
 
        this._acquiredConnection = connection
        this._acquiredConnection.on('rollbackTransaction', this._abort)
        this._acquiredConfig = config
 
        connection.beginTransaction(err => {
          if (err) err = new TransactionError(err)
 
          debug('transaction(%d): begun', IDS.get(this))
 
          callback(err)
        }, this.name, this.isolationLevel)
      })
    })
  }
 
  _commit (callback) {
    super._commit(err => {
      if (err) return callback(err)
 
      debug('transaction(%d): commit', IDS.get(this))
 
      this._acquiredConnection.commitTransaction(err => {
        if (err) err = new TransactionError(err)
 
        this._acquiredConnection.removeListener('rollbackTransaction', this._abort)
        this.parent.release(this._acquiredConnection)
        this._acquiredConnection = null
        this._acquiredConfig = null
 
        if (!err) debug('transaction(%d): commited', IDS.get(this))
 
        callback(err)
      })
    })
  }
 
  _rollback (callback) {
    super._rollback(err => {
      if (err) return callback(err)
 
      debug('transaction(%d): rollback', IDS.get(this))
 
      this._acquiredConnection.rollbackTransaction(err => {
        if (err) err = new TransactionError(err)
 
        this._acquiredConnection.removeListener('rollbackTransaction', this._abort)
        this.parent.release(this._acquiredConnection)
        this._acquiredConnection = null
        this._acquiredConfig = null
 
        if (!err) debug('transaction(%d): rolled back', IDS.get(this))
 
        callback(err)
      })
    })
  }
}
 
module.exports = Transaction