Giter Club home page Giter Club logo

Comments (2)

T-parrish avatar T-parrish commented on June 2, 2024 4

A few more updates: after running some more tests it seems like this issue may be related to Document state not resetting between transaction retries.

If we modify the the second operation to force a field to be 'modified', everything works as expected:

transaction_builder.addOperation(async (session) => {
   console.log('updating in banking config');
   company.markModified('features.banking_config');
   await company.save({ session });
 });

However, my assumption is that this will only apply updates to that specific field; to ensure that all updates are applied properly, we would need to diff the og object with the updated object and mark each updated field as 'modified' for each subsequent transaction retry.

So, a couple questions:

  1. Why does the document state not fully revert when a transaction aborts and 'rolls back'?
  2. What's the best way to ensure that the document remembers what fields have been modified between transaction retries?
  3. How can a call to document.save() resolve to a findOne() operation? Is this documented anywhere?

Thank you kindly,
Taylor

from mongoose.

vkarpov15 avatar vkarpov15 commented on June 2, 2024

I managed to create a more simplified repro. The issue is that Mongoose's document state doesn't revert when the transaction aborts, and save() becomes a findOne() if there's no changes to save.

const mongoose = require('mongoose');

mongoose.set('debug', true);

main().catch(error => {
  console.error(error);
  process.exit(-1);
});

async function main() {
  await mongoose.connect('mongodb://127.0.0.1:27017,127.0.0.1:27018/mongoose_test?replicaSet=rs');

  const Test = mongoose.model('Test', mongoose.Schema({ name: String }));
  await Test.deleteMany({});
  const { _id } = await Test.create({ name: 'foo' });

  const doc = await Test.findById(_id);
  doc.name = 'bar';
  for (let i = 0; i < 2; ++i) {
    const session = await mongoose.startSession();
    session.startTransaction();

    await doc.save({ session });
    if (i === 0) {
      await session.abortTransaction();
    } else {
      await session.commitTransaction();
    }
    await session.endSession();
  }

  console.log(await Test.findById(_id)); // Still prints name: 'foo'
}

The easiest workaround is to use Mongoose's transaction() helper, which tracks and resets document state if the transaction errors.

Another workaround is to avoid modifying documents that are external to the transaction. For example, the following works fine:

  for (let i = 0; i < 2; ++i) {
    const session = await mongoose.startSession();
    session.startTransaction();

    const doc = await Test.findById(_id);
    doc.name = 'bar';

    await doc.save({ session });
    if (i === 0) {
      await session.abortTransaction();
    } else {
      await session.commitTransaction();
    }
    await session.endSession();
  }

Do either of these approaches work for you?

from mongoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.