From 2b74b9b4dc796e7d6e4430dcab038e5b9f9442a5 Mon Sep 17 00:00:00 2001 From: Mustafa Elbehery Date: Mon, 9 Jun 2025 20:59:48 +0200 Subject: [PATCH] Revert "Merge pull request #969 from nspcc-dev/drop-unused-txs" This reverts commit f33bb135c8427bf3b0a50af16e879beb3aebb9d1, reversing changes made to cc41cbb87d0a26d7e236b1c5d9b0b3b020bfdefd. Signed-off-by: Mustafa Elbehery --- db.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/db.go b/db.go index e7e4c09dc..4171983bc 100644 --- a/db.go +++ b/db.go @@ -132,6 +132,7 @@ type DB struct { pageSize int opened bool rwtx *Tx + txs []*Tx freelist fl.Interface freelistLoad sync.Once @@ -793,6 +794,9 @@ func (db *DB) beginTx() (*Tx, error) { t := &Tx{} t.init(db) + // Keep track of transaction until it closes. + db.txs = append(db.txs, t) + n := len(db.txs) if db.freelist != nil { db.freelist.AddReadonlyTXID(t.meta.Txid()) } @@ -803,7 +807,7 @@ func (db *DB) beginTx() (*Tx, error) { // Update the transaction stats. db.statlock.Lock() db.stats.TxN++ - db.stats.OpenTxN++ + db.stats.OpenTxN = n db.statlock.Unlock() return t, nil @@ -852,6 +856,17 @@ func (db *DB) removeTx(tx *Tx) { // Use the meta lock to restrict access to the DB object. db.metalock.Lock() + // Remove the transaction. + for i, t := range db.txs { + if t == tx { + last := len(db.txs) - 1 + db.txs[i] = db.txs[last] + db.txs[last] = nil + db.txs = db.txs[:last] + break + } + } + n := len(db.txs) if db.freelist != nil { db.freelist.RemoveReadonlyTXID(tx.meta.Txid()) } @@ -861,7 +876,7 @@ func (db *DB) removeTx(tx *Tx) { // Merge statistics. db.statlock.Lock() - db.stats.OpenTxN-- + db.stats.OpenTxN = n db.stats.TxStats.add(&tx.stats) db.statlock.Unlock() }