This discussion is archived
1 2 Previous Next 20 Replies Latest reply: May 23, 2013 1:45 AM by gimbal2 RSS

How to refresh JPA data when the table data is updated from the backend

943589 Newbie
Currently Being Moderated
Is there a way to inform the Entity Manager or force the JPA provider to reload data from the database? The scenario could be data being updated by a store procedure or direct SQLPlus maintenance, without restarting the Application Server, the JPA need to load the newly updated data from the database.

I think the current JPA API is not enough. The void refresh(java.lang.Object entity) from EntityManager need to pass in the Entity object, I will like to know how to refresh the entire JPA Entity data after the physical table data being update from backend.

Edited by: leonyip on Nov 27, 2012 2:58 PM
  • 1. Re: How to refresh JPA data when the table data is updated from the backend
    Kayaman Guru
    Currently Being Moderated
    leonyip wrote:
    I will like to know how to refresh the entire JPA Entity data after the physical table data being update from backend.
    Do you mean the currently attached entities or did you want all entity data refreshed? The former might be possible (although likely quite useless, you can still do it by hand). The latter would be impossible for obvious reasons.
  • 2. Re: How to refresh JPA data when the table data is updated from the backend
    r035198x Pro
    Currently Being Moderated
    leonyip wrote:
    Is there a way to inform the Entity Manager or force the JPA provider to reload data from the database?
    Reload to where?
  • 3. Re: How to refresh JPA data when the table data is updated from the backend
    gimbal2 Guru
    Currently Being Moderated
    leonyip wrote:
    Is there a way to inform the Entity Manager or force the JPA provider to reload data from the database? The scenario could be data being updated by a store procedure or direct SQLPlus maintenance, without restarting the Application Server, the JPA need to load the newly updated data from the database.
    JPA is just an API, it does not have a brain. If you want it to reload stuff you need to tell it to reload stuff. In other words the "clever" piece of logic you want so desperately to be handled for you, will need to be developed by you.
  • 4. Re: How to refresh JPA data when the table data is updated from the backend
    943589 Newbie
    Currently Being Moderated
    Kayaman wrote:
    Do you mean the currently attached entities or did you want all entity data refreshed? The former might be possible (although likely quite useless, you can still do it by hand). The latter would be impossible for obvious reasons.
    I mean the currently attached entities. JPA2 API only provide option to refresh a single entity object refresh(entity) for now I don't see where to get to refresh the entire set of Entities.
  • 5. Re: How to refresh JPA data when the table data is updated from the backend
    Kayaman Guru
    Currently Being Moderated
    leonyip wrote:
    As the JPA API only provide option to refresh a single entity object refresh(entity) for now I don't see where to get to refresh the entire set of Entities.
    You'd need to make that functionality yourself, keep track of the entities and refresh them one by one.

    Why do you think you'll need functionality like that anyways? What's your use case?
  • 6. Re: How to refresh JPA data when the table data is updated from the backend
    943589 Newbie
    Currently Being Moderated
    r035198x wrote:
    leonyip wrote:
    Is there a way to inform the Entity Manager or force the JPA provider to reload data from the database?
    Reload to where?
    Reload the newly updated value to the Entity Manager. I did a test using EJB-JPA to load all data from a table say Employee. I run a listAll method and it display all values, now I use SQL Plus to update a certain field like age updated all to 20. After I commit the changes into the db, and I run rerun the EJB-JPA program to fetch the data again but the value remain unchanged.

    I suppose the EJB-JPA has first load the table value and cache the data into the memory . Backend data changes doesn't get reflected when I do a second loading.

    This program however finally get refresh after 2 days. I run this test last Friday afternoon, the value remain unchanged till last Friday evening. I rerun it this again Monday morning and the value get updated.

    Edited by: leonyip on Nov 26, 2012 6:06 PM
  • 7. Re: How to refresh JPA data when the table data is updated from the backend
    943589 Newbie
    Currently Being Moderated
    I need the API to reload the full set of Entities. I see the current JPA2 API only provide method to reload a single instance of entity, which is void refresh(java.lang.Object entity), there are 4 similar refresh method in the API but it seems only able to refresh a single entity at a given time.

    My scenario is the set of data being updated from the backend to a few entities, when I do a findAll(JPQL query), the result return still having the old values. I need to find a way to notify or get the EntityManager to reload the data from the Database for the newly commmited data that was updated backend.
  • 8. Re: How to refresh JPA data when the table data is updated from the backend
    943589 Newbie
    Currently Being Moderated
    You'd need to make that functionality yourself, keep track of the entities and refresh them one by one.

    The scenario is my application needed to refresh the new values updated from the backend (SQLPlus or store procedure that execute using other protocol). I believe there is a way, cause if I leave my EJB-JPA program for a while, I did this test last Friday and I reload it this Monday morning and the data get updated.

    I think the JPA do perform a refresh periodically, I can't find where to exeute it.
  • 9. Re: How to refresh JPA data when the table data is updated from the backend
    r035198x Pro
    Currently Being Moderated
    Even if we take JPA out of the question your requirement isn't all that feasible. A view screen that is not itself the datastore will never be 100% in sync with the datastore if the updates don't happen through the view screen itself.

    The DB does not make calls to your java layer, the Java layer makes the calls.
    If the data is being updated outside of the Java layer then you won't always get the latest data in the Java layer (unless if you resort to some ugly database triggers that call the service layer). Even if there was an EntityManager.refreshAll method, the data could get updated just after the call is done, then what? Also, there could be no operation accessing the data when the database updates are done so there is no need to trigger any.
    If an operation is reliant on the latest data then the best the Java layer can do is to make sure the operation always loads records from the database rather than relying on the cache. Just disable caching for those entities (JPA construct in EE6 and persistence provider specific for EE5). This is good enough for most application requirements.
  • 10. Re: How to refresh JPA data when the table data is updated from the backend
    943589 Newbie
    Currently Being Moderated
    r035198x wrote:
    Even if we take JPA out of the question your requirement isn't all that feasible. A view screen that is not itself the datastore will never be 100% in sync with the datastore if the updates don't happen through the view screen itself.
    Yes, agree to this design context.
    The DB does not make calls to your java layer, the Java layer makes the calls.
    If the data is being updated outside of the Java layer then you won't always get the latest data in the Java layer (unless if you resort to some ugly database triggers that call the service layer). Even if there was an EntityManager.refreshAll method, the data could get updated just after the call is done, then what? Also, there could be no operation accessing the data when the database updates are done so there is no need to trigger any.
    So is there a EntityManager.refreshAll method or similar? At least I have the option to reload the data whenever I know there is an operation update from backend. This option can be executed by the operation
    If an operation is reliant on the latest data then the best the Java layer can do is to make sure the operation always loads records from the database rather than relying on the cache. Just disable caching for those entities (JPA construct in EE6 and persistence provider specific for EE5). This is good enough for most application requirements.
    I have set the cache to non <property name="eclipselink.cache.type.default" value=""/>, but the value didn't get update after I re-execute the listAll method
  • 11. Re: How to refresh JPA data when the table data is updated from the backend
    Kayaman Guru
    Currently Being Moderated
    Did you try these options? http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink
  • 12. Re: How to refresh JPA data when the table data is updated from the backend
    r035198x Pro
    Currently Being Moderated
    leonyip wrote:
    So is there a EntityManager.refreshAll method or similar? At least I have the option to reload the data whenever I know there is an operation update from backend. This option can be executed by the operation
    No there is not one because all finds will hit the database anyway to get the right results if caching has been switched off.
    I have set the cache to non <property name="eclipselink.cache.type.default" value=""/>, but the value didn't get update after I re-execute the listAll method
    You need to read the manuals of your persistence provide more because that setting you set alone is unlikely to be the one that turns off caching.
  • 13. Re: How to refresh JPA data when the table data is updated from the backend
    r035198x Pro
    Currently Being Moderated
    And another thing ...
    Even with the default settings of your provider the data update shouldn't only reflect after two days. If it is the cache that's causing this then you must have altered the defaults drastically. If you didn't then I would investigate this program further to find the source of this problem, could be the program is not doing what you think it's doing.
  • 14. Re: How to refresh JPA data when the table data is updated from the backend
    943589 Newbie
    Currently Being Moderated
    r035198x wrote:
    leonyip wrote:
    So is there a EntityManager.refreshAll method or similar? At least I have the option to reload the data whenever I know there is an operation update from backend. This option can be executed by the operation
    No there is not one because all finds will hit the database anyway to get the right results if caching has been switched off.
    I agreed to this point. But some how my test result against it, I shall check further until I confirm this result. Thanks.
    I have set the cache to non <property name="eclipselink.cache.type.default" value=""/>, but the value didn't get update after I re-execute the listAll method
    You need to read the manuals of your persistence provide more because that setting you set alone is unlikely to be the one that turns off caching.
    The value is provided by the JDeveloper IDE, the setting should be right. Now I am skeptic about the EJB that cache the JPA object, but it shouldn't but nonetheless I am going to test it further.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points