Skip to content

Confusion about transaction code: #1618

@smhk

Description

@smhk

My transaction code:

try {
    const cn = await pool.getConnection()
    await cn.beginTransaction()

    await cn.execute(' ...
    await cn.execute(' ...

    await cn.commit()
}
catch {
    await cn.rollback()
}

But am I wrong? Should it be:

const cn = await pool.getConnection()
try {
    await cn.beginTransaction()

    await cn.execute(' ...
    await cn.execute(' ...

    await cn.commit()
}
catch {
    await cn.rollback()
}

But wait! Should it be?

const cn = await pool.getConnection()
await cn.beginTransaction()
try {
    await cn.execute(' ...
    await cn.execute(' ...

    await cn.commit()
}
catch {
    await cn.rollback()
}

I'm afraid I don't know the answer. Thanks in advance anyone who can advise. (It may help others also?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions