System.Data.SQLite事务锁定整个数据库

时间:2018-10-24 23:53:18

标签: sqlite

我正在尝试将来自不同交易的2张表插入。不幸的是,我在cn2上遇到了“数据库锁定”的情况。执行...

            var cb = new System.Data.SQLite.SQLiteConnectionStringBuilder
        {
            BinaryGUID = true,
            DataSource = string.Format("file:SqliteTest-{0:N}.db", Guid.NewGuid()),
            FailIfMissing = false,
            JournalMode = System.Data.SQLite.SQLiteJournalModeEnum.Wal,
            LegacyFormat = false,
            Pooling = true,
            SyncMode = System.Data.SQLite.SynchronizationModes.Normal,
            DefaultIsolationLevel = System.Data.IsolationLevel.ReadCommitted
        };

        using (var cn1 = new System.Data.SQLite.SQLiteConnection(cb.ToString()))
        {
            cn1.Open();
            cn1.Execute("create table t_1(uuid BLOB not null primary key, ts INTEGER not null);");
            cn1.Execute("create table t_2(uuid BLOB not null primary key, ts INTEGER not null);");
            using (var cn2 = (System.Data.SQLite.SQLiteConnection)cn1.Clone())
            {
                using (var tr1 = cn1.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
                using (var tr2 = cn2.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
                {
                    cn1.Execute("insert into t_1(uuid,ts) values(@uuid, @ts);", 
                                new { uuid = Guid.NewGuid(), ts = DateTime.UtcNow.Ticks }, tr1);
                    cn2.Execute("insert into t_2(uuid,ts) values(@uuid, @ts);",
                                new { uuid = Guid.NewGuid(), ts = DateTime.UtcNow.Ticks }, tr2);
                }
            }
        }

0 个答案:

没有答案