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

chore: handle transactions already started at the controller layer #4953

Merged
merged 5 commits into from
Oct 6, 2023

Conversation

gastonfournier
Copy link
Contributor

@gastonfournier gastonfournier commented Oct 6, 2023

About the changes

This PR adds a method to safeguard us from opening a new transaction while inside another transaction, resulting in two isolated transactions that will not be atomic (if one fails, the other might still complete successfully).

https://github.com/knex/knex/blob/bbbe4d4637b3838e4a297a457460cd2c76a700d5/lib/knex-builder/make-knex.js#L143C5-L144C88

We're currently opening transactions at the controller layer

await this.exportImportServiceV2.transactional((service) =>
service.import(dto, user),
);

but in some other places, we do it at the store level:

await this.db.transaction(async (tx) => {

Alternative

We can remove store-level transactions and move them to the controller following this approach:

const exportImportServiceV2 = db
? withTransactional(deferredExportImportTogglesService(config), db)
: withFakeTransactional(createFakeExportImportTogglesService(config));

await this.exportImportServiceV2.transactional((service) =>
service.import(dto, user),
);

This option is more expensive because we have to:

  1. Write the factory methods that propagate the transaction to the stores (therefore creating the store factory methods as well)
  2. Identify the methods for creating the transactions at the store level and backtrack the calls until the controller layer

@vercel
Copy link

vercel bot commented Oct 6, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
unleash-docs ❌ Failed (Inspect) Oct 6, 2023 11:25am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
unleash-monorepo-frontend ⬜️ Ignored (Inspect) Visit Preview Oct 6, 2023 11:25am

@@ -104,7 +104,7 @@ export interface IUnleashOptions {
versionCheck?: Partial<IVersionOption>;
telemetry?: boolean;
authentication?: Partial<IAuthOption>;
ui?: object;
ui?: IUIConfig;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice to have... probably I can remove it from this PR

@kwasniew kwasniew self-requested a review October 6, 2023 09:11
Copy link
Contributor

@kwasniew kwasniew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very elegant solution. I'd like us to handle transactions around the service boundaries but it's great for the transitional period.

@gastonfournier
Copy link
Contributor Author

Very elegant solution. I'd like us to handle transactions around the service boundaries but it's great for the transitional period.

Yes, I've added a comment in the method we're using

@gastonfournier gastonfournier merged commit 52fa872 into main Oct 6, 2023
8 of 9 checks passed
@gastonfournier gastonfournier deleted the set-project-state branch October 6, 2023 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants