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]