Forum Stats

  • 3,814,528 Users
  • 2,258,880 Discussions
  • 7,892,767 Comments

Discussions

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

943589
943589 Member Posts: 12
edited May 23, 2013 4:45AM in Enterprise JavaBeans
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
Tagged:
«1

Answers

  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    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.
  • r035198x
    r035198x Member Posts: 2,499
    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?
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    edited Nov 26, 2012 2:47AM
    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.
  • 943589
    943589 Member Posts: 12
    edited Nov 26, 2012 5:06AM
    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.
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    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?
  • 943589
    943589 Member Posts: 12
    edited Nov 26, 2012 5:11AM
    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
  • 943589
    943589 Member Posts: 12
    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.
  • 943589
    943589 Member Posts: 12
    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.
  • r035198x
    r035198x Member Posts: 2,499
    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.
    r035198x
  • 943589
    943589 Member Posts: 12
    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
This discussion has been closed.