Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A long-running Java report on Oracle fails after 2 hours with ORA-01555: snapshot too old. What does it mean, and how do you fix it?

ORA-01555 means the report needed to rebuild an old version of a block as of its start time, but the undo records it needed had already been overwritten, because busy OLTP updates cycled the undo faster than the two hours the query ran. We fix it by keeping undo longer (a larger UNDO_RETENTION and undo tablespace, or RETENTION GUARANTEE), by making each query shorter, for example splitting the report into key ranges, by running it on a standby, and by never committing inside a loop over an open cursor.

Why this question is asked

ORA-01555 is one of the most famous Oracle errors, and it is frustrating because it appears after hours of work, often only at month end when the report and the OLTP load overlap. Interviewers like it because the fix depends on really understanding read consistency. In this answer, we will recap why a query needs undo at all, see why the error happens after two hours, look at the classic fetch-across-commit mistake in Java, and then go through the fixes from the database and the application side.

1A Quick Recap: Why a Query Needs Undo

In Oracle, each query sees the data as of the SCN, the logical timestamp, at which it started. Writers change rows in place, and before each change they write the old value into undo. When our query reads a block that was changed after its start SCN, Oracle uses those undo records to rebuild the block as it was. So a query that runs for two hours may need undo from two hours ago for every block that changed in that time.

Undo is not kept forever. The undo tablespace is a circular space: once a transaction commits, its undo becomes reusable after the retention period. The UNDO_RETENTION parameter, 900 seconds by default, is a target, not a promise; when space runs low, Oracle overwrites committed undo early so that new transactions can proceed.

2Why It Fails After Two Hours

Put those two facts together. The report starts at 01:00 with SCN 5000. The OLTP workload keeps updating the same tables and committing. By 02:30 the undo from 01:00 has been reused for new transactions. At 03:00 the report reaches a block that was changed at 01:20, asks for the undo to rebuild the 01:00 version, and finds it gone. Oracle cannot give a consistent answer, so it fails the query with ORA-01555, snapshot too old.

Step throughHow a long report loses its undo
ReportUndo tablespaceOLTP sessions
01:00 start, SCN 5000
01:20 update block X, undo U1 written
02:30 undo full: U1 overwritten by new work
03:00 read block X, need U1
U1 is gone: ORA-01555

5 messages in order. Play them, or step through them one at a time.

The longer the query and the busier the tables, the more likely the undo is gone.

3A Classic Cause: Committing While Fetching

Very often the report is not only reading. A common Java pattern opens a cursor over many rows, processes each one, updates something and commits every few hundred rows to keep transactions small. It feels responsible, but it is a well-known cause of ORA-01555.

Java
try (PreparedStatement select = conn.prepareStatement(
"SELECT id, amount FROM invoices WHERE status = 'OPEN'");
PreparedStatement update = conn.prepareStatement(
"UPDATE invoices SET status = 'BILLED' WHERE id = ?")) {
conn.setAutoCommit(false);
ResultSet rs = select.executeQuery(); // cursor consistent as of SCN 5000
int n = 0;
while (rs.next()) {
update.setLong(1, rs.getLong("id"));
update.executeUpdate();
if (++n % 500 == 0) conn.commit(); // frees the undo our own cursor still needs
}
conn.commit();
}
// after an hour: java.sql.SQLException: ORA-01555: snapshot too old

The cursor is still reading as of SCN 5000, but our own updates change the very table it reads, and each commit makes that undo reusable. So we are overwriting the undo our cursor needs, and the bigger the table, the more certain the failure. The fix is to not keep a cursor open across commits: either process in one transaction, or read a batch of keys, close the cursor, process and commit, then read the next batch with a fresh query.

4Fix 1: Keep Undo Longer

The direct database fix is to keep undo long enough for the longest query. We set UNDO_RETENTION above the report's run time and make sure the undo tablespace is big enough to hold that much undo at the peak change rate. Without the space, the retention target is ignored when pressure rises. RETENTION GUARANTEE makes it a hard promise, at the cost that OLTP transactions fail with out-of-space errors instead of the report failing.

SQL
-- Check the longest query and undo pressure
SELECT MAX(maxquerylen) AS longest_query_s, SUM(ssolderrcnt) AS ora_01555_count
FROM v$undostat;
-- 7350 3
ALTER SYSTEM SET UNDO_RETENTION = 10800; -- 3 hours
ALTER DATABASE DATAFILE '/u01/undotbs01.dbf' RESIZE 64G;
-- Only if the report must never fail and OLTP can tolerate the risk:
ALTER TABLESPACE undotbs1 RETENTION GUARANTEE;

