Any way to peek at the exception currently being thrown?

kdevel kdevel at vogtner.de
Thu Jun 17 20:49:39 UTC 2021


On Thursday, 17 June 2021 at 20:35:42 UTC, Steven Schveighoffer 
wrote:
[...]
> Thought of a great usecase for this (or something like it). 
> This is pretty much code from my application, I do this all 
> over:
>
> ```d
> auto conn = DB.getConnection;
>
> conn.exec("START TRANSACTION");
> scope(success) conn.exec("COMMIT");
> scope(failure) conn.exec("ROLLBACK");

Is the explicit rollback really necessary? Which DBMS does not 
roll a running transaction back if the connection terminates?

> While this works, and isn't too horrible, what is horrible is 
> the fact that I can only do this ONCE. So I can't defensively 
> use a reentrant transaction troika as above. I have to rely on 
> the top level doing the transaction (which means this code gets 
> repeated everywhere, and I can't do it in helper functions).
>
> I thought of making a type that automatically either commits or 
> rolls back based on whether we are throwing an exception or 
> not. But there's no way to figure that out in the destructor of 
> the type.

Why don't you make the COMMIT explicit (by implementing commit 
method) and let the destructor ROLLBACK the transactino 
unconditionally?


More information about the Digitalmars-d mailing list