Site icon Chris Woody Woodruff

Transactional Savepoints in EF Core: Rollback Just What You Need!

Transactional Savepoints in EF Core: Rollback Just What You Need!

We’ve all been there—you’re halfway through a multi-step transaction, and boom! 💥 Something fails. You don’t want to roll back everything, just the part that went wrong.

That’s where Transactional Savepoints come in!

Savepoints let you partially roll back transactions, keeping the good stuff while undoing just the problematic parts. If you’ve ever wished for a “Ctrl + Z” in database operations, this is it.

Let’s dive into what savepoints are, why they’re useful, and how to use them in Entity Framework Core (EF Core)!


Why Use Transactional Savepoints?

By default, transactions in EF Core follow the “all or nothing” rule—either everything commits successfully, or the entire transaction gets rolled back. But sometimes, you don’t want to lose everything just because of a small issue.

With savepoints, you can:

Imagine you’re processing a batch of payments:


Step 1: Using Savepoints in EF Core Transactions

Let’s say we have a banking app where users can transfer money between accounts. If one transfer fails, we don’t want to cancel all the transactions—just the one that failed.

1. Start a Transaction

using var transaction = await context.Database.BeginTransactionAsync();

try
{
    await TransferMoney(1, 2, 500); // Transfer $500 from Account 1 to 2
    await context.Database.ExecuteSqlRawAsync("SAVEPOINT BeforeSecondTransfer");

    await TransferMoney(3, 4, 1000); // Transfer $1000 from Account 3 to 4
    await context.Database.ExecuteSqlRawAsync("SAVEPOINT BeforeThirdTransfer");

    await TransferMoney(5, 6, 2000); // Oops! This one might fail

    await transaction.CommitAsync(); // If everything is good, commit!
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex.Message}");
    await context.Database.ExecuteSqlRawAsync("ROLLBACK TO SAVEPOINT BeforeThirdTransfer"); // Roll back just the last one
}

What’s happening here?

  1. Start a transaction
  2. Make some transfers
  3. Create savepoints before risky operations (like large transfers)
  4. Rollback only the problematic step instead of losing everything

Step 2: Handling Savepoints in EF Core Using Transaction APIs

If you prefer EF Core’s built-in transaction API, you can do this:

using var transaction = await context.Database.BeginTransactionAsync();

try
{
    await TransferMoney(1, 2, 500);
    await transaction.CreateSavepointAsync("BeforeSecondTransfer");

    await TransferMoney(3, 4, 1000);
    await transaction.CreateSavepointAsync("BeforeThirdTransfer");

    await TransferMoney(5, 6, 2000); // This might fail

    await transaction.CommitAsync();
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex.Message}");
    await transaction.RollbackToSavepointAsync("BeforeThirdTransfer"); // Undo only the last step
    await transaction.CommitAsync(); // Keep the rest!
}

Now, you can rollback part of a transaction without discarding everything!


When Should You Use Savepoints?

1. Batch Processing (Payments, Orders, Inventory Updates)

If you’re processing multiple orders/payments in a single transaction, use savepoints to rollback only failed ones while keeping the rest.

2. Long-Running Transactions

Large transactions risk locking database resources for too long. Savepoints help recover faster without restarting the entire process.

3. Handling Conditional Logic in Transactions

If certain operations depend on previous ones, use savepoints to undo bad steps without breaking the rest.

4. Preventing Partial Data Corruption

If one step fails, but the rest are fine, rolling back everything might be unnecessary. Savepoints let you recover selectively.


Savepoints vs. Full Rollback: When to Use Each

ScenarioSavepointsFull Rollback
A single step fails in a multi-step transactionYesNo
A critical issue occurs, and everything must be undoneNoYes
Some operations should be committed while others should notYesNo
The database state must return to the exact point before the transaction startedNoYes

Savepoints are great when you want a “soft rollback” instead of a complete undo.


Common Issues & How to Fix Them

Not All Databases Support Savepoints

Savepoints Must Be Created Inside Transactions

Avoid Too Many Savepoints


Wrap-Up: Smarter Rollbacks with Savepoints

Transactional Savepoints in EF Core let you undo only what you need, keeping successful operations intact while recovering from failures. Instead of rolling back everything, savepoints let you:

Next time you’re dealing with multi-step transactions, consider using savepoints to make them more reliable!

Have you used savepoints in EF Core? Let’s chat in the comments!

Exit mobile version