V$UNDOSTAT is a good place to start: it shows the longest query in each interval and how many snapshot-too-old errors happened, which tells us how much retention we really need. Oracle's undo advisor can then suggest a tablespace size for that retention.

5Fix 2: Make Each Query Shorter

The more robust fix is on the application side: a query that finishes in two minutes never needs two hours of undo. We split the report into key ranges and run one short query per range, writing each part's result as we go. Each query has its own start SCN, so it needs only a few minutes of undo.

Java
long step = 100_000;
for (long from = minId; from <= maxId; from += step) {
try (PreparedStatement ps = conn.prepareStatement(
"SELECT customer_id, SUM(amount) FROM invoices " +
"WHERE id >= ? AND id < ? GROUP BY customer_id")) {
ps.setLong(1, from);
ps.setLong(2, from + step);
ps.setFetchSize(1000); // Oracle's default of 10 rows per round trip is slow
writePart(ps.executeQuery()); // each range takes about a minute
}
}

There is a catch: the parts are no longer consistent with each other at one moment, because each range is read at a different SCN. For most operational reports that is fine. If the report must be exact as of one point in time, we can pass the same AS OF SCN to every range query, which needs undo for the whole run again, or take the data from a snapshot copy instead.

That leads to the third option: run the report on a standby database. With Active Data Guard (a separately licensed option) the standby is open read-only, so the report's undo needs do not compete with the primary's OLTP workload. A restored copy or a reporting replica works in the same way.

6Follow-up questions

Can ORA-01555 happen even when nobody else is updating the table?

Yes, through delayed block cleanout. After a large update, Oracle may leave the commit information in blocks unfinished, and a later reader has to check the transaction table in undo to confirm the commit. If that undo segment header has been reused, the reader can get ORA-01555. Gathering statistics or running a full scan after a big batch load cleans the blocks out.

Why not just make the undo tablespace huge?

It helps, and disk is cheap, but it treats the symptom. The report still holds a snapshot for hours, and as data and change rates grow the problem returns. Shorter queries keep working as the system grows, so I prefer to combine a sensible retention with shorter queries.

What does RETENTION GUARANTEE risk?

With the guarantee, Oracle will not overwrite unexpired undo even when space runs out. New transactions that need undo space then fail with errors such as ORA-30036, unable to extend segment in the undo tablespace. So we only use it when the undo tablespace is sized with plenty of headroom and the long query matters more than OLTP writes.

7Conclusion

In this answer, we saw that ORA-01555 happens because a long query needs undo to rebuild blocks as of its start SCN, and busy updates on the same tables overwrite that undo before the query finishes. A classic cause in Java is committing inside a loop over an open cursor on the same table. We fix it by keeping undo longer with UNDO_RETENTION, a larger undo tablespace or RETENTION GUARANTEE, and more robustly by making queries short, splitting the report into key ranges, or running it on a standby.

What to say in the interviewAbout 90 seconds aloud

Open with

ORA-01555, snapshot too old, means my query needed an old version of a block and the undo to rebuild it was gone. In Oracle a query sees data as of its start SCN, and any block changed since then is rolled back using undo records.

Then, if they want more

The report ran for two hours, while the OLTP workload kept updating the same tables, and once undo is committed it becomes reusable after UNDO_RETENTION, 900 seconds by default, or earlier under space pressure. So by the time the report reached a changed block, its undo had been overwritten.

First I would check whether the report itself commits inside a loop over an open cursor on the same table, because that overwrites the undo the cursor needs and is a classic cause; the fix is to read keys in batches and reopen the query.

On the database side I would raise UNDO_RETENTION above the longest query time, which V$UNDOSTAT shows, and size the undo tablespace so the target can be honoured, using RETENTION GUARANTEE only if OLTP can tolerate undo space errors.

The more durable fix is to make each query short: split the report into key ranges so each query runs for a minute, with a larger fetch size, accepting that ranges are read at slightly different times. Or run the report on an Active Data Guard standby or a reporting copy.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A long-running Java report on Oracle fails after 2 hours with ORA-01555: snapshot too...": https://scrivio-interview-library.netlify.app/databases/oracle-and-sql-server/e42032ce3d/