-->
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.  [ 3 posts ] 
Author Message
 Post subject: Handling updates to DB with multiple applications
PostPosted: Mon Apr 23, 2007 4:56 pm 
Newbie

Joined: Fri Nov 10, 2006 7:26 pm
Posts: 11
I'm having problems with multiple applications updating my DB when both are running. One application is an Eclipse RCP application. The other is a Spring-based web application being built up that performs some similar functionality as the RCP app.

The RCP app basically has a long running Hibernate session opened at the start. All objects and associations are lazy-loaded. Users can create n update objects in the object graph as needed. A new transaction is created for each save(), update(), delete(), etc.

For the Spring web application, I implement the basic filters as defined in the Hibernate bible, which closes the session after each request.

The RCP app has been working just fine for a year now but as I build up my Spring-based web application do some similar functionality I'm getting the (as expected) org.hibernate.StaleObjectStateException when I interleave DB operations between the two applications when both are running.

For example, a user in the Eclipse RCP app creates a record, the web user updates the record and then I go back to the Eclipse RCP and try to add a related object to my original record and I get the StaleObjectStateException.

The web application implements object fetching 'the hard way'. I fetch the objects from Hibernate as I need them. I have no problems with DB access (e.g., no Stale...) using this method.

I've tried various strategies in the data layer of my RCP application so that before I call update() on the changed object I evict() it, refresh() it, merge() it, close the session, etc. All to no avail......

Various mapping documents n code follow.

Help.....

Hibernate version:
3.1.3
Mapping documents:
An abbriviated hibernate.cfg.xml and two class mapping files follow:
Code:
<hibernate-configuration>
    <session-factory>
      <!-- use Jakarta DBCP -->
      <property name="hibernate.connection.provider_class">com.xxx.model.dbConnection.DBCPConnectionProvider</property>
        <property name="connection.pool_size">8</property>

        <property name="current_session_context_class">thread</property>

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <mapping resource="Person.hbm.xml"/>
        <mapping resource="Client.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Person.hbm.xml:
Code:
<hibernate-mapping package="com.xxx.model">
  <class name="Person" table="PERSON">
   <id name="key" column="personKey">
      <generator class="native"/>
   </id>
   <timestamp name="Ts" column="ts" source="vm"/>
   <property name="Id"        type="string"   column="id"        length="128"/>
  </class>
</hibernate-mapping>

Client.hbm.xml
Code:
<hibernate-mapping package="com.xxx.model">
  <joined-subclass name="Client" extends="Person" table="CLIENT">
   <key column="clientKey"/>
      <set name="Sessions"
      inverse="true"
      cascade="all-delete-orphan"
      >
      <key column="clientKey"/>
      <one-to-many class="Session"/>
   </set>
  </joined-subclass>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
      session.setClient(client);
      session.setId(new UUID());
      client.add(session);
      update(client);


Full stack trace of any exception that occurs:
Code:
ERROR [main] def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.tm.proXDriver.model.Client#3]
   at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1635)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2208)
   at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
   at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)

Name and version of the database you are using:
MySQL 5.0.27
The generated SQL (show_sql=true):
Code:
Hibernate: update PERSON set ts=?, id=?, firstName=?, lastName=?, gender=?, email=?, phone=?, dob=?, lastSynch=?, locationKey=?, addressKey=? where personKey=? and ts=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 10:04 am 
Newbie

Joined: Fri Nov 10, 2006 7:26 pm
Posts: 11
Research n testing reveal the following:

1) I've implemented a 'refresh' action in my RCP application which, in the current Hibernate session, clears out the object graph starting at the root and re-reads them from the DB. When I perform that refresh operation while interleaving operations b/t the RCP app and the web app all is fine.

2) A call to 'evict()' on the object that was modified in the web app and I'm trying to modify again in my RCP app causes the 'update PERSON set ts=?,....' statement to be fired which causes the StaleObjectStateException. Hmmm.

I'll try to mimic the object graph refresh but from some point in the middle of the graph vs. on the top to see what happens.....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 11:19 am 
Newbie

Joined: Fri Nov 10, 2006 7:26 pm
Posts: 11
I think I have this licked. I followed Bruce Alspaugh's advice to use the 'application transaction pattern'. The database is hit quite a bit more as I try to synchronize with what is displayed vs. what is in the DB.

The new pattern I use actually consists of three patterns:

1) The workbench adapters to fill the tree viewer views. These adapters are hit *a lot*. I've implemented a simple object caching technique using the object version facility in Hibernate. Works nice.

2) the action routines that create, edit, or delete objects from the Hibernate model. These are triggered from user actions on the tree viewers.

3) The editors that edit blobs of data from the object model. I'm not quite done with this pattern, but will finish w/in the day.

I'm sleeping muuuuch better these daze now that I have this problem solved......

RB


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.