Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping search result consistent across multiple transactions

Tags:

java

sql

oracle

I have to implement a requirement for a Java CRUD application where users want to keep their search results intact even if they do actions which affects the criteria by which the returned rows are matched.

Confused? Ok. Let me give you a familiar example. In Gmail if you do an advanced search on unread emails, you are presented with a list of matching results. Click on an entry and then go back to the search list. What happens is that you have just read that entry but it hasn't disappeard from the original result set. Only that line has changed from bold to normal.

I need to implement the exact same behaviour but the application is designed in such a way that any transaction is persisted first and then the UI requeries the db to keep in sync. The complexity of the application and the size of the database prevents me from doing just a simple in memory caching of the matching rows and making the changes both in db and in memory.

I'm thinking of solving the problem on the database level by creating an intermediate table in the Oracle database holding pointers to matching records and requerying only those records to keep the UI in sync with data. Any Ideas?

like image 279
Amin Mozafari Avatar asked May 01 '11 10:05

Amin Mozafari


Video Answer


2 Answers

In Oracle, if you open a cursor, the results of that cursor are static, regardless if another transaction inserts a row that would appear in your cursor, or updates or deletes a row that does exist in your cursor.

The challenge then is to not close the cursor if you want results consistent from when the cursor was opened.

like image 90
Adam Musch Avatar answered Oct 28 '22 11:10

Adam Musch


If the UI maintains a single session on the database, one solution is to use Global Temporary Tables in Oracle. When you execute a search, insert the unique IDs into the GTT, then the UI just queries the GTT.

If the UI doesn't keep the session open, you could do the same thing but with an ordinary table. Then, of course, you'd just have to add some cleanup code to remove old search results from the table.

like image 26
Jeffrey Kemp Avatar answered Oct 28 '22 11:10

Jeffrey Kemp