broker.store() can be called only on persistent objects defined in your data-model. So if 'doc' is not a persistent object itself, then you will have to extract all the persistent objects and store them with several calls to broker.store(), something like this:

broker = getBroker(new PBKey(site.getDbConnectionAlias()));

        broker.beginTransaction();
        broker.store( doc.getItem1() );
        broker.store( doc.getItem2() );
        broker.store( doc.getItem3() );
        broker.commitTransaction();

now if store of item1 is ok, but an exception is thrown while storing item2, the exception is caught in the try-catch block, the transaction aborted and a rollback is called in the database.
This means that all successful modifications done after broker.beginTransaction() will be rolled back at database level.
However the 'doc' object itself will not be 'rolled back', until you don't reload all the data from database.


cheers
danilo

hi,

let's say, 'doc' contains of bunch of data. let's make it 'doc' contains of
item1, item2 and item3.
if let say, when the ojb persisting 'doc', item1 is persisted successfully,
but, item2 failed. will ojb rollback in this case (make no changes on the
db, even item1 is not persisted)?
how can i do this? tracing the TransactionAbortedException??
pls advise... thanks..



Cheers,
yenonn

-----Original Message-----
From: Danilo Tommasina [mailto:[EMAIL PROTECTED]
Sent: Friday, October 17, 2003 3:29 PM
To: OJB Users List
Subject: Re: i want to rollback if a PersistenceBroker transaction
failed


try this code.


try{
        result = doc.getResult();
        broker = getBroker(new PBKey(site.getDbConnectionAlias()));

        broker.beginTransaction();
        broker.store(doc, ObjectModificationDefaultImpl.INSERT);
        broker.commitTransaction();

//IF THERE IS NO SPECIAL REASON FOR THIS, IT IS NOT NECESSARY
//      broker.clearCache();

        result.setValidity(IResult.SAVED_SUCCESS);
        return doc;
}catch( Exception e ){
        if ( ( broker != null ) && ( broker.isInTransaction() ) ) {
                broker.abortTransaction();
        }
        pb.removeFromCache( doc );  //Make sure it is not cached
        throw new ServiceException(e);
}finally{
        if( broker != null ){
                broker.close();
        }
}

Note that the content of the 'doc' instance that you return will not be
rolled back, you have to reload its contents from the database.

bye
danilo


hi,

i cant make a rollback if a PersistenceBorker transaction is failed...
my snippet is as followed.... pls advised.... thanks....

try{
        result = doc.getResult();
        broker = getBroker(new PBKey(site.getDbConnectionAlias()));

        broker.beginTransaction();
        if(broker.isInTransaction())
                broker.store(doc, ObjectModificationDefaultImpl.INSERT);
        else{
                broker.abortTransaction();
        }
        broker.commitTransaction();

        broker.clearCache();
        result.setValidity(IResult.SAVED_SUCCESS);
        return doc;
}catch(TransactionAbortedException e){
        broker.abortTransaction();
        throw new ServiceException(e);
}catch(PersistenceBrokerException e){
        broker.abortTransaction();
        throw new ServiceException(e);
}finally{
        if(broker != null){
                broker.close();
}


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]




--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to