-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: EJB and JTA sync problems between process
PostPosted: Thu Sep 29, 2016 9:24 am 
Newbie

Joined: Thu Sep 29, 2016 8:35 am
Posts: 1
Hi,
I work at a complex web application that use EJB and Hibernate on JBoss. I use singleton EntityManagerFactory, and share it between all running process using Entity Manager instance.

The problem occurs when in a struct action is called an update on entity, and before action ends another process read and update same object.
I have that second process (it's a web service called from third-parts) read an old value and not the updated one in the action.

I know that data become persistent on database only after action end its work and control come back to user. Unfortunately this action after Entity Manager merge execution, it must call a web service that sometimes return after 10s . Meanwhile other process have wrong value if read this object.

I need that merge in first process become instantly persistent, or, I need that other process read right value.
I don't know if the second level cache is working and has effect in this scenario.

A solution is to make an update using JDBC instead Hibernate, but I would like a clean solution to do it.

a brief outline

t0 = start action ;t1= action find and merge entity; t2= start call to web service; t6= web service return; tend = end action ;
t3= start second process ; t4= find and merge entity; t5=end second process

t0 t1 t2 t3 t4 t5 t6 tend
|---------------|-------|------------------------------|-------|
|-----|----|

I need that at t3 the value read is that one merged at t2.

This is my persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="VisiaIntegrazione_EJB" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/ds/VisiaIntegrazioneDS</jta-data-source>
    <class>.....entity.ApplicationServer</class>
   ....
    <class>.....entity.Devices</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
      <!-- Caching properties -->
      <property name="hibernate.cache.use_second_level_cache" value="true"/>
      <!--<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />-->
      <property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml"/>
      <!--<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>-->
      <!--<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />-->
      <property name="hibernate.cache.use_query_cache" value="true"/>
      <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>
      <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
      <property name="hibernate.max_fetch_depth" value="4"/>
   <!-- hibernate.generate_statistics a true produce informazioni su hibernate da loggare -->
      <property name="hibernate.generate_statistics" value="true"/>
     
    </properties>
  </persistence-unit>
</persistence>


this is an example of Entity Manager update
Code:
EntityManager em = EntityMan.getEMF().createEntityManager();
     
        try {

            em.find(Devices.class, device.getId());
            em.merge(device);
            em.flush();
        } catch (Exception e) {
            logger.debug(e.getMessage());
            e.printStackTrace();
        }


Top
 Profile  
 
 Post subject: Re: EJB and JTA sync problems between process
PostPosted: Fri Sep 30, 2016 7:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
There are two ways to prevent lost updates:

- either you detect them using optimistic locking
- or you avoid them using pessimistic locking

Reading the last updated value using a separate connection is not really a solution. Imagine that even after you just read the value, a process can still update it, therefore you are using a stale data anyway. The only way to guarantee that you don;t miss an update is to use optimistic or pessimistic locking.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.