I have been studying for JPA and struggled with a bug which made me very confused and later I have found out that it was Chrome caused the program run incorrectly.
I recently switched from Firefox to Chrome. And when I use glassfish admin console in Chrome, whenever after I deployed the app and try to open http://<mymachinename>:8080/myapp/runservlet, I found out the servlet (I don't include code here as it is not specific but generally every servlet dealing with JPA and javadb) will be called TWICE.
Normally if behave correctly, after persisted a entity and auto generate ID, I should get one entity in the database. However, I always get another duplicate in the database. For example, I have a employee named John Smith and I should have a row in Employee table with auto-gen'ed ID 1 and John Smith, but instead somehow, I got ID 1/John Smith and ID 2/John Smith.
At first I thought it was a bug in my code and I tried for a long time to find out but no success. Then I tried same code in IE, the same code behaves correctly no matter how many times I try!
And I tried to run different code and I found it is a general problem not the specific program which I found the problem first with.
Then I found out more:
If I open the app in Chrome at the following address the problem will be gone:
Do anyone know why app running by Chrome is behaved this way and how to fix it?
Thanks in advance.
I will continue to try the suggested.
But there is a little progress: I found the request header from Chrome and IE are different by one line, in Chrome there is "Cache-Control: max-age=0" but not in IE. And if I config setting to disable cache in Chrome developer tools, everything works fine.
Paul.J Wong wrote:I found the request header from Chrome and IE are different by one line, in Chrome there is "Cache-Control: max-age=0" but not in IE. And if I config setting to disable cache in Chrome developer tools, everything works fine.
From your question, the servlet is hit twice per request. I don't understand how cache-control affects the first request. If Chrome is making one request to render the page and another to cache it, then that would be a bug.
Mansukhdeep Thind wrote:That explains it Paul. Cache-Control max-age setting instructs the client about whether it should re-validate every request again or pick it up from the cache as per the max age setting. Read this.
I have Chrome in all its default settings, and I disabled all extensions.
But I found that from line 14 and 15 of the servlet above everything works but 17 to 20 cause entities to persisted twice, those two portion of codes belong to same doGet method.