-->
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: replicate + transactions
PostPosted: Mon May 31, 2004 9:52 am 
Newbie

Joined: Fri Aug 29, 2003 3:31 pm
Posts: 6
Hello.

Hibernate 1.2.3, mySQL 4.0.18 (windows).

I use replicate method for synchronization of objects between 2 databases.

I have the following code:

Code:
Session localSession = getLocalSession();
Session synchroSession = getSynchroSession();
Transaction tx = synchroSession.beginTransaction();
synchroSession.delete("from sometable in class " + getSynchronizedClass().getName());
Iterator it = localSession.iterate("from sometable in class " + getSynchronizedClass().getName());
while (it.hasNext()) {
PersistentObject object = (PersistentObject) it.next();
localSession.evict(object);
synchroSession.replicate(object, ReplicationMode.OVERWRITE);
}
tx.commit();


All i do here, is:
- begin transaction for synchro session
- remove all objects of specified class from synchro database
- load all objects of specified class from local database to iterator
- replicate them one by one to synchro database
- commit transaction for synchro session

When i run that code first time (synchro database is clear and doesn't contain any information), everything is fine and works as expected - it tries to remove old objects from synchro database (and removes nothing because synchro is empty at this moment), and replicates new objects from local to synchro.

If i run that code second time, at the end i will have empty synchro database again.

I can't understand what's wrong here, since session should first remove all instances from table and then save new ones because of replicate IMHO.

If i insert

Code:
tx.commit();
tx = synchroSession.beginTransaction();


right after calling delete method, it works fine but that doesn't seems right to me because i want to make delete and replicate in one transaction.


And the second question.

When i try to replicate object that has one-to-many using the code above, i get the following error:

Code:
17:40:24,193 ERROR SynchroAction:51 - can't synchronize all objects of type asd.Foo
net.sf.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
   at net.sf.hibernate.collection.PersistentCollection.setCurrentSession(PersistentCollection.java:257)
   at net.sf.hibernate.impl.OnReplicateVisitor.processCollection(OnReplicateVisitor.java:37)
   at net.sf.hibernate.impl.AbstractVisitor.processValue(AbstractVisitor.java:69)
   at net.sf.hibernate.impl.AbstractVisitor.processValues(AbstractVisitor.java:36)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:909)
   at net.sf.hibernate.impl.SessionImpl.replicate(SessionImpl.java:3891)
   at asd.PersistentObjectManager.replicate(PersistentObjectManager.java:1136)


That's why i have
Code:

localSession.evict(object);

in my code. If it is there, all works fine. But thats looks like a some bug.

Is that normal and i have to use evict before replicate for saving tree of objects?

Here are the mapping files:

Code:
  <class name="asd.Foo" table="foo">
    <id name="id" column="ID" type="long" unsaved-value="null">
      <generator class="hilo">
        <param name="table">hibernate_unique_key</param>
        <param name="column">next_foo</param>
        <param name="max_lo">100</param>
      </generator>
    </id>
   
    <property name="name" column="NAME" type="string"/>
    <set name="bars" lazy="false" cascade="all">
      <key column="PARENT_ID"/>
      <one-to-many class="asd.Bar"/>
    </set>       

  </class>

  <class name="asd.Bar" table="bar">
    <id name="id" column="ID" type="long" unsaved-value="null">
      <generator class="hilo">
        <param name="table">hibernate_unique_key</param>
        <param name="column">next_bar</param>
        <param name="max_lo">100</param>
      </generator>
    </id>
   
    <property name="name" column="NAME" type="string"/>
    <property name="parentId" column="PARENT_ID" type="long"/>

  </class>


Thanks in advance for any help on that topics.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 3:55 am 
Newbie

Joined: Fri Aug 29, 2003 3:31 pm
Posts: 6
Since nobody answered my questions, i turned on debug log level for hibernate and went trough the log output.

I post here only the part of output, which was generated right after i commit transaction.

