Coder Social home page Coder Social logo

Comments (7)

iheanyi avatar iheanyi commented on May 24, 2024 3

Okay, figured out this bug. We've cut a release of v1.10.0 which fixes this error. Thanks for the report, your reproduction repo was really helpful in debugging it!

from database-js.

iheanyi avatar iheanyi commented on May 24, 2024 2

Hey there @jacobwgillespie and @OultimoCoder . We've put this on the radar and hope to get around to patching it ASAP. Thank you for opening this ticket and making a repo with a reproduction, it's greatly appreciated.

from database-js.

OultimoCoder avatar OultimoCoder commented on May 24, 2024 2

Upgraded the package in all my repos that use this and tests all pass (had tests failing due to this error before). Thank you!

from database-js.

jacobwgillespie avatar jacobwgillespie commented on May 24, 2024

On ^, the minimal subset of the reproduction code at https://github.com/jacobwgillespie/kysely-planetscale-issue-20 is this:

  const conn = connect(config.database)
  try {
    await conn.transaction(async (trx) => {
      const userId = await trx.execute(
        'insert into `user` (`name`, `email`, `is_email_verified`, `password`, `role`) values (?, ?, ?, ?, ?)',
        [name, email, true, null, 'user']
      )
      await trx.execute(
        'insert into `authorisations` (`user_id`, `provider_type`, `provider_user_id`) values (?, ?, ?)',
        [Number(userId.insertId), providerType, providerUserId]
      )
      return userId
    })
  } catch (error) {
    await conn.execute('delete from `user` where `user`.`email` = ?', [email])
    throw new Error('User already exists!')
  }

The first insert into 'user' fails as it's a duplicate of an existing row's email value - this throws an error, but then the delete from 'user' in the catch block is stuck on a lock.

from database-js.

mattrobenolt avatar mattrobenolt commented on May 24, 2024

@jacobwgillespie just out of curiosity if you don't mind, inside of the catch block, if you do await conn.execute("rollback") does that allow things to continue correctly?

In theory we should be doing it here automatically https://github.com/planetscale/database-js/blob/main/src/index.ts#L189

Just not 100% sure without digging in why that wouldn't be working.

from database-js.

jacobwgillespie avatar jacobwgillespie commented on May 24, 2024

@mattrobenolt Adding await conn.execute('ROLLBACK') did not free the lock — the rollback returns without an error, but then the delete from 'user' still times out due to the lock.

from database-js.

jacobwgillespie avatar jacobwgillespie commented on May 24, 2024

I also tried just issuing the transaction commands directly, since I know transaction() is creating a second connection, and that also fails with the lock timeout:

  try {
      await conn.execute('BEGIN')
      const userId = await conn.execute(
        'insert into `user` (`name`, `email`, `is_email_verified`, `password`, `role`) values (?, ?, ?, ?, ?)',
        [name, email, true, null, 'user']
      )
      await conn.execute('COMMIT')
  } catch (error) {
    await conn.execute('ROLLBACK')
    await conn.execute('delete from `user` where `user`.`email` = ?', [email])
    throw new Error('User already exists!')
  }

from database-js.

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.