zoukankan      html  css  js  c++  java
  • SQLite multiple threads

    const int loops = 1000;
    
    public void DatabaseThreadSafetyTest()
    {
        var backgroundThread = new Thread(new System.Threading.ThreadStart(() =>
        {
            for (int i = 1; i <= loops; i++)
            {
                Console.WriteLine("Background thread loop " + i);
                using (var db = new SQLiteConnection(DbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache)) {
                    db.Insert (new MyClass());
                }
            }
        }));
        backgroundThread.Start();
    
        for (int i = 1; i <= loops; i++)
        {
            Console.WriteLine("Main thread loop " + i);
            using (var db = new SQLiteConnection(DbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache)) {
                db.Insert (new MyClass());
            }
        }
    }
    

     

    using System;
    using System.Data.SQLite;
    using System.Threading.Tasks;
    
    namespace SQLiteTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var tasks = new Task[100];
    
                for (int i = 0; i < 100; i++)
                {
                    tasks[i] = new Task(new Program().WriteToDB);
                    tasks[i].Start();
                }
    
                foreach (var task in tasks)
                    task.Wait();
            }
    
            public void WriteToDB()
            {
                try
                {
                    using (SQLiteConnection myconnection = new SQLiteConnection(@"Data Source=c:123.db"))
                    {
                        myconnection.Open();
                        using (SQLiteTransaction mytransaction = myconnection.BeginTransaction())
                        {
                            using (SQLiteCommand mycommand = new SQLiteCommand(myconnection))
                            {
                                Guid id = Guid.NewGuid();
    
                                mycommand.CommandText = "INSERT INTO Categories(ID, Name) VALUES ('" + id.ToString() + "', '111')";
                                mycommand.ExecuteNonQuery();
    
                                mycommand.CommandText = "UPDATE Categories SET Name='222' WHERE ID='" + id.ToString() + "'";
                                mycommand.ExecuteNonQuery();
    
                                mycommand.CommandText = "DELETE FROM Categories WHERE ID='" + id.ToString() + "'";
                                mycommand.ExecuteNonQuery();
                            }
                            mytransaction.Commit();
                        }
                    }
                }
                catch (SQLiteException ex)
                {
                    if (ex.ReturnCode == SQLiteErrorCode.Busy)
                        Console.WriteLine("Database is locked by another process!");
                }
            }
        }
    }
    

      

  • 相关阅读:
    字节跳动2020年九月笔试题-爬楼梯(不能连续爬两个两步)
    c/c++经典面试题-part1
    C++单例模式之一见钟情
    多线程同步的四种方式(史上最详细+用例)
    c++多态之动态绑定
    Redis从入门到入坑
    编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串 是由同一字符组成的。
    面试之进制转换函数
    c++编程题之空调遥控器
    static 和 const关键字的作用
  • 原文地址:https://www.cnblogs.com/mschen/p/8268494.html
Copyright © 2011-2022 走看看