C# SQLite database locked exception

1

I'm trying to save data to my database by using SaveChangesAsync method, at first the user is logging in (the user's isOnline bool property as true and saves the changes to database) but when im logging out im doing the exact same method (with isOnline setting to false) and then at the SaveChangesAsync - SQLiteException is popping, I looked at the exception information and it says the database is somehow locked.

  public readonly ProjectDbContext _projectDbContext;
        public UserRepository(ProjectDbContext projectDbContext)
        {
            _projectDbContext = projectDbContext;
        }

   public async Task<User> Logout(User user)
        {
            var userList = _projectDbContext.Users.ToList();

            foreach (var userX in userList)
            {
                if (userX.Id == user.Id)
                {
                    userX.IsOnline = false;
                    try
                    {
                        await _projectDbContext.SaveChangesAsync();
                    }
                    catch (SqliteException e)
                    {
                        var str = e.Data;
                        return userX;
                    }

                    return userX;
                }
            }

            return null;
        }

        public async Task<User> GetUserAsync(User user)
        {
            var userList = _projectDbContext.Users.ToList();

            foreach (var userX in userList)
            {
                if (userX.UserName == user.UserName && userX.Password == await HashMD5(user.Password))
                {
                    userX.IsOnline = true;
                    await _projectDbContext.SaveChangesAsync();

                    return userX;
                }
            }

            return null;         
        }

The exception message:

{Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 5: 'database is locked'.
   at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
   at Microsoft.Data.Sqlite.SqliteConnectionExtensions.ExecuteNonQuery(SqliteConnection connection, String commandText)
   at Microsoft.Data.Sqlite.SqliteTransaction..ctor(SqliteConnection connection, IsolationLevel isolationLevel)
   at Microsoft.Data.Sqlite.SqliteConnection.BeginTransaction(IsolationLevel isolationLevel)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionWithNoPreconditions(IsolationLevel isolationLevel)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionAsync(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple`2 parameters, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList`1 entriesToSave, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at ServerSideAngularProject.DAL.UserRepository.Logout(User user) in C:\Users\osher\source\repos\ServerSideAngularProject\ServerSideAngularProject\DAL\UserRepository.cs:line 33}
c#
multithreading
sqlite
asked on Stack Overflow Jun 5, 2019 by O. Dror • edited Jun 5, 2019 by O. Dror

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0