Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Using DatabaseTransactions does not play well with app transactions #22

Closed
moltar opened this issue May 21, 2018 · 10 comments
Closed

Using DatabaseTransactions does not play well with app transactions #22

moltar opened this issue May 21, 2018 · 10 comments

Comments

@moltar
Copy link

moltar commented May 21, 2018

Test:

trait('DatabaseTransactions')

test('...', async ({ assert }) => {
  // this is created inside a trx1 via DatabaseTransactions
  const user = await Factory
    .model('App/Models/User')
    .create()

  const res = await userFacade.doStuff(user, 'foo')
})

App code (App/Facades/userFacade.js):

  async doStuff (user, foo) {
    // will start a new trx2, which then does not see the user previously created outside of this txn
    const trx = await use('Database').beginTransaction()

    await user.stuff().create({}, trx)
  }
@RomainLanz
Copy link
Member

Hey @moltar! 👋

This is a known issue that is going to be fixed.
In the meanwhile, I recommend you to change your code for the following.

const globalTrx = Database.connection('mysql')._globalTrx
const trx =  globalTrx ? globalTrx : await Database.beginTransaction()

// Do your stuff....
const article = await Article.create(metadata, trx)
await article.translations().create(translation, trx)

if (!globalTrx) {
  trx.commit()
}

@moltar
Copy link
Author

moltar commented May 22, 2018

What about savepoints? Knex supports that and could be an easy & quick fix.

@thetutlage thetutlage self-assigned this Jun 27, 2018
@s0ld13r
Copy link

s0ld13r commented Sep 6, 2018

Are there any updates on this issue?

@thetutlage
Copy link
Member

Nope not yet. However, if someone is interested in working on same. I can help in guiding them

@borie88
Copy link

borie88 commented Nov 12, 2018

This has been fixed in the latest Lucid release, right? @thetutlage

@viniciusoyama
Copy link

What if we create a trait for a DatabaseTruncate strategy?

@erikkallen
Copy link
Contributor

erikkallen commented Mar 25, 2019

Thnx for the workaround, I was wondering why transactions where not cleaning up the database after each test. This explains it.

@luisguilhermemsalmeida
Copy link

luisguilhermemsalmeida commented Apr 27, 2020

Hey guys! Do we have a solution to this problem?

@mateus4k
Copy link

mateus4k commented Jun 6, 2020

@luisguilhermemsalmeida apparently no changes so far

@fnoquiq
Copy link

fnoquiq commented Jun 25, 2020

Has this been fixed? (for adonis v4.1)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants