简体   繁体   中英

Java Hibernate EntityManager sync

I have a problem with syncronization of a Database field.

I have a Singleton class for instance EntityManager, I use code like this :

Persistence.createEntityManagerFactory("Myproject_EJB")

I create EntityManager using this singleton from both a WebApplication and from WebService layer and use it for access to persistence.

User can modify this Database field using WebApplication, but data is stored in Database only when request is complete (request can take several seconds). Meanwhile if someone call WebService and ask for same field I have an inconsistent state.

In persistence.xml I have use_second_level_cache and use_query_cache set true, and transaction-type="JTA".

In webApp I use code like this for update data :

EntityManager em = EntityMan.getEMF().createEntityManager();       
    try {
        em.find(Tel.class, tel.getTelId());
        em.merge(tel);
        em.flush();
    } catch (Exception e) {
        logger.debug(e.getMessage());
        e.printStackTrace();
    }

How can I solve please ??

don't use caching for data which is changed frequently, or keep cache time very small ? you can set table level caches http://www.tutorialspoint.com/hibernate/hibernate_caching.htm just giving pointers to look not a perfect answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM