Howto use transactions correctly?

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Howto use transactions correctly?

Post by bairog » Fri 27 Nov 2015 06:53

For example I have the following code (extremely simplified):

Code: Select all

...
var connection = ((IObjectContextAdapter)dbContext).ObjectContext.Connection;
connection.Open();
using (DbTransaction transaction = connection.BeginTransaction())
            {
                dbContext.Users.Add(new User() { Id = 1, Name = "111" });
                dbContext.SaveChanges();

                dbContext.Users.Add(new User() { Id = 2, Name = "122" });
                dbContext.SaveChanges();

                dbContext.Users.Add(new User() { Id = 3, Name = "333" });
                
                transaction.Commit();
            }
connection.Close();
...
1) Is that correct transaction usage scenario?
2) What data will be written to database? All 3 users, or only 2 (does transaction.Commit() performs dbContext.SaveChanges() automatically)?
3) Is there a difference in what dbContext.SaveChanges() actually do when it is called separately or within a transaction?

Thank you.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Howto use transactions correctly?

Post by Shalex » Fri 27 Nov 2015 17:56

1. Yes, that is.
2. Only 2.
3. Please enable the dbMonitor tool and compare the database activity in both cases.

bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Re: Howto use transactions correctly?

Post by bairog » Mon 30 Nov 2015 06:48

Ok, I've got it.
And is there a diffference between calling SaveChanges() many times before commiting

Code: Select all

using (DbTransaction transaction = connection.BeginTransaction())
            {
                dbContext.Users.Add(new User() { Id = 1, Name = "111" });
                dbContext.SaveChanges();

                dbContext.Users.Add(new User() { Id = 2, Name = "222" });
                dbContext.SaveChanges();

                dbContext.Users.Add(new User() { Id = 3, Name = "333" });
                dbContext.SaveChanges();

                transaction.Commit();
            }
and calling SaveChanges() only one time just before the commit?

Code: Select all

using (DbTransaction transaction = connection.BeginTransaction())
            {
                dbContext.Users.Add(new User() { Id = 1, Name = "111" });                
                dbContext.Users.Add(new User() { Id = 2, Name = "222" });
                dbContext.Users.Add(new User() { Id = 3, Name = "333" });

                dbContext.SaveChanges();

                transaction.Commit();
            }
Finally 3 users will be written into database in both examples, but which one is better transaction using practice and why?
Last edited by bairog on Tue 01 Dec 2015 08:28, edited 1 time in total.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Howto use transactions correctly?

Post by Shalex » Tue 01 Dec 2015 08:25

bairog wrote:Finally 3 users will be written into database in both examoles
That is correct. The generated SQL statements are equal in the both cases.
bairog wrote:but which one is better transaction using practice and why?
It is better to use the second example because more entities attached to context means more time for validation of changes when each SaveChanges() is called.

bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Re: Howto use transactions correctly?

Post by bairog » Tue 01 Dec 2015 08:38

Thx, I will use that recomendation.

Post Reply