-->
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: Help atomically moving data between two databases
PostPosted: Thu Mar 11, 2004 12:09 am 
Newbie

Joined: Sun Oct 12, 2003 6:33 pm
Posts: 7
Hello,

I use Hibernate 2.1 and my application has two databases, one local (MS Access) and a remote (SAP DB). The databases have the same tables and from time to time I move data from the local to the remote one (data is deleted from the local database in the process).

For the sake of simplicity let's assume I need to transfer all local objects from class "MyEntry" (which has no relationships) to the remote database.

The code that does the transfer is as follows:

Code:

// Initializes local and remote session objects
// From two distinct session factories (local and remote)

MyEntry myEntry = null;

try
{
   String query   = "from MyEntry";
   List   matches = localSession.find( query );
   for ( Iterator it = matches.iterator(); it.hasNext(); )
   {
      myEntry = ( MyEntry ) it.next();
      remoteSession.save( myEntry.clone() );
      localSession.delete( myEntry );
   }
   remoteTransaction.commit();
   localTransaction.commit();
}
catch ( HibernateException e )
{
   try
   {
      remoteTransaction.rollback();
      localTransaction.rollback();
   }
   catch ( HibernateException e2 )   {}
}
finally
{
   try
   {
      remoteSession.close();
      localSession.close();
   }
   catch ( HibernateException e ) {}
}


This works 99% of the time; however, as MS Access is not really supported, it crashes esporadically at the line

Code:
   localSession.delete( myEntry );


The immediate result is that the object is not deleted locally. I tried to protect myself from any eventual exceptions by rolling back both the local and remote transactions. My intent was that the object that was just saved remotely should be erased when the remote transaction is rolled back.

However, this is not working, for the remote object keeps being stored even after rollback. When the transfer routine is called again, the object that was not deleted locally is being sent to the remote database once again.

I ask any Hibernate experts out there what am I missing and what is the correct approach to this use case ("atomic transfer between databases").

I am also posting the exception raised by MS Access just in case anyone has a hint of what could be causing it.

java.sql.SQLException: [Microsoft][Microsoft Access ODBC Driver] Could not update; currently blocked by user 'admin' on machine 'ETC'

Thanks everyone


[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 12:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
I ask any Hibernate experts out there what am I missing and what is the correct approach to this use case ("atomic transfer between databases").


XA transactions. Although I kind of doubt that Access supports XA transactions.

Regardless, this really isn't a Hibernate question/issue. Its a question of coordinating the transactions from the "local session" and the "remote session". That's the whole concept behind XA.

The other approach (the one we currently use in our apps) is "Message Oriented Middleware" (MOM). The idea is to perform the migration in stages through a MOM process (typically JMS based). Even here, though, the use of Access and its lack of trasaction support is going to make it tricky.


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.