Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec57ad6

Browse files
author
Antonio Scandurra
committed
Prevent dangling transactions
1 parent 45fb45d commit ec57ad6

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/models/repository.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ export default class Repository {
2626
if (this.rawRepository) throw new Error('A transaction is already started. Nested transactions are not supported.')
2727

2828
return this.rawRepositoryPool.enqueue(async (rawRepositoryPromise) => {
29-
this.rawRepository = await rawRepositoryPromise
30-
let result = await fn()
31-
this.rawRepository = null
32-
return result
29+
try {
30+
this.rawRepository = await rawRepositoryPromise
31+
let result = await fn()
32+
return result
33+
} finally {
34+
this.rawRepository = null
35+
}
3336
})
3437
}
3538

test/models/repository.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,24 @@ describe('Repository', () => {
3838
assert.deepEqual(actualEvents, expectedEvents)
3939
})
4040

41-
it('does not allow transactions to nest', async function () {
41+
it('does not allow transactions to nest', async () => {
4242
const workingDirPath = copyRepositoryDir(1)
4343
const repo = await buildRepository(workingDirPath)
4444
await repo.transact(function () {
4545
assert.throws(() => repo.transact(), /Nested transaction/)
4646
})
4747
})
48+
49+
it('allows to create a new transaction if the previous one throws an error', async () => {
50+
const workingDirPath = copyRepositoryDir(1)
51+
const repo = await buildRepository(workingDirPath)
52+
let executed = false
53+
try {
54+
await repo.transact(async () => { throw new Error('An error!') })
55+
} catch (e) { }
56+
await repo.transact(async () => { executed = true })
57+
assert(executed)
58+
})
4859
})
4960

5061
describe('refreshing', () => {

0 commit comments

Comments
 (0)