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!");
}
}
}
}
SQLite multiple threads的更多相关文章
- Multiple Threads reading from the same file(转载)
问 I have a xml file that needs to be read from many many times. I am trying to use the Parallel.ForE ...
- Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类
Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...
- caffe网络在多线程中无法使用GPU的解决方案 | cpp caffe net run in multiple threads
本文首发于个人博客https://kezunlin.me/post/8d877e63/,欢迎阅读! cpp caffe net run in multiple threads Guide set_mo ...
- bsxfun.h multiple threads backup
https://code.google.com/p/deep-learning-faces/source/browse/trunk/cuda_ut/include/bsxfun.h?r=7&s ...
- Hashtable insert failed. Load factor too high. The most common cause is multiple threads writing to the Hashtable simultaneously
暂时也没准确定位到问题 https://support.microsoft.com/zh-cn/help/2803754/hotfix-rollup-2803754-is-available-for- ...
- 临界区代码 critical section Locks and critical sections in multiple threads
临界区 在同步的程序设计中,临界区段(Critical section)指的是一个访问共享资源(例如:共享设备或是共享存储器)的程序片段,而这些共享资源有无法同时被多个线程访问的特性. 当有线程进入临 ...
- PatentTips - Controlling TSC offsets for multiple cores and threads
BACKGROUND Many processors include a time stamp count (TSC) counter which is typically implemented a ...
- 【腾讯Bugly干货分享】微信iOS SQLite源码优化实践
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57b58022433221be01499480 作者:张三华 前言 随着微信iO ...
- SQLite源程序分析之sqlite3.c
/****************************************************************************** ** This file is an a ...
随机推荐
- bootstrap动态添加Tab标签页
好久没有写博客了(主要是懒),工作中用到一个动态添加Tab的功能,众所周知,bootstrap没有动态添加Tab的功能,网上又没找到什么好用的,那咱就自己写呗?(因为懒,所以只写了添加的方法.(๑&g ...
- Activity的Launch mode详解,A B C D的singleTask模式
本文参考了此文http://hi.baidu.com/amauri3389/blog/item/a54475c2a4b2f040b219a86a.html 另附 android task与back s ...
- windows下设置PHP环境变量
1.找到“高级系统设置”(二选一的方法找到环境变量) ① 我的电脑-属性-高级-环境变量 ②win8,10 直接在搜索框搜 “查看高级系统设置”-环境变量 2.找到变量"Path" ...
- CentOS 7下安装samba
Samba是一种软件,它可以运行在非Windows平台上,比如UNIX, Linux, IBM System 390, OpenVMS或其他操作系统.Samba使用安装在主机上的TCP/IP协议.当正 ...
- Python实现代码统计工具——终极加速篇
Python实现代码统计工具--终极加速篇 声明 本文对于先前系列文章中实现的C/Python代码统计工具(CPLineCounter),通过C扩展接口重写核心算法加以优化,并与网上常见的统计工具做对 ...
- OpenJDK和JDK区别
OpenJDK和JDK区别 OpenJDK与JDK的区别分析 Sun的JDK7.OpenJDK及IcedTea释疑 简介(ps): 简单来说jdk从7开始,弄出一个可以自由使用的公共版本(openjd ...
- Hadoop 跨集群访问
[原文地址] 跨集群访问 发表于 2015-06-01 | 简单总结下跨集群访问的多种方式. 跨集群访问HDFS 直接给出HDFS URI 我们平常执行hadoop fs -ls /之类的操作 ...
- 严重: A child container failed during start的问题解决方法
找到tomcat中的server.xml中的文件, 将图中阴影的部分注释掉,即可.
- C/S,B/S的区别
一.概念说明 C/S(Client/Server(客服机/服务器))架构:客户端/服务器架构.通过将任务合理分配到Client端和Server端,降低了系统的通讯开销,需要安装客服端才可进行管理操作. ...
- 大课深度复盘、解密研发效率之道 | 第42届MPD工作坊成都站日程公布!
互联网时代,随着区块链.大数据.人工智能等技术的快速发展,产品迭代速度飞快.在这样的市场环境下,提升研发效率.降低研发成本,同时支撑业务的快速发展,是每个企业都追求的目标之一. 大中型企业如何快速转型 ...