4

I have a very extensive PHP update script based on PHP/Yii that updates a database on different database types (MSSQL, Postgres and MySQL).

The whole script runs within a transaction. However there are some statements that lead to a query error (for example if a certain key already exists on a table). I surrounded these with try/catch statements - this works fine so far in MySQL

However on Postgres once an invalid query was issued the transaction is automatically failing. The following error message is shown for all following statements:

CDbCommand failed to execute the SQL statement: SQLSTATE[25P02]: In failed sql transaction: ERROR: current transaction is aborted, commands ignored until end of transaction block

But what I need is Postgres to continue the transaction because I want to decide in the application when to roll back - or some way to clear out the error and continue the transaction.

How to do that?

2
  • What will be the next step if the script detects a "duplicate key error" ? Commented Dec 23, 2012 at 11:59
  • @wildplasser: Some other unrelated statement. Commented Dec 23, 2012 at 12:42

1 Answer 1

5

Use a savepoint for that purpose:

BEGIN; -- transaction starts here

-- do things if you want

SAVEPOINT my_savepoint;

-- failing statement here
-- all other statements are ignored

ROLLBACK TO SAVEPOINT my_savepoint;

-- continue your transaction starting from my_savepoint

COMMIT;
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.