Replies: 2 comments 2 replies
-
Thanks for the code example, FYI transactions in OrmLite should be created with using var db = OpenDbConnection();
using (var transaction = db.OpenTransaction())
{
//...
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
Support for Transaction SavePoints have been added for all supported Databases in the latest ServiceStack v6.10 Release, which look like: // Sync
using (var trans = db.OpenTransaction())
{
try
{
db.Insert(new Person { Id = 2, Name = "John" });
var firstSavePoint = trans.SavePoint("FirstSavePoint");
db.UpdateOnly(() => new Person { Name = "Jane" }, where: x => x.Id == 1);
firstSavePoint.Rollback();
var secondSavePoint = trans.SavePoint("SecondSavePoint");
db.UpdateOnly(() => new Person { Name = "Jack" }, where: x => x.Id == 1);
secondSavePoint.Release();
db.Insert(new Person { Id = 3, Name = "Diane" });
trans.Commit();
}
catch (Exception e)
{
trans.Rollback();
}
} Async Versionusing (var trans = db.OpenTransaction())
{
try
{
await db.InsertAsync(new Person { Id = 2, Name = "John" });
var firstSavePoint = await trans.SavePointAsync("FirstSavePoint");
await db.UpdateOnlyAsync(() => new Person { Name = "Jane" }, where: x => x.Id == 1);
await firstSavePoint.RollbackAsync();
var secondSavePoint = await trans.SavePointAsync("SecondSavePoint");
await db.UpdateOnlyAsync(() => new Person { Name = "Jack" }, where: x => x.Id == 1);
await secondSavePoint.ReleaseAsync();
await db.InsertAsync(new Person { Id = 3, Name = "Diane" });
trans.Commit();
}
catch (Exception e)
{
trans.Rollback();
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
A transaction is a group of database operations that are executed as a single unit of work, which means that either all of them are completed successfully or none of them are. This helps to ensure data consistency and integrity in the database.
A savepoint is a point within a transaction where you can rollback to in case an error occurs, without rolling back the entire transaction. In other words, it provides a way to undo part of the transaction without undoing the entire transaction.
I can't find an API in Ormlite that can set and roll back to the specified savepoint, so I suggest adding these API,e.g:
See the Sqlserver SavePoint and Postgresql SavePoint page for more info on working with SavePoint.
Beta Was this translation helpful? Give feedback.
All reactions