Changes directly in the database reflect in the bean Managed?

Asked

Viewed 122 times

1

I’m developing a simple web application, a form. I did the DAO’s, Facades and they are working correctly (at least I think they are) but I will also need to edit some values directly in the bank only that these changes are not being reflected in the application, for example, the bank contains the column "student registration" with the value 20, and this value is loaded and displayed normally in the application, when editing the value by the application the value is edited normally, but when performing manual editing (replace 20 by 30 directly in the database for example) and reload the view, JSF goes through all the necessary methods (an init() method annotated with @Postconstruct) but does not display the changed value but the old value.

I have tried with @Requestscoped and @Viewscoped. This is normal?

I am using JSF2.2, Eclipselink, Mysql and JPA.

2 answers

2


First, check that the change is isolated in an unaffected transaction. If so, do the commit for the values to be read correctly by the application.

It may still be occurring if the values are in cache.

If it is the cache JPA normal, run the method refresh() of EntityManager entities to be updated. Values will be reloaded from the database.

If you use any kind of Second level cache, you can clean that cache with the following method:

entityManager.getEntityManagerFactory().getCache().evictAll();

Or, specifically for an entity, use the method:

entityManager.getEntityManagerFactory().getCache().evict(
        classeEntidade, 
        valorChavePrimaria);

See the documentation.

0

After editing directly in the database, restart your application server and reload the view. As I already experienced this same 'problem', I saw that after restarting the server (in my case Tomcat) the application started to 'see' those changes made. But I’m really looking for a more ideal solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.