You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. I need help working with transactions and mongodb replica sets. I'm trying to run multi-document transactions. The query creates multiple Permissions which are then added as ref in Role and if query fails, the transaction is aborted.
What is the current behavior?
Initially roles are added and transaction fails at save({session}) and throws 'TransientTransactionError: Transaction 1 does not exist'. If I run script for the second time, no roles are inserted and database freezes on Permissions.create(permissions, {session}).
mongoose.connect('mongodb://localhost:27017, localhost:27018/Test?replicaSet=rs`');
Role.collection.drop();
Permissions.collection.drop();
let permissions = [list];
let session = null;
console.log('Creating Roles');
Permissions.createCollection() // Role is created automatically because of index
.then(() => Promise.all(roleQueries));
role.validate()
.then(() => Role.startSession())
.then(_session => {
session = _session;
session.startTransaction();
return Permissions.create(permissions, {session}) // freezes here
})
.then(() => {
doc.permissions = permissions.map(val => val._id);
return doc.save({session});
})
.then(async () => {
await session.commitTransaction();
session.endSession();
})
.catch(async error => {
if (session) {
await session.abortTransaction();
session.endSession();
}
throw error;
});
And here's the error log.
Dropped Permissions
Dropped Roles
Creating Roles
{ MongoError: Given transaction number 1 does not match any in-progress transactions.
at /home/adil/WebstormProjects/jinnah-hospital/server/node_modules/mongodb-core/lib/connection/pool.js:581:63
at authenticateStragglers (/home/adil/WebstormProjects/jinnah-hospital/server/node_modules/mongodb-core/lib/connection/pool.js:504:16)
at Connection.messageHandler (/home/adil/WebstormProjects/jinnah-hospital/server/node_modules/mongodb-core/lib/connection/pool.js:540:5)
at emitMessageHandler (/home/adil/WebstormProjects/jinnah-hospital/server/node_modules/mongodb-core/lib/connection/connection.js:310:10)
at Socket.<anonymous> (/home/adil/WebstormProjects/jinnah-hospital/server/node_modules/mongodb-core/lib/connection/connection.js:489:15)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
errorLabels: [ 'TransientTransactionError' ],
operationTime:
Timestamp { _bsontype: 'Timestamp', low_: 12, high_: 1544265326 },
ok: 0,
errmsg:
'Given transaction number 1 does not match any in-progress transactions.',
code: 251,
codeName: 'NoSuchTransaction',
'$clusterTime':
{ clusterTime:
Timestamp { _bsontype: 'Timestamp', low_: 12, high_: 1544265326 },
signature: { hash: [Binary], keyId: 0 } },
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {} }
Dropped Permissions
Dropped Roles
Creating Roles
> freezes for a minute here
{ MongoError: Transaction 1 has been aborted.
at server/node_modules/mongodb-core/lib/connection/pool.js:581:63
at authenticateStragglers (/server/node_modules/mongodb-core/lib/connection/pool.js:504:16)
at Connection.messageHandler (server/node_modules/mongodb-core/lib/connection/pool.js:540:5)
at emitMessageHandler (server/node_modules/mongodb-core/lib/connection/connection.js:310:10)
at Socket.<anonymous> (server/node_modules/mongodb-core/lib/connection/connection.js:453:17)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
errorLabels: [ 'TransientTransactionError' ],
operationTime:
Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1544250067 },
ok: 0,
errmsg: 'Transaction 1 has been aborted.',
code: 251,
codeName: 'NoSuchTransaction',
'$clusterTime':
{ clusterTime:
Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1544250067 },
signature: { hash: [Binary], keyId: 0 } },
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {} }
Please mention your node.js, mongoose and MongoDB version.
node.js: v10.14.1
mongoose: 5.3.15
mongodb: 4.0.4
The text was updated successfully, but these errors were encountered:
adiliqbl
changed the title
TransientTransactionError, need help with transactions
Transactions with Promise.all throwing TransientTransactionErrorDec 8, 2018
This is because you're doing 4 operations in parallel:
Role.collection.drop();
Permissions.collection.drop();
Permissions.createCollection()
role.validate()
So you've got a bunch of race conditions. Just use async/await and make your life easier.
awaitRole.collection.drop();awaitPermissions.collection.drop();letpermissions=[list];letsession=null;console.log('Creating Roles');awaitPermissions.createCollection()// Role is created automatically because of indexawaitroleQueries();awaitrole.validate();constsession=awaitRole.startSession();
Hi. I need help working with transactions and mongodb replica sets. I'm trying to run multi-document transactions. The query creates multiple Permissions which are then added as ref in Role and if query fails, the transaction is aborted.
What is the current behavior?
Initially roles are added and transaction fails at
save({session})
and throws 'TransientTransactionError: Transaction 1 does not exist'. If I run script for the second time, no roles are inserted and database freezes onPermissions.create(permissions, {session})
.run-rs --mongod --dbpath "mongodb" --keep --number 2
And here's the error log.
Please mention your node.js, mongoose and MongoDB version.
node.js: v10.14.1
mongoose: 5.3.15
mongodb: 4.0.4
The text was updated successfully, but these errors were encountered: