Oracle “order by” clause returning results in wrong order or with results missing

纵然是瞬间 提交于 2019-12-08 12:42:12

问题


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:

  1. 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?
  2. I'm using BoneCP for connection pooling.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!