Code:
11:34:23,714 DEBUG JDBCTransaction:59 - commit
11:34:23,724 DEBUG SessionImpl:2235 - flushing session
11:34:23,754 DEBUG SessionImpl:3379 - running Session.finalize()
11:34:23,754 DEBUG SessionImpl:2428 - Flushing entities and processing referenced collections
11:34:23,754 DEBUG SessionImpl:2522 - Updating entity: [asd.Dummy#608]
11:34:23,834 DEBUG SessionImpl:2522 - Updating entity: [asd.Dummy#709]
11:34:23,844 DEBUG SessionImpl:2522 - Updating entity: [asd.Dummy#710]
11:34:23,844 DEBUG SessionImpl:2522 - Updating entity: [asd.Dummy#809]
11:34:23,854 DEBUG SessionImpl:2771 - Processing unreferenced collections
11:34:23,864 DEBUG SessionImpl:2785 - Scheduling collection removes/(re)creates/updates
11:34:23,864 DEBUG SessionImpl:2259 - Flushed: 1 insertions, 4 updates, 4 deletions to 9 objects
11:34:23,874 DEBUG SessionImpl:2264 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
11:34:23,884 DEBUG Printer:75 - listing entities:
11:34:23,884 DEBUG Printer:82 - asd.Dummy{age=5, name=qweqweqwe, id=809}
11:34:23,894 DEBUG Printer:82 - asd.Dummy{age=5, name=asd, id=608}
11:34:23,904 DEBUG Printer:82 - asd.Dummy{age=5555, name=asdasdasd, id=709}
11:34:23,914 DEBUG Printer:82 - asd.Dummy{age=5555, name=zxczxzxczxczxc, id=710}
11:34:23,914 DEBUG Printer:82 - asd.Transaction{created=1086161661641, transmittedClassFqcn=asd.Dummy, type=1, removedObjects=0, updatedObjects=0, addedObjects=0, processed=false, id=1921}
11:34:23,924 DEBUG SessionImpl:2348 - executing flush
11:34:23,924 DEBUG EntityPersister:453 - Inserting entity: [asd.Transaction#1921]
11:34:23,934 DEBUG BatcherImpl:196 - about to open: 0 open PreparedStatements, 0 open ResultSets
11:34:23,944 DEBUG SQL:237 - insert into synchro_transaction (TYPE, TRANSMITTED_CLASS_FQCN, CREATED, PROCESSED, ADDED_OBJECTS, REMOVED_OBJECTS, UPDATED_OBJECTS, ID) values (?, ?, ?, ?, ?, ?, ?, ?)
11:34:23,944 DEBUG BatcherImpl:241 - preparing statement
11:34:23,954 DEBUG EntityPersister:388 - Dehydrating entity: [asd.Transaction#1921]
11:34:23,964 DEBUG IntegerType:46 - binding '1' to parameter: 1
11:34:23,964 DEBUG StringType:46 - binding 'asd.Dummy' to parameter: 2
11:34:23,984 DEBUG LongType:46 - binding '1086161661641' to parameter: 3
11:34:23,994 DEBUG BooleanType:46 - binding 'false' to parameter: 4
11:34:23,994 DEBUG IntegerType:46 - binding '0' to parameter: 5
11:34:24,004 DEBUG IntegerType:46 - binding '0' to parameter: 6
11:34:24,014 DEBUG IntegerType:46 - binding '0' to parameter: 7
11:34:24,024 DEBUG LongType:46 - binding '1921' to parameter: 8
11:34:24,024 DEBUG BatcherImpl:203 - done closing: 0 open PreparedStatements, 0 open ResultSets
11:34:24,034 DEBUG BatcherImpl:261 - closing statement
11:34:24,044 DEBUG EntityPersister:646 - Updating entity: [asd.Dummy#608]
11:34:24,044 DEBUG BatcherImpl:196 - about to open: 0 open PreparedStatements, 0 open ResultSets
11:34:24,054 DEBUG SQL:237 - update dummy set NAME=?, AGE=? where ID=?
11:34:24,064 DEBUG BatcherImpl:241 - preparing statement
11:34:24,074 DEBUG EntityPersister:388 - Dehydrating entity: [asd.Dummy#608]
11:34:24,084 DEBUG StringType:46 - binding 'asd' to parameter: 1
11:34:24,094 DEBUG IntegerType:46 - binding '5' to parameter: 2
11:34:24,104 DEBUG LongType:46 - binding '608' to parameter: 3
11:34:24,114 DEBUG EntityPersister:646 - Updating entity: [asd.Dummy#709]
11:34:24,114 DEBUG EntityPersister:388 - Dehydrating entity: [asd.Dummy#709]
11:34:24,124 DEBUG StringType:46 - binding 'asdasdasd' to parameter: 1
11:34:24,134 DEBUG IntegerType:46 - binding '5555' to parameter: 2
11:34:24,134 DEBUG LongType:46 - binding '709' to parameter: 3
11:34:24,144 DEBUG EntityPersister:646 - Updating entity: [asd.Dummy#710]
11:34:24,154 DEBUG EntityPersister:388 - Dehydrating entity: [asd.Dummy#710]
11:34:24,164 DEBUG StringType:46 - binding 'zxczxzxczxczxc' to parameter: 1
11:34:24,174 DEBUG IntegerType:46 - binding '5555' to parameter: 2
11:34:24,174 DEBUG LongType:46 - binding '710' to parameter: 3
11:34:24,184 DEBUG EntityPersister:646 - Updating entity: [asd.Dummy#809]
11:34:24,204 DEBUG EntityPersister:388 - Dehydrating entity: [asd.Dummy#809]
11:34:24,204 DEBUG StringType:46 - binding 'qweqweqwe' to parameter: 1
11:34:24,214 DEBUG IntegerType:46 - binding '5' to parameter: 2
11:34:24,224 DEBUG LongType:46 - binding '809' to parameter: 3
11:34:24,234 DEBUG BatcherImpl:203 - done closing: 0 open PreparedStatements, 0 open ResultSets
11:34:24,234 DEBUG BatcherImpl:261 - closing statement
11:34:24,244 DEBUG EntityPersister:568 - Deleting entity: [asd.Dummy#608]
11:34:24,254 DEBUG BatcherImpl:196 - about to open: 0 open PreparedStatements, 0 open ResultSets
11:34:24,264 DEBUG SQL:237 - delete from dummy where ID=?
11:34:24,264 DEBUG BatcherImpl:241 - preparing statement
11:34:24,274 DEBUG LongType:46 - binding '608' to parameter: 1
11:34:24,284 DEBUG EntityPersister:568 - Deleting entity: [asd.Dummy#709]
11:34:24,294 DEBUG LongType:46 - binding '709' to parameter: 1
11:34:24,304 DEBUG EntityPersister:568 - Deleting entity: [asd.Dummy#710]
11:34:24,314 DEBUG LongType:46 - binding '710' to parameter: 1
11:34:24,324 DEBUG EntityPersister:568 - Deleting entity: [asd.Dummy#809]
11:34:24,324 DEBUG LongType:46 - binding '809' to parameter: 1
11:34:24,334 DEBUG BatcherImpl:203 - done closing: 0 open PreparedStatements, 0 open ResultSets
11:34:24,344 DEBUG BatcherImpl:261 - closing statement
11:34:24,354 DEBUG SessionImpl:2815 - post flush
11:34:24,364 DEBUG SessionImpl:572 - transaction completion


As i understand, rows that say about updating Dummy objects was caused by replicate method. But in my code i call delete BEFORE replicate, and when commit is called, delete is completed after replicate.

Is that bug or normal behaviour?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 9:02 am 
Newbie

Joined: Fri Aug 29, 2003 3:31 pm
Posts: 6
In case someone is interested ;-) i've got solution for my problem with transactions. Instead of doing

Code:
tx.commit();
tx = synchroSession.beginTransaction();


i call

Code:
session.flush();


and this way it works as expected - everything runs within one single transaction and delete is processed before replicate.


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.