0

I hope this is not a stupid question, or one formulated wrong. For my code's purpose, I am getting the value of a button with jquery, post it with ajax in a table and then fetching it in another part of the code. This is the simple explanation. And I appreciate the concern, but dont question why I am doing so because this is how it works and I wont change it.

Now the thing is that the data I get with the select query in the end are only those that were registered before the last page reload.

If you need to see code, this is the link to my previous question regarding this same problem: Code returns the latest value before last refresh instead of the latest value inserted?

I was wondering if the reason I get such data has something to do with caching?

Community
  • 1
  • 1
Lazarus Rising
  • 2,597
  • 7
  • 33
  • 58
  • Please post the relevant parts of the code in the question. – Jakub Kania Feb 09 '15 at 15:22
  • sure sounds like a race condition and not a caching issue. – Joe Love Feb 10 '15 at 06:47
  • @joeLove: And actually it's not caching related, you are right. I did the same thing using a file to write the temporary data and it gives me the same results. – Lazarus Rising Feb 10 '15 at 13:57
  • @joeLove: From what im reading it might really be a race condition. I'll get back to this post as soon as I clarify my ideas. Meanwhile, you might as well post your comment as an answer. Ill make sure to accept it in case you are right – Lazarus Rising Feb 10 '15 at 14:04

2 Answers2

1

Your problem appears to be a race condition and not a caching issue. When running 2 asynchronous processes where 1 is updating/inserting and another is reading, it's highly possible that the "read" process starts reading before the data is committed. The best solution is to do the update and read in "1 thread" (synchronously) where the update and read are done in a specific order and the update is guaranteed to finish before the read is started.

Postgres doesn't really "cache" data in such a way where old results would ever be returned, but uncommitted results will never be returned -- updates must first be committed before any other session will see them.

Joe Love
  • 5,594
  • 2
  • 20
  • 32
  • But since I need the update and read to be done in 2 different files, this is impossible. I was reading about flock() but (apparently) it doesn't block the file from other processes. I don't really know how to work this out. – Lazarus Rising Feb 10 '15 at 14:58
  • Can you have 1 of the files call the other, or have 1 wait until the other is finished? – Joe Love Feb 10 '15 at 14:59
  • That is what I was trying to do. But I haven't found any way to do that. – Lazarus Rising Feb 10 '15 at 15:00
  • Try using the example here to set the ajax async to false: http://stackoverflow.com/questions/755885/how-do-i-make-jquery-wait-for-an-ajax-call-to-finish-before-it-returns – Joe Love Feb 10 '15 at 15:08
0

Even though I initially thought myself it was a race condition (nice suggestion), it was actually a client-side/ server side interaction issue. A race condition could have caused the previous result to display, but not necessarily the result before the previous page load.

However, I found the answer of Joe Love very useful, since I hadn't heard / considered racing condition before. Also, good point in saying that postgres doesn't really "cache" data

Lazarus Rising
  • 2,597
  • 7
  • 33
  • 58