问题
In my application logs, I can see that the query results are being iterated over (using a JDBC ResultSet, nothing fancy), and certain results which are actually in the table, are either missing, or appear too late in the resultset (I'm not sure which, because I often get a primary key violation before I hit the end if this happens).
My query is:
select t.* from myschema.vm t order by id;
However I can't see this issue occurring when I run the exact same query in SQL Developer (while the database is not changing), and it only occurs about half the time in my application (I suspect it sometimes doesn't happen due to the table changing due to all the inserts and updates).
Other key points to mention:
- I'm doing updates, inserts and potentially deletes using another database connection, while I'm iterating over the ResultSet. However, this shouldn't matter, right?
- I'm using BoneCP for connection pooling.
- The database encoding is AL32UTF8.
Why is this happening? It's driving me crazy!
回答1:
Oracle resultsets are freezed in the moment you execute the query, no matter how later you fetch the data (or part of them); so, if other session inserts data, you will found (rightly ordered) only rows already committed when you open the cursor.
Also, I suspect you create your ID with some evaluation on current values found on the table instead of using a sequence or a shared atomic counter, so you get the PK violation.
回答2:
This problem was due to user error. I had another copy of the application running on a different machine, which I had forgotten about, and that was changing the database at the same time.
来源:https://stackoverflow.com/questions/19516163/oracle-order-by-clause-returning-results-in-wrong-order-or-with-results-missin