You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Hiu Yen Onn <yo...@shinyang.com.my> on 2003/10/17 05:01:00 UTC

i want to rollback if a PersistenceBroker transaction failed

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: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: i want to rollback if a PersistenceBroker transaction failed

Posted by Danilo Tommasina <dt...@risksys.com>.
Hi Yennon,

As Thomas said, if 'doc' itself is a persistent object and all the 
references to its related objects are correctly defined in your 
repository.xml, everything should rolled back automatically.
However the transaction/rollback functionality is a database feature not 
an OJB feature, OJB only offers an easy programming interface to control 
transaction boundaries and rollback. So if your database does not 
support transactions the problem you described is absolutely normal.

Note that also MySQL before version 4.0 does not support transactions, 
MySQL > 4.0 has transaction support, but you have to configure it 
explicitely.

cheers
danilo

> Hello Yennon,
> 
> As the broker.store(doc) happens *inside* the broker transaction the 
> implicit store operations for item1 and item2 also happen within the 
> same transaction.
> 
> if an error occurs somewhere within broker.store() a 
> PersistenceBrokerException is thrown.
> You have to catch it and explicitely rollback the transaction as in the 
> following code
> 
> try
> {
>   broker.beginTransaction();
>   broker.store(doc);
>   broker.commitTransaction();
> }
> catch (PersistenceBrokerException ex)
> {
>   // in case of errors roll back the transaction
>   broker.abortTransaction();
> }
> finally
> {
>   // return the broker back to the pool once the work is done.
>   broker.close();
> }
> 
> If you still find depend objects in the database it might be an 
> indicatio that your database does not properly support transactions.
> (Ms Access for example)
> 
> cheers,
> Thomas
> 
> Hiu Yen Onn wrote:
> 
>> if 'doc' and 'item' are both persistence objects, and they are having m:n
>> relationship.
>> if i set [auto-update=true], then by persisting 'doc' will automatically
>> persisting 'item' too.
>>
>> just make a testcase. i have a 'doc' consists of 3 'item'.
>> if i coded as such
>>
>>     broker.beginTransaction();
>>     broker.store(doc);
>>     broker.commitTransaction();
>>
>> by default, if no errors, all of them are persisted into db.
>> BUT, what if, the transaction met error at 'item2'? then, the 
>> transaction is
>> terminated.
>> but, it was not rolled back. 'doc' and 'item1' is persisted into db.
>>
>>
>> do u mean i need to set auto-update=false? then, i need to store the 
>> 'item'
>> in a seperated
>> transaction? i have no glue over this. pls advise.
>> my target is just wanting 1 transaction storing 'doc' object, bundled 
>> with
>> several 'item' objects. if any of the object is failed. then, everything
>> will be rolled back.
>>
>> hope to hear u soon...thanks
>>
>> Cheers,
>> yenonn
>>
>>
>>
>> -----Original Message-----
>> From: Danilo Tommasina [mailto:dtommasina@risksys.com]
>> Sent: Friday, October 17, 2003 4:57 PM
>> To: OJB Users List
>> Subject: Re: i want to rollback if a PersistenceBroker transaction
>> failed
>>
>>
>> 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:dtommasina@risksys.com]
>>> 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: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: i want to rollback if a PersistenceBroker transaction failed

Posted by Thomas Mahler <th...@web.de>.
Hello Yennon,

As the broker.store(doc) happens *inside* the broker transaction the 
implicit store operations for item1 and item2 also happen within the 
same transaction.

if an error occurs somewhere within broker.store() a 
PersistenceBrokerException is thrown.
You have to catch it and explicitely rollback the transaction as in the 
following code

try
{
   broker.beginTransaction();
   broker.store(doc);
   broker.commitTransaction();
}
catch (PersistenceBrokerException ex)
{
   // in case of errors roll back the transaction
   broker.abortTransaction();
}
finally
{
   // return the broker back to the pool once the work is done.
   broker.close();
}

