Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb: transparently disable transactions while allowing use in code #3992

Closed
xenoterracide opened this issue Jan 31, 2023 · 1 comment · Fixed by #4260
Closed

mongodb: transparently disable transactions while allowing use in code #3992

xenoterracide opened this issue Jan 31, 2023 · 1 comment · Fixed by #4260
Labels
enhancement New feature or request

Comments

@xenoterracide
Copy link

This error makes total sense to me, something to do with using the in memory mongo server. However, it would be nice to be able to execute my code in spite of this so it works in the real environment

      previous MongoServerError: Transaction numbers are only allowed on a replica set member or mongos
      at Connection.onMessage (../../node_modules/@mikro-orm/mongodb/node_modules/mongodb/src/cmap/connection.ts:440:20)
      at MessageStream.<anonymous> (../../node_modules/@mikro-orm/mongodb/node_modules/mongodb/src/cmap/connection.ts:240:56)
      at processIncomingData (../../node_modules/@mikro-orm/mongodb/node_modules/mongodb/src/cmap/message_stream.ts:189:12)
      at MessageStream._write (../../node_modules/@mikro-orm/mongodb/node_modules/mongodb/src/cmap/message_stream.ts:70:5)
      return await this.em.transactional(async () => {
        const aggregate = await this.repository.findOneOrFail(
          storefrontId,
          productId,
        );
        aggregate.offerModificationRule(modificationRuleId, offerDuring);
        return await this.repository.save(aggregate);
      });
    });

I think I'd be ok if I could set transactions: false on the global config and then just having transaction code being called be a no-op (transaction wise).

@xenoterracide xenoterracide added the enhancement New feature or request label Jan 31, 2023
@B4nan
Copy link
Member

B4nan commented Jan 31, 2023

FYI you can have a replica set with in-memory mongodb too.

https://github.com/nodkz/mongodb-memory-server#replica-set-start

Kind of a duplicate of #3747, we could support both if we design this properly.

B4nan added a commit that referenced this issue Apr 23, 2023
Is it now possible to disable transactions, either globally via `disableTransactions` config option, or locally when using `em.transactional()`.

```ts
// only the outer transaction will be opened
await orm.em.transactional(async em => {
  // but the inner calls to both em.transactional and em.begin will be no-op
  await em.transactional(...);
}, { disableTransactions: true });
```

Alternatively, you can disable transactions when creating new forks:

```ts
const em = await orm.em.fork({ disableTransactions: true });
await em.transactional(...); // no-op
await em.begin(...); // no-op
await em.commit(...); // commit still calls `flush`
```

Closes #3747
Closes #3992
B4nan added a commit that referenced this issue Apr 23, 2023
Is it now possible to disable transactions, either globally via `disableTransactions` config option, or locally when using `em.transactional()`.

```ts
// only the outer transaction will be opened
await orm.em.transactional(async em => {
  // but the inner calls to both em.transactional and em.begin will be no-op
  await em.transactional(...);
}, { disableTransactions: true });
```

Alternatively, you can disable transactions when creating new forks:

```ts
const em = await orm.em.fork({ disableTransactions: true });
await em.transactional(...); // no-op
await em.begin(...); // no-op
await em.commit(...); // commit still calls `flush`
```

Closes #3747
Closes #3992
@B4nan B4nan closed this as completed in 8e8bc38 Apr 23, 2023
jsprw pushed a commit to jsprw/mikro-orm-full-text-operators that referenced this issue May 7, 2023
Is it now possible to disable transactions, either globally via
`disableTransactions` config option, or locally when using
`em.transactional()`.

```ts
// only the outer transaction will be opened
await orm.em.transactional(async em => {
  // but the inner calls to both em.transactional and em.begin will be no-op
  await em.transactional(...);
}, { disableTransactions: true });
```

Alternatively, you can disable transactions when creating new forks:

```ts
const em = await orm.em.fork({ disableTransactions: true });
await em.transactional(...); // no-op
await em.begin(...); // no-op
await em.commit(...); // commit still calls `flush`
```

Closes mikro-orm#3747
Closes mikro-orm#3992
derevnjuk added a commit to derevnjuk/tsed that referenced this issue Apr 23, 2024
derevnjuk added a commit to tsedio/tsed that referenced this issue Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants