Any way to peek at the exception currently being thrown?

Steven Schveighoffer schveiguy at gmail.com
Thu Jun 17 20:59:06 UTC 2021


On 6/17/21 4:49 PM, kdevel wrote:
> 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?

The connection is not terminated (it's kept in a reusable pool)

> 
>> 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?

Yeah, that's possible, but then I still have to do:

auto txn = conn.beginTransaction;
scope(success) txn.commit;

In which the `commit` call only does something on the outer-most txn.

Not much better. And a bit awkward.

-Steve


More information about the Digitalmars-d mailing list