my aim was to rollback the transaction in a way that all write operations will not commit
but its not working.
i tried this method that worked on sql server and mysql but its not working for me here. my test is to create a table and drop it but i keep see that the write opertions work even that a rollback.
private async Task<List<Dictionary<string, object>>> ExecuteQueryAsync(string host, string connectionString, string query)
{
var results = new List<Dictionary<string, object>>();
try
{
await using var conn = new OracleConnection(connectionString);
_logger.LogInformation($"[{DateTime.Now}] [Thread: {Thread.CurrentThread.ManagedThreadId}] Attempting to open connection to {host} with connection string {connectionString}");
await conn.OpenAsync();
_logger.LogInformation($"[{DateTime.Now}] [Thread: {Thread.CurrentThread.ManagedThreadId}] Successfully opened connection to {host}");
var tran = conn.BeginTransaction(IsolationLevel.ReadCommitted); // Begin a transaction
try
{
await using var cmd = conn.CreateCommand();
cmd.CommandText = query;
cmd.Transaction = tran; // Associate the transaction with the command
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var row = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
var value = reader.IsDBNull(i) ? null : reader.GetValue(i);
row[reader.GetName(i)] = value;
}
results.Add(row);
}
await tran.RollbackAsync();
}
catch (Exception ex)
{
await tran.RollbackAsync();
_logger.LogError(ex, $"Error executing query on host {host}");
throw;
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error executing query on host {host}");
var errorResult = new Dictionary<string, object>
{
{ "host", host },
{ "Error", $"General error: {ex.Message}" }
};
results.Add(errorResult);
}
return results;
}
New contributor
yochai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.