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!");
                }
            }
        }
    }
    

      

  • 相关阅读:
    js01
    js---18miniJquery
    js---17继承中方法属性的重写
    js---16继承
    js---16原型链
    js---15深拷贝浅拷贝 原型链
    js---14公有私有成员方法
    js---13 this call apply
    js---12对象创建方式,构造器,原型
    ESXi导出的CentOS7 ovf文件导入到workstation 无法打开GUI登录界面的问题解决方案
  • 原文地址:https://www.cnblogs.com/mschen/p/8268494.html
Copyright © 2011-2022 走看看