![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I use PostgreSQL 7.4.7, and access it with JDBC (PostgreSQL Native Driver, PostgreSQL 7.4.5 JDBC3 with SSL (build 216)) |
#3
| |||
| |||
|
|
I do the following with postgresql. First I disable auto-commit, then create table tab ( col varchar(30), constraint colunq unique (col))"); commit insert into tab ( col ) values ('zack') insert into tab ( col ) values ('zack') The last command throws a UNIQUE VIOLATION (code 23505) as expected. Then I want to continue with: insert into tab ( col ) values ('zick') Then I get a IN FAILED SQL TRANSACTION (code 25P02). So the previous unique violation somehow made my transaction unusable. I tried the same with hsqldb/MySQL/Oracle but these do handle the situation gracefully. Any hints what I can do? I don't want to start a new transaction right after the unique violation, because I want to be able to commit or rollback the whole thing later. I use PostgreSQL 7.4.7, and access it with JDBC (PostgreSQL Native Driver, PostgreSQL 7.4.5 JDBC3 with SSL (build 216)) |
#4
| |||
| |||
|
|
Sorry, but there is no way to change this behaviour, it is the intended behaviour. When there is an error in a transaction, it is automatically aborted, and you can only roll it back. I don't know about MySQL (MySQL and transactions?), I forgot to mention, that I use the InnoDB engine only on MySQL. I never |
|
but Oracle definitely behaves similarly. The only difference is that Oracle will issue the ROLLBACK for you and starts a new transaction on the sly, while PostgreSQL requires you to issue the rollback. Try the following in Oracle (with autocommit off): insert into tab ( col ) values ('zock'); insert into tab ( col ) values ('zack'); insert into tab ( col ) values ('zack'); insert into tab ( col ) values ('zick'); select col from tab where col like 'z_ck'; It is best to have only those statements inside one transaction block that should either all fail or all succeed. |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
I tried this: insert into tab ( col ) values ('zock'); insert into tab ( col ) values ('zack'); insert into tab ( col ) values ('zack'); insert into tab ( col ) values ('zick'); commit; select col from tab; All my previously used databases do as I would expect: throw an error on the second insertion of "zack" and the final select return zack, zock and zick. "previously used databases" means hsqldb 1.8, mysql 4 and 5 with the innodb engine, Oracle 9 and 10. |
#7
| |||
| |||
|
|
I looked, and there is a way to get something similar in PostgreSQL by using savepoints. If you issue a SAVEPOINT prior to every INSERT, you can ROLLBACK TO SAVEPOINT when you encounter an error. Would that help with what you want to do? |
![]() |
| Thread Tools | |
| Display Modes | |
| |