Deferring Constraint Checking
In the Oracle Lite documentation, there is a section:
==========================================================
3.11.3.2 Defer Constraint Checking Until After All Transactions Are Applied
1. Drop all foreign key constraints and then recreate them as DEFERRABLE constraints.
2. Bind user-defined PL/SQL procedures to publications that contain tables with referential integrity constraints.
3. The PL/SQL procedure should set constraints to DEFERRED in the BeforeApply function and IMMEDIATE in the AfterApply function as in the following example featuring a table named SAMPLE3 and a constraint named address.14_fk:
==========================================================
For step 1, I am assuming this is on the base table that the publication references, since it is the one throwing the FK constraint violations at me.
For step 2 however, are these procedures to be bound to the base tables as well, or somewhere else related to where the publication items are stored?
Thanks,
Allen
In this case, the data model that I have to work with causes this situation
TABLE A has 2 FK constraints to TABLE B
TABLE B has 1 FK constraint to TABLE A
In Oracle Lite, I:
1-create a record in TABLE A where both of the columns with FK constraints are null
2-create a record in TABLE B which refers to the original in TABLE A
3-create a second record in TABLE B which also refers to the record in TABLE A
4-update the record in TABLE A to refer to the 2 records in TABLE B
When these changes reach the mobile server, they are placed in the error queue, because steps 1 and 4 are being combined and performed as one single insert. Since the records from step 2 and 3 don't exist yet, there is an FK violation.
Is there a way to prevent the synchronization process from 'opitimizing' the transaction and combining steps 1 and 4? Everything works fine if I follow steps 1-4, but when 1 and 4 are combined it all falls apart.
Thanks,
Allen
Similar Messages
-
Influence of deferred constraint to Table is mutating proble
Hello,
i have a question regarding the 'Table is mutating,
Trigger/Function may not see it'-problem. I
wondered whether a deferrable constraint can solve the problem,
but as I tested it, it's the same
behaviour as before.
Situation:
assume tables LOC_TO and TORDER and the following constraint:
ALTER TABLE LOC_TO ADD (CONSTRAINT LT_TOID FOREIGN KEY (LT_TOID)
REFERENCES TORDER ON DELETE CASCADE
DEFERRABLE INITIALLY DEFERRED);
assume a trigger on table TORDER:
create or replace trigger tIU_TORDER_ADD
after insert or update of TO_STATE on TORDER
for each row
declare
begin
insert
into loc_to
( lt_locid,
lt_toid )
values
( 'HRL14042',
:new.to_id )
end tIU_TORDER_ADD;
I thought, that on a deferred constraint at the triggered action
no check to TORDER is done, and
that this fact will prevent the ORACLE_ERROR ORA-04091.
Can anyone tell me, why this doesn't work?
Thanks
Titus Leskien
nullThere was a loophole in the mutating table gotcha: single row inserts in a BEFORE EACH ROW trigger were not considered mutating, but were in AFTER EACH ROW triggers. This did not apply to INSERT INTO ... SELECT ... statements, which always mutated, even if they only inserted one row.
This is get-out is no longer included in the documentation, but as you have found still applies in the 9.2 database (and it's unlikely Oracle will have broken it in 10g).
Cheers, APC -
EjbRemove fails with deferred constraints in 8.1.6
I get a constraint violation under certain conditions from Oracle.
IBM ejb container hides the exception, and raises a CSI_ROLLBACK exception. The only workaround I can think of, is disabling all constraints in Oracle, and trusting IBMs Application server to guarantee the
referential integrity of the database.
Does anyone else notice these problems and found a resolution?
Oracle introduced deferred constraints to allow the constraint violation checking at the end of an transaction.
For the IBM implementation of entity EJBs this is essential,
because any the "DELETE FROM WHERE id=" which result from an ejbRemove() happens >>>before<<< an ejbStore() which results in "UPDATE SET foreignkey = NULL ".
I am using the jdbc driver from classes12.zip, and noticed that
you get an constraint violation on a commit, when the record has been created in a transaction, is updated in a transaction, and then is deleted in a transaction. The transactions are isolated.
A rollback, and deleting again resolves the problem.
But the problem is that an Websphere Application server is in between the
delete from a user, and the jdbc driver accessed through a datasource.
nullDoes anyone know where to configure and find required settings (e.g. the number of open_cursors) for Oracle 8.1.6 server when using the jdbc thin driver ?
null -
Reporting exceptions on deferred constraints
Is it possible to report exceptions on deferred constraints? I am using this mechanism to load tables in an arbitrary order and to prevent FK violations whilst loading.
As a starting point, the script below works as expected (enabling and disabling):
CREATE TABLE EXCEPTIONS
( ROW_ID ROWID
, OWNER VARCHAR2(30)
, TABLE_NAME VARCHAR2(30)
, CONSTRAINT VARCHAR2(30)
CREATE TABLE EMP( ENAME VARCHAR2(10));
ALTER TABLE EMP ADD CONSTRAINT EMP_UK UNIQUE (ENAME) DEFERRABLE;
ALTER TABLE EMP DISABLE CONSTRAINT EMP_UK;
INSERT INTO EMP VALUES ('SMITH');
INSERT INTO EMP VALUES ('SMITH');
ALTER TABLE EMP ENABLE CONSTRAINT EMP_UK EXCEPTIONS INTO EXCEPTIONS;
SELECT * FROM EXCEPTIONS;
However, I don't want to disable constraints, because the application may be performing DML in another session.
So in the script below I'm deferring the constraints instead of disabling them. But how can I report the constraint violations in this scenario?
CREATE TABLE EXCEPTIONS
( ROW_ID ROWID
, OWNER VARCHAR2(30)
, TABLE_NAME VARCHAR2(30)
, CONSTRAINT VARCHAR2(30)
CREATE TABLE EMP( ENAME VARCHAR2(10));
ALTER TABLE EMP ADD CONSTRAINT EMP_UK UNIQUE (ENAME) DEFERRABLE;
ALTER SESSION SET CONSTRAINTS=DEFERRED;
INSERT INTO EMP VALUES ('SMITH');
INSERT INTO EMP VALUES ('SMITH');
-- Which statement goes here to report constraint violations?
SELECT * FROM EXCEPTIONS;
The statement:
SET CONSTRAINTS ALL IMMEDIATE;
will validate deferred constraints and result in a SQL Error: ORA-00001: unique constraint (SCOTT.EMP_UK) violated
But this does not tell me the rows.
The statement:
ALTER TABLE EMP ENABLE CONSTRAINT EMP_UK EXCEPTIONS INTO EXCEPTIONS;
results in the following error, and no rows in the exceptions table:
SQL Error: ORA-02091: transaction rolled back
ORA-00001: unique constraint (SCOTT.EMP_UK) violated
Is there any way to report the violations?
At the end, I'd like to report ALL violating rows to the user.
The best I can come up with is, for each constraint perform:
SET CONSTRAINT EMP_UK IMMEDIATE;
and in the exception handler explicitly query for duplicates in the table:
SELECT ENAME FROM EMP GROUP BY ENAME HAVING COUNT(*) > 1;
I hope there is a better way.I believe this will not be usable in a scenario in which the tables are loaded in the "wrong" order. The database will only be consistent after all tables have been loaded. Using DML error logging will produce false errors. Please correct me if I'm wrong.
-
Constraint Checking Vs After Row Trigger
According to db concepts Chap 17 Trigger -> Trigger Execution, integrity checking is performed BEFORE the after row trigger executed. I guess this is not 100% true. AFAIK, we can create an AFTER ROW trigger on a child table which automatically insert new referenced row in the parent table. e.g.
CREATE TABLE child (
child_id int primary key,
parent_id int references parent,
CREATE TABLE parent (
parent_id int primary key,
CREATE OR REPLACE TRIGGER A
AFTER INSERT OR UPDATE on child
FOR EACH ROW
BEGIN
INSERT INTO parent values (:new.parent_id,...);
END;
If the constraint checking is done before the trigger, the referential integrity should have been violated when the child row is inserted.
So if the documentation is wrong, when will the constraint checking be carried out with respect to trigger execution.There are plenty of things that are easier and/or safer to do in an AFTER ROW trigger, since can see exactly what the data in the row has been set to by any BEFORE ROW triggers and you can't change the data inadvertently. If your BEFORE ROW triggers are calling relatively complex procedures owned by various disparate groups of developers, it can be non-trivial to ensure that every groups changes are perfectly coordinated. AFTER ROW triggers are a heck of a lot easier to deal with here because you don't have to worry about another section of code changing the data you're using to make your decision.
I'm hard pressed to think of something that couldn't possibly be accomplished in a BEfORE ROW trigger, though I suspect there are at least a few oddball situations.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
Error 90548: Disabling SSL constraint checking
I'm trying to setup some LDAPAuthenticators (WLS 10.1) to connect securely with SSL. All the servers have valid keystores they use already, so I just switched to port 636, checked the SSLEnabled box, and setup a user and credential to connect as. But when the Admin server starts, it gives the error:
<090548> The certificate chain received from peer contained a V3 CA certificate which was missing the basic constraints extension.
Looking at the error codes, it says a work around is "...disable the basic constraints checking to allow the certificate chain to be accepted..." but I cannot find this anywhere in the admin console. Does anyone know what property this is talking about?
Mike
Update
Found out it's a command line switch: -Dweblogic.security.SSL.enforceConstraints=off
http://edocs.bea.com/wls/docs100/secmanage/ssl.html#wp1194486
Edited by: prattm on Feb 4, 2009 7:51 PMAny one has any idea??/
-
Reporting of deferred constraint failures
Hello
I'm running Kodo 3.1.4 in WebLogic 8.1 against Oracle 9. I'd like to run
with deferred constraints but there are problems reporting failures of
these constraints: the database rolls back but no exception is thrown by
weblogic or KoDo. The user is therefore presented with a silent failure
(though the correct exception is written into the weblogic container log).
Running with DataSourceMode set to enlisted (rather than local) produces
the expected exceptions when constraints fail but the constraints are no
longer correctly deferred (they are tested after each statement).
I'm all out of ideas. Can anyone point me in the right direction?
TIA
RobertThere should be some sort of client exception... are you sure that you
are not consuming it? Are you using a WL datasource?
Robert Donkin wrote:
Hello
I'm running Kodo 3.1.4 in WebLogic 8.1 against Oracle 9. I'd like to run
with deferred constraints but there are problems reporting failures of
these constraints: the database rolls back but no exception is thrown by
weblogic or KoDo. The user is therefore presented with a silent failure
(though the correct exception is written into the weblogic container log).
Running with DataSourceMode set to enlisted (rather than local) produces
the expected exceptions when constraints fail but the constraints are no
longer correctly deferred (they are tested after each statement).
I'm all out of ideas. Can anyone point me in the right direction?
TIA
Robert--
Steve Kim
[email protected]
SolarMetric Inc.
http://www.solarmetric.com -
Deferred constraints & XAExceptions details
When executing an XA transaction on Oracle 8i with a J2EE server, we are not getting enough details in the OracleXAException that occurs as a result of a deferred constraint being violated (during the commit).
We're getting a oracle.jdbc.xa.OracleXAException (wrapped inside a TransactionRolledBackException), and upon further inspection I can call the OracleXAException methods:
getOracleError() : int
getOracleSQLError() : int
I cannot get any textual details about the error (ie. which constraint was violated).
Do other version of Oracle provide more details during XA transaction failures?
Thanks,
M<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by SAMEER DESHPANDE ([email protected]):
Hello
I would like to know the use of Deferred Constraints.
At the time of COMMMIT, the Oracle ROLLBACKS the transaction if I set "SET CONSTRAINT ALL DEFERRED;"...
What is the use of DEFERRED CONSTRAINTS...?
Thanks
Sameer<HR></BLOCKQUOTE>
You can do "SET CONSTRAINT ALL IMMEDIATE" before committing. That statement will raise an error, without rolling back the transaction, if there are violated constraints. -
Hi,
One of our projects is using Kodo to link Java to the Oracle db I works fine with 8.1.7 but we need to connect to a Lite db.
Kodo is running this query which fails because Oracle Lite does not have a DEFERRED column in the ALL_CONSTRAINTS table. I can't find any info on deferred constraints in Lite.
Does Lite handle constraints in the same way?
Is there a workaround ?
Peter
SELECT t2.OWNER AS PKTABLE_SCHEM,
t2.TABLE_NAME AS PKTABLE_NAME,
t2.COLUMN_NAME AS PKCOLUMN_NAME,
t0.OWNER AS FKTABLE_SCHEM,
t0.TABLE_NAME AS FKTABLE_NAME,
t0.COLUMN_NAME AS FKCOLUMN_NAME,
t0.POSITION AS KEY_SEQ,
DECODE (t1.DELETE_RULE,
'NO ACTION', 3,
'RESTRICT', 1,
'CASCADE', 0,
'SET NULL', 2,
'SET DEFAULT', 4) AS DELETE_RULE,
t0.CONSTRAINT_NAME AS FK_NAME,
DECODE (t1.DEFERRED,
'DEFERRED', 5,
'IMMEDIATE', 6) AS DEFERRABILITY
FROM ALL_CONS_COLUMNS t0,
ALL_CONSTRAINTS t1,
ALL_CONS_COLUMNS t2
WHERE t0.OWNER = t1.OWNER
AND t0.CONSTRAINT_NAME = t1.CONSTRAINT_NAME
AND t1.CONSTRAINT_TYPE = 'R'
AND t1.R_OWNER = t2.OWNER
AND t1.R_CONSTRAINT_NAME = t2.CONSTRAINT_NAME
AND t0.POSITION = t2.POSITION;Peter,
Please let us know if you still need help with this post.
Thank you. -
Deferred Constraints - error on inserting
Hi there,
i've got a very courios problem with deferred constraints. My table creates are:
CREATE TABLE mitglied
( svnr NUMBER(10) NOT NULL,
instr VARCHAR(20) NOT NULL,
CONSTRAINT mitglied_pk PRIMARY KEY (svnr));
CREATE TABLE abteilung
( instr VARCHAR(20) NOT NULL,
geleitetVon NUMBER(10) NOT NULL,
stvVon NUMBER(10) NOT NULL,
CONSTRAINT abteilung_pk PRIMARY KEY (instr),
CONSTRAINT abteilung_geleitetVon_fk FOREIGN KEY (geleitetVon)
REFERENCES mitglied(svnr) INITIALLY DEFERRED DEFERRABLE,
CONSTRAINT abteilung_stvVon_fk FOREIGN KEY (stvVon)
REFERENCES mitglied(svnr) INITIALLY DEFERRED DEFERRABLE);
ALTER TABLE mitglied
ADD CONSTRAINT mitglied_fk FOREIGN KEY (instr)
REFERENCES abteilung(instr) INITIALLY DEFERRED DEFERRABLE;My Inserts (from sql-file):
INSERT INTO mitglied VALUES ('1833040984', 'Edwin Neugebauer', '1984', '20-JAN-2005', 'Posaune');
<all of the other "mitglied"-inserts>
INSERT INTO abteilung VALUES ('Posaune', '1833040984', '1512101070');
<all of the other "abteilung"-inserts>
COMMIT;The Error msg:
Error starting at line 79 in command:
INSERT INTO mitglied VALUES ('1833040984', 'Edwin Neugebauer', '1984', '20-JAN-2005', 'Posaune')
Error report:
SQL Error: ORA-02091: transaction rolled back
ORA-02291: integrity constraint (U0426435.MITGLIED_FK) violated - parent key not found
(it's the same for all of the other mitglied-inserts), and of course, I get also errors on the abteilung-inserts.
I've already tried to solve this problem by using "SET AUTOCOMMIT OFF" in sqlplus, but that wasn't the point.
What's wrong with my Constraints?Please post a complete example cut-n-paste from SQL*Plus window that shows the exact output step by step (as like below).
It apparently works for me as shown below.
SQL> CREATE TABLE mitglied
2 ( svnr NUMBER(10) NOT NULL,
3 mName VARCHAR(30) NOT NULL,
4 gJahr INTEGER NOT NULL,
5 aDat DATE NOT NULL,
6 instr VARCHAR(20) NOT NULL,
7 CONSTRAINT mitglied_pk PRIMARY KEY (svnr));
Table created.
SQL> CREATE TABLE abteilung
2 ( instr VARCHAR(20) NOT NULL,
3 geleitetVon NUMBER(10) NOT NULL,
4 stvVon NUMBER(10) NOT NULL,
5 CONSTRAINT abteilung_pk PRIMARY KEY (instr),
6 CONSTRAINT abteilung_geleitetVon_fk FOREIGN KEY (geleitetVon)
7 REFERENCES mitglied(svnr) INITIALLY DEFERRED DEFERRABLE,
8 CONSTRAINT abteilung_stvVon_fk FOREIGN KEY (stvVon)
9 REFERENCES mitglied(svnr) INITIALLY DEFERRED DEFERRABLE);
Table created.
SQL> ALTER TABLE mitglied
2 ADD CONSTRAINT mitglied_fk FOREIGN KEY (instr)
3 REFERENCES abteilung(instr) INITIALLY DEFERRED DEFERRABLE;
Table altered.
SQL> INSERT INTO mitglied VALUES ('1833040984', 'Edwin Neugebauer', '1984', to_date('20-JAN-2005', 'dd-mon-yyyy'), 'Posaune');
1 row created.
SQL> INSERT INTO abteilung VALUES ('Posaune', '1833040984', '1512101070');
1 row created.
SQL> commit ;
commit
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-02291: integrity constraint (OPS$LAPTOP\KKISHORE.ABTEILUNG_STVVON_FK)
violated - parent key not found
SQL>Message was edited by:
Kamal Kishore -
Hi,
What's the difference between initially immediate and NOT DEFFERABLE? so initially deferred waits to check the constraint until the transaction ends. But when?! After issuing commit/rollback?
and initially immediate checks the constraint at the statement execution. so in my opinion INITIALLY IMMEDIATE is same as NOT DEFERRABLE.
By default the constraint is NOT DEFERRABLE, so the validity is checked immediately after the DML statement.
thanks
Edited by: Roger22 on 09.09.2011 14:08Also, something else to be aware of, which is not necessarily a difference
the ROLLBACK occurs on all transactions, even those that were succecsfull when DEFERRED, and a COMMIT failed on a check;
Whereas when IMMEDIATE, the rollback would occur only on the transaction that failed.
SQL> insert into t1 values (1,1);
1 row created.
SQL> commit;
Commit complete.
SQL> insert into t2 values (1);
1 row created.
SQL> insert into t2 values (2);
insert into t2 values (2)
ERROR at line 1:
ORA-02291: integrity constraint (XDB_DEV3.T2_FK) violated - parent key not found
SQL> select * from t2;
A
1
SQL> rollback;
Rollback complete.
SQL> select * from t2;
no rows selected
SQL> alter session set constraints = deferred;
Session altered.
SQL> insert into t2 values (1);
1 row created.
SQL> insert into t2 values (2);
1 row created.
SQL> commit;
commit
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-02291: integrity constraint (XDB_DEV3.T2_FK) violated - parent key not found
SQL> select * from t2;
no rows selected -
Deferable Constraint of NO USE
I heard deferrable constraint do a check at the end of
transaction...
I have statement like this in java
insert into master(parent_id) values (2);
insert into child(child_id) values (2);
commit;
I have the relation like this
master.parent_id-->child.child_id
Deferable initially Deferred
When I try to run it it gives me
violated - parent key not found
If it give me this error what is the deferrable constraint used for ?Sorry for above post..
I was little mislesaded in code.. -
Feedback deferred constraint is incorrect in sql developer 2.1.0.63.41
In the earlier 2.1.0.63 the set contraint didnt work entirely in sql developer.
In this new release the set constraint can be executed and it works, however when you violate a constraint there is no feed back.
example:
I create the following table and insert 2 rows
CREATE TABLE d_orafout
( code NUMBER CONSTRAINT d_codepk PRIMARY KEY DEFERRABLE,
message VARCHAR2(50))
INSERT INTO d_orafout VALUES(904,'invalid table name')
INSERT INTO d_orafout VALUES(903,'invalid column name')
Then i execute:
set constraint d_codepk deferred;
set succeeded.
Then an update that violates the primary key:
update d_orafout set code = 903 where message = 'invalid table name';
1 rows updated
Then when i commit i should get the following error (which works fine in SQL*Plus):
ORA-02091: transaction rolled back
ORA-00001: unique constraint (NWG806.D_CODEPK) violated
However the commit gives me the following output:
commit;
commited
The transaction is not commited, it is rolledback but i dont get the error i need.Hi <please supply your name>,
Thanks for this feedback,
I have replicated this issues and I have logged a bug.
Bug 9201183 - COMMIT REPORTING COMMIT INCORRECTLY WHEN USED WITH SET CONSTRAINT DEFERRED
Regards,
Dermot.
SQL Developer Team -
Deferred constraints and transaction
Hello.
Let me explain my problem:
1. I have table TBL_BASE in database Oracle with one column - ID (Primary Key)
2. I have several entity bean with CMP which mapped to according tables in database,
all of which has foreign key to TBL_BASE.ID.
3. All FK constraints is DEFERRABLE, INITIALY DEFERRED.
4. All methods in entity are marked as REQUIRED transaction.
5. In ejbRemove method I try to remove the record from TBL_BASE first - I must
be able do that because I have not committed transactio
The problem is I receive error - that I cant do that because Foreign Key violation.
It's look like that WebLogic try call commit on exit of method ejbRemove before
real delete identity from database.
Sorry.
I found the issue. The problem was that I used DataSource in the ejbRemove. I
change it ot TXDataSource and everything are working now. Thank you.
"Yuriy Kuzmenko" <[email protected]> wrote:
>
>Hello.
>Let me explain my problem:
>1. I have table TBL_BASE in database Oracle with one column - ID (Primary
>Key)
>2. I have several entity bean with CMP which mapped to according tables
>in database,
>all of which has foreign key to TBL_BASE.ID.
>3. All FK constraints is DEFERRABLE, INITIALY DEFERRED.
>4. All methods in entity are marked as REQUIRED transaction.
>5. In ejbRemove method I try to remove the record from TBL_BASE first
>- I must
>be able do that because I have not committed transactio
>
>The problem is I receive error - that I cant do that because Foreign
>Key violation.
>It's look like that WebLogic try call commit on exit of method ejbRemove
>before
>real delete identity from database.
>
-
Deferred constraint at session level
Can you define a specific constraint to be deferred at the session level?
I know that all deferrable constraints can be set deferred at the[b] session level:
ALTER SESSION SET CONSTRAINTS = DEFERRED;
But what if I want a certain constraint only to be set deferred at the session level?
thank uSET CONSTRAINT[S]
Maybe you are looking for
-
Problem with clob_write under client 9.2.0.6: UnsatisfiedLinkError
Hi all, I'm running Oracle client 9.2.0.4 on Windows XP and Java 1.4.1_3. Writing or reading clob's works pretty well. As i tried to update to Oracle client 9.2.0.6, i got the error Communication failed: java.lang.UnsatisfiedLinkError: clob_write whe
-
Should We consider Temp Data Files While Estimating The Database Size
Hi, The Database Size is sum of physical files like Control file redo log file datafiles temp files so i want to know why are we considering the temp files..Because it's temporary. At one stage of database, temp size it could me more and at one stage
-
Business Objects Universes generation
Hi everyone, We wish to install and use Web Intelligence (Business Objects Enterprise Premium). I understand that Web Intelligence requires universes and that they can be generated via Universe Designer or Universe Builder. With these tools, we can g
-
First off, I never imagined networking could be so hard. I'm trying to wirelessly network my laptop and my desktop through a Linksys WRT54GL. Neither computer can 'see' the other one, despite them being in the same workgroup. I managed to get them ne
-
I am a songwriter and want to upload my work to itune but I need a few question cleared up. 1. Does Itune interact with the Performing Arts organization such as BMI in order for artist to be paid or do i just set up a paypal account and pay artis