1

I am using pg/transaction to handle a complex process. It can be simplified as this:

async() => {
    const client = await new Pool().connect();
    try {
        await client.query("BEGIN");
        await client query("query 1");
        try {
            await client query("query 2");
            // Some error happen
        } catch(err) {
            console.log(err);
        }
        await client query("query 3");
        await client query("COMMIT");
    } catch(err) {
        await client.query("ROLLBACK");
    } finally {
        client.release
    }
}

The problem is that when query2 returns an error, the client fails to execute query3 even I don't Rollback the process. The error it returns is current transaction is aborted, commands ignored until end of transction block. Is there any way to prevent the transaction from being aborted when a query error happends?

1 Answer 1

2

This is the default behaviour because it is the whole point of a transaction. If you don't want queries 1,2,3 to all succeed or all fail together you probably don't want them in the same transaction.

However, if what you want is really "1 and (2 or 3)" then you should put a SAVEPOINT after query 1 and rollback to that savepoint in the event of an error in query 2.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.