Skip to content

Commit 70810d1

Browse files
authored
fix(NODE-1843): bulk operations ignoring provided sessions (#2868)
1 parent ee903cb commit 70810d1

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

src/bulk/common.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,8 @@ export abstract class BulkOperationBase {
12031203
}
12041204

12051205
this.s.executed = true;
1206-
return executeLegacyOperation(this.s.topology, executeCommands, [this, options, callback]);
1206+
const finalOptions = { ...this.s.options, ...options };
1207+
return executeLegacyOperation(this.s.topology, executeCommands, [this, finalOptions, callback]);
12071208
}
12081209

12091210
/**

test/functional/bulk.test.js

+142
Original file line numberDiff line numberDiff line change
@@ -2003,4 +2003,146 @@ describe('Bulk', function () {
20032003
);
20042004
});
20052005
});
2006+
2007+
describe('Bulk operation transaction rollback', () => {
2008+
/** @type {import('../../src/index').MongoClient} */
2009+
let client;
2010+
/** @type {import('../../src/index').Collection<{ answer: number }>} */
2011+
let collection;
2012+
2013+
beforeEach(async function () {
2014+
const config = this.configuration;
2015+
client = config.newClient();
2016+
await client.connect();
2017+
2018+
try {
2019+
await client
2020+
.db('bulk_operation_writes_test')
2021+
.collection('bulk_write_transaction_test')
2022+
.drop();
2023+
} catch (_) {
2024+
// do not care
2025+
}
2026+
2027+
collection = await client
2028+
.db('bulk_operation_writes_test')
2029+
.createCollection('bulk_write_transaction_test');
2030+
2031+
await collection.deleteMany({});
2032+
});
2033+
2034+
afterEach(async () => {
2035+
if (client) await client.close();
2036+
});
2037+
2038+
it('should abort ordered bulk operation writes', {
2039+
metadata: { requires: { mongodb: '>= 4.2', topology: ['replicaset'] } },
2040+
async test() {
2041+
const session = client.startSession();
2042+
session.startTransaction({
2043+
readConcern: { level: 'local' },
2044+
writeConcern: { w: 'majority' }
2045+
});
2046+
2047+
let bulk = undefined;
2048+
2049+
bulk = collection.initializeOrderedBulkOp({ session });
2050+
bulk.insert({ answer: 42 });
2051+
await bulk.execute();
2052+
2053+
await session.abortTransaction();
2054+
await session.endSession();
2055+
2056+
const documents = await collection.find().toArray();
2057+
2058+
expect(documents).to.have.lengthOf(
2059+
0,
2060+
'bulk operation writes were made outside of transaction'
2061+
);
2062+
}
2063+
});
2064+
2065+
it('should abort unordered bulk operation writes', {
2066+
metadata: { requires: { mongodb: '>= 4.2', topology: ['replicaset'] } },
2067+
async test() {
2068+
const session = client.startSession();
2069+
session.startTransaction({
2070+
readConcern: { level: 'local' },
2071+
writeConcern: { w: 'majority' }
2072+
});
2073+
2074+
let bulk = undefined;
2075+
2076+
bulk = collection.initializeUnorderedBulkOp({ session });
2077+
bulk.insert({ answer: 42 });
2078+
await bulk.execute();
2079+
2080+
await session.abortTransaction();
2081+
await session.endSession();
2082+
2083+
const documents = await collection.find().toArray();
2084+
2085+
expect(documents).to.have.lengthOf(
2086+
0,
2087+
'bulk operation writes were made outside of transaction'
2088+
);
2089+
}
2090+
});
2091+
2092+
it('should abort unordered bulk operation writes using withTransaction', {
2093+
metadata: { requires: { mongodb: '>= 4.2', topology: ['replicaset'] } },
2094+
async test() {
2095+
const session = client.startSession();
2096+
2097+
await session.withTransaction(
2098+
async () => {
2099+
let bulk = undefined;
2100+
2101+
bulk = collection.initializeUnorderedBulkOp({ session });
2102+
bulk.insert({ answer: 42 });
2103+
await bulk.execute();
2104+
await session.abortTransaction();
2105+
},
2106+
{ readConcern: { level: 'local' }, writeConcern: { w: 'majority' } }
2107+
);
2108+
2109+
await session.endSession();
2110+
2111+
const documents = await collection.find().toArray();
2112+
2113+
expect(documents).to.have.lengthOf(
2114+
0,
2115+
'bulk operation writes were made outside of transaction'
2116+
);
2117+
}
2118+
});
2119+
2120+
it('should abort ordered bulk operation writes using withTransaction', {
2121+
metadata: { requires: { mongodb: '>= 4.2', topology: ['replicaset'] } },
2122+
async test() {
2123+
const session = client.startSession();
2124+
2125+
await session.withTransaction(
2126+
async () => {
2127+
let bulk = undefined;
2128+
2129+
bulk = collection.initializeOrderedBulkOp({ session });
2130+
bulk.insert({ answer: 42 });
2131+
await bulk.execute();
2132+
await session.abortTransaction();
2133+
},
2134+
{ readConcern: { level: 'local' }, writeConcern: { w: 'majority' } }
2135+
);
2136+
2137+
await session.endSession();
2138+
2139+
const documents = await collection.find().toArray();
2140+
2141+
expect(documents).to.have.lengthOf(
2142+
0,
2143+
'bulk operation writes were made outside of transaction'
2144+
);
2145+
}
2146+
});
2147+
});
20062148
});

0 commit comments

Comments
 (0)