Oracle 26ai JSON Duality Views: A Missing Field Deleted the Rows. A Missing ETAG Lost the Update.
Oracle 23ai introduced JSON-relational duality views, and 26ai keeps them: the application reads and writes a JSON document, and the database stores it in ordinary tables. An order with its lines is one document to the app, and two tables to every report, batch job and CDC pipeline. The selling point is that each side gets the shape it wants, with no mapping layer in between.
The part that decides whether that works is the write path. Oracle describes it as optimistic, lock-free concurrency control built on an ETAG in each document. I built a lab on Oracle AI Database 26ai Free (23.26.3) and wrote to one order the way real applications do: read it, change it in memory, send it back. Three results matter:
- An app that sent its copy back without the ETAG got no error, and undid another app’s committed change.
- A document that left out one of its lines deleted that line’s row, with a valid ETAG.
- While a batch job had one line updated and not yet committed, every write through the view to that order
failed at once with
ORA-40896.
The setup
Two tables and one view over them:
create table orders (
order_id number primary key,
customer varchar2(30),
status varchar2(10),
internal_note varchar2(30) default 'none');
create table order_lines (
line_id number primary key,
order_id number not null references orders,
sku varchar2(20),
qty number);
create json relational duality view orders_dv as
orders @insert @update @delete
{ _id : order_id, customer : customer, status : status,
lines : order_lines @insert @update @delete
[ { lineId : line_id, sku : sku, qty : qty } ] };
INTERNAL_NOTE is deliberately left out of the view. Order 1 has three lines, 11, 12 and 13, with quantities 10,
20 and 30. Reading it through the view returns the document with a _metadata object:
{
"_id" : 1,
"_metadata" : { "etag" : "02D0A78DA7087D7BC762349CFEEAD6DC", "asof" : "0000000000227EF2" },
"customer" : "acme",
"status" : "open",
"lines" : [ { "lineId" : 11, "sku" : "bolt", "qty" : 10 }, ... ]
}
The ETAG is what makes it optimistic. An app sends the document back with the ETAG it read, the database compares it with the current one, and if someone changed the document in between, the write fails.
The ETAG check only runs if the ETAG is there
App A reads order 1 and keeps the document. App B sets line 11’s quantity to 99 through the view and commits. Then
app A, which knows nothing about that, changes the status to shipped and writes its copy back.
With the ETAG it read:
ORA-42699: Cannot update JSON Relational Duality View 'ORDERS_DV': The ETAG of document with ID 'FB03C10200'
in the database did not match the ETAG passed in.
That’s the design working. Nothing changed, line 11 is still 99, and A has to read the document again.
Now the same copy, with _metadata removed before the write. That happens more often than it sounds: a DTO that
doesn’t map _metadata, a client that builds the document from its own model, a serializer that drops fields it
doesn’t know:
1 row updated.
Commit complete.
LINE_ID QTY
11 10
No error. Line 11 is back to 10, and B’s committed 99 is gone. A replacement document without an ETAG isn’t checked at all; it’s written as it is. Oracle’s concurrency chapter describes writes being rejected when the ETAG doesn’t match; I couldn’t find it saying what happens when there’s none. This is what happens.
The control: the same status change made in the database, with JSON_TRANSFORM on the current document, kept B’s
99. It changes one field of whatever is stored now, so there’s nothing stale to write back.
A missing array element is a delete
App A’s next screen shows only the first two lines of an order. It sends back the document it has, with lines 11 and 12, and the current ETAG:
1 row updated.
LINE_ID QTY
11 10
12 20
Line 13 is gone from ORDER_LINES. The document is the whole truth for the rows it maps: an element that isn’t in
the array is a row that shouldn’t exist, and @delete on ORDER_LINES allows the view to delete it. The ETAG was
valid, so the check passed. It protects against a stale document, not against an incomplete one.
A document with no lines field at all deleted all three rows.
Without @delete on the lines (ORDERS_NODEL_DV in the lab), the same write failed instead:
ORA-40938: Cannot delete from table 'ORDER_LINES' in JSON Relational Duality View 'ORDERS_NODEL_DV':
Missing DELETE annotation or NODELETE annotation specified.
Two more things the view is strict about, both measured:
- A field the view doesn’t define is rejected. A document with an extra
priorityfield failed withORA-40944: ... The input JSON document is invalid., plus an internalJZN-00651code and nothing that names the field. - Columns the view doesn’t map are left alone.
INTERNAL_NOTEsurvived a full replace of the document.
A write through the view never waits
A nightly batch job updates ORDER_LINES directly, row by row, and commits at the end. While it has an uncommitted
change to line 13, the app writes to order 1 through the view:
-- change the status (a different row from the batch job's):
ORA-40896: Cannot update JSON Relational Duality View 'ORDERS_DV': Concurrent modification detected to
document with ID 'FB03C10200'. -- after 37 ms
-- change line 13:
ORA-42698: Cannot update JSON Relational Duality View 'ORDERS_DV': Concurrent modification detected to
field 'lines' of document with ID 'FB03C10200'. -- after 38 ms
Neither write waited for the batch job, and the first one didn’t even touch its row: the status lives in ORDERS. A
full document sent back from the app’s copy, with the current ETAG, failed the same way. The view refuses to write a
document that has an open change to any of its mapped columns, and it refuses at once.
An open change to INTERNAL_NOTE, which the view doesn’t map, is narrower. It didn’t block a write of line 13. A
write of the status, which is in the same ORDERS row, failed at once with ORA-42698, this time naming table
ORDERS. In none of these cases did the write through the view wait.
The other direction behaves like plain SQL. With a write through the view open on line 11:
| base-table write | result |
|---|---|
| update line 13 | no wait (49 ms) |
| update line 11, the row the view changed | waited 11.5 s, until the view’s transaction ended |
So the batch job queues on row locks as it always did, and the document writers get an error instead of a queue. Once
a write through the view has succeeded, it’s an ordinary transaction: when the batch job’s change to line 13
committed while the view’s write to line 11 was still open, the view’s COMMIT worked, and both changes were stored.
A long batch transaction over the base tables means every app write to the documents it touches fails for as long as that transaction is open. The error says what to do: read the latest document and try again. The app needs that loop, with a limit, before it goes live.
What the ETAG is, and what a write costs
The ETAG is a hash of the document’s content, not a version number. In the lab:
- changing
INTERNAL_NOTEleft it as it was; - changing line 11 from 10 to 12 changed it;
- changing it back to 10 brought back the original ETAG,
02D0A78DA7087D7BC762349CFEEAD6DC.
The documentation calls this value-based, or content-based, concurrency control. A column can be left out of the
hash with the NOCHECK annotation, and the ORA-42698 help text suggests exactly that for fields where concurrent
changes don’t matter. The lab doesn’t test NOCHECK.
A full replace doesn’t rewrite every row. Sending the whole document back with only the status changed wrote one
undo record, the same as UPDATE orders SET status = .... The database compares the document with the rows and
changes only what differs. That’s good news for redo, and for any CDC on the base tables, which sees the row changes,
not the documents.
Find out what your apps send
Before an app writes through a duality view in production:
- Make the ETAG mandatory in your code. The database won’t. Every replace should carry the
_metadatait read, and a test should fail if it doesn’t. - Prefer changing fields to replacing documents where you can:
JSON_TRANSFORMon the stored document in SQL, or a partial update through your API layer. It changes what you name and nothing else. - Decide which arrays may delete rows. Leave
@deleteoff child tables that apps shouldn’t remove rows from, and getORA-40938instead of a silent delete. - Handle
ORA-40896,ORA-42698andORA-42699as “read again, apply the change again, try again”, with a limit. - Keep batch transactions on the base tables short, or schedule them away from the hours when apps write those documents.
To see what a view allows:
select view_name, table_name, allow_insert, allow_update, allow_delete
from user_json_duality_view_tabs
order by view_name, table_name;
VIEW_NAME TABLE_NAME ALLOW_INSERT ALLOW_UPDATE ALLOW_DELETE
ORDERS_DV ORDERS TRUE TRUE TRUE
ORDERS_DV ORDER_LINES TRUE TRUE TRUE
ORDERS_NODEL_DV ORDERS TRUE TRUE TRUE
ORDERS_NODEL_DV ORDER_LINES TRUE TRUE FALSE
Every TRUE in ALLOW_DELETE on a child table is an array that deletes rows when an element is missing.
Prove it
The lab runs Oracle AI Database 26ai Free (23.26.3) in one container; the two apps and the batch job are SQL sessions:
drill case result
etag A's old copy, with its ETAG ORA-42699, nothing written
A's old copy, without _metadata accepted, no error; B's 99 back to 10
control: JSON_TRANSFORM on the current document B's 99 kept
omit document without line 13 (ETAG valid) row 13 deleted
document without the lines field all 3 rows deleted
the same through a view without @delete ORA-40938
a field the view doesn't define ORA-40944
INTERNAL_NOTE (not mapped) survives a full replace
conflict batch job's open update of line 13:
view write of the status / of line 13 ORA-40896 / ORA-42698, at once
the app's full document, current ETAG ORA-40896, at once
batch job's open change to INTERNAL_NOTE only:
view write of line 13 / of the status works / ORA-42698 (table ORDERS), at once
view write open on line 11:
base update of line 13 / of line 11 no wait / waits for it
base change to line 13 commits first the view's COMMIT works
hash INTERNAL_NOTE / qty 10 -> 12 -> 10 same ETAG / new / the original again
full replace changing only the status 1 undo record, like a plain UPDATE
Don’t take my word for it — run it. The json-duality lab asserts every row:
ORA-42699for the stale ETAG and the lost update without one, the deleted lines,ORA-40938andORA-40944,ORA-40896andORA-42698with no wait, the base-table waits, the commit that works, and the content-hash ETAG. If any of that doesn’t reproduce, the run fails. Proven on every CI push.
What the lab doesn’t cover: ORDS REST and the MongoDB API (they’re not in the image, and they handle _metadata
their own way), views defined in GraphQL, NOCHECK, flex columns, CDC on the base tables, and RAC.
What teams get wrong
- Assuming the ETAG is enforced. It’s checked when the document carries one. A client that drops
_metadataoverwrites other people’s committed changes without an error. - Treating a replace as a merge. An array element the document doesn’t have is a row the database deletes, and a valid ETAG doesn’t stop it.
- Expecting writes to queue like
UPDATEs. A write through the view fails at once when any mapped row of the document has an open change, even a row it doesn’t touch. - Leaving long batch transactions on the base tables. Every document they touch rejects app writes until they commit.
Frequently asked questions
What happens if I update a JSON duality view document without the _metadata etag?
In the companion lab on Oracle AI Database 26ai Free 23.26.3, the ETAG check was skipped and the document was written as it was. App A wrote back an old copy without _metadata after app B had committed a change to the same order; there was no error, and B's change (qty 99) was set back to A's old value (10). With the ETAG, the same write failed with ORA-42699 and changed nothing.
Does replacing a duality view document delete child rows that aren't in the array?
Yes, when the child table has the @delete annotation. In the companion lab, a document that left out one of its three lines deleted that row from ORDER_LINES, and a document without the lines field deleted all three, both with a valid ETAG. Through a view without @delete on the lines, the same write failed with ORA-40938.
What does ORA-40896 mean on a duality view?
Concurrent modification detected to document: another transaction has an uncommitted change to a row of the document. In the companion lab, while a batch session had an open UPDATE of one line in the base table, a write through the view that changed the order's status failed with ORA-40896 after 37 ms, without waiting. A write that changed the lines failed with ORA-42698, the field-level version. Oracle's action: read the latest document and try again.
Do writes through a JSON duality view wait for row locks?
Not in the companion lab. A write through the view failed at once with ORA-40896 or ORA-42698 when any mapped row of the document had an open change, even a row it didn't change. Writers to the base tables behaved as usual: an update of the row an open view write had changed waited for it, and an update of another row didn't. An open change to a column the view doesn't map blocked only view writes to that same row, again at once (ORA-42698 naming the table).
Is the duality view ETAG a version number?
No, it's a hash of the document's content. In the companion lab a change to a column the view doesn't map left the ETAG unchanged, a qty change from 10 to 12 changed it, and setting the qty back to 10 restored the original ETAG. Columns annotated NOCHECK don't contribute to it.
Does a full document replace rewrite every row?
No. In the companion lab a full replace of an order that changed only its status wrote one undo record, the same as a plain UPDATE of the ORDERS row. The database writes only the rows whose mapped values differ, so CDC on the base tables sees those row changes.
A duality view gives an app a document and keeps the data in tables, and it does that well. What it doesn’t do is guess what the app meant: a document without an ETAG is trusted, a document without an array element is a delete, and a document with an open change in its rows is refused. Make your apps send the ETAG, decide where deletes are allowed, and build the retry, then prove it with the json-duality lab.
Have a question or some feedback?
I write here in a personal capacity and enjoy comparing notes with other Oracle folks. Say hello.
Get in touch