why scope(success)?

Walter Bright newshound at digitalmars.com
Sat May 13 10:59:58 PDT 2006


Mike Capp wrote:
> In article <e42jr6$2u07$1 at digitaldaemon.com>, Walter Bright says...
>> For example, if I have operations A(), B(), and C(), and A.rollback(), 
>> B.rollback() and C.rollback:
>>
>> 	A();
>> 	scope(failure) A.rollback();
>> 	B();
>> 	scope(failure) B.rollback();
>> 	C();
>> 	scope(failure) C.rollback();
>>
>> Just for fun, try to do that with either C++ exceptions or Java try-finally.
> 
> Off the top of my head...
> 
> struct A // ditto for B and C
> {
> A() { doA(); }
> ~A() { if (std::uncaught_exception()) { rollbackA(); } }
> };
> 
> A a;
> B b;
> C c;

This doesn't work because when the scope is done, no matter how it ends, 
A, B and C are *all* rolled back. The idea is that if the scope exits 
normally, A, B and C are *not* rolled back. The starting point for C++ 
would be:

	A* a = new A();
	B* b = new B();
	C* c = new C();

I need all three to succeed or none. Think of something like a database 
transactions, where 3 different places in the database have to be 
updated as one operation. You can't leave it as 1 done, or 2 done. It 
has to be all 3, or none of them.



More information about the Digitalmars-d mailing list