If you still find depend objects in the database it might be an 
indicatio that your database does not properly support transactions.
(Ms Access for example)

cheers,
Thomas

Hiu Yen Onn wrote:
> if 'doc' and 'item' are both persistence objects, and they are having m:n
> relationship.
> if i set [auto-update=true], then by persisting 'doc' will automatically
> persisting 'item' too.
> 
> just make a testcase. i have a 'doc' consists of 3 'item'.
> if i coded as such
> 
> 	broker.beginTransaction();
> 	broker.store(doc);
> 	broker.commitTransaction();
> 
> by default, if no errors, all of them are persisted into db.
> BUT, what if, the transaction met error at 'item2'? then, the transaction is
> terminated.
> but, it was not rolled back. 'doc' and 'item1' is persisted into db.
> 
> 
> do u mean i need to set auto-update=false? then, i need to store the 'item'
> in a seperated
> transaction? i have no glue over this. pls advise.
> my target is just wanting 1 transaction storing 'doc' object, bundled with
> several 'item' objects. if any of the object is failed. then, everything
> will be rolled back.
> 
> hope to hear u soon...thanks
> 
> Cheers,
> yenonn
> 
> 
> 
> -----Original Message-----
> From: Danilo Tommasina [mailto:dtommasina@risksys.com]
> Sent: Friday, October 17, 2003 4:57 PM
> To: OJB Users List
> Subject: Re: i want to rollback if a PersistenceBroker transaction
> failed
> 
> 
> 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:dtommasina@risksys.com]
>>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: ojb-user-unsubscribe@db.apache.org
>>>For additional commands, e-mail: ojb-user-help@db.apache.org
>>>
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>>For additional commands, e-mail: ojb-user-help@db.apache.org
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>>For additional commands, e-mail: ojb-user-help@db.apache.org
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


RE: i want to rollback if a PersistenceBroker transaction failed

Posted by Hiu Yen Onn <yo...@shinyang.com.my>.
if 'doc' and 'item' are both persistence objects, and they are having m:n
relationship.
if i set [auto-update=true], then by persisting 'doc' will automatically
persisting 'item' too.

just make a testcase. i have a 'doc' consists of 3 'item'.
if i coded as such

	broker.beginTransaction();
	broker.store(doc);
	broker.commitTransaction();

by default, if no errors, all of them are persisted into db.
BUT, what if, the transaction met error at 'item2'? then, the transaction is
terminated.
but, it was not rolled back. 'doc' and 'item1' is persisted into db.


do u mean i need to set auto-update=false? then, i need to store the 'item'
in a seperated
transaction? i have no glue over this. pls advise.
my target is just wanting 1 transaction storing 'doc' object, bundled with
several 'item' objects. if any of the object is failed. then, everything
will be rolled back.

hope to hear u soon...thanks

Cheers,
yenonn



-----Original Message-----
From: Danilo Tommasina [mailto:dtommasina@risksys.com]
Sent: Friday, October 17, 2003 4:57 PM
To: OJB Users List
Subject: Re: i want to rollback if a PersistenceBroker transaction
failed


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:dtommasina@risksys.com]
> 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: ojb-user-unsubscribe@db.apache.org
>>For additional commands, e-mail: ojb-user-help@db.apache.org
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: i want to rollback if a PersistenceBroker transaction failed

Posted by Danilo Tommasina <dt...@risksys.com>.
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:dtommasina@risksys.com]
> 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: ojb-user-unsubscribe@db.apache.org
>>For additional commands, e-mail: ojb-user-help@db.apache.org
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


RE: i want to rollback if a PersistenceBroker transaction failed

Posted by Hiu Yen Onn <yo...@shinyang.com.my>.
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:dtommasina@risksys.com]
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: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: i want to rollback if a PersistenceBroker transaction failed

Posted by Danilo Tommasina <dt...@risksys.com>.
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: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org