Advertisement
Guest User

Untitled

a guest
May 8th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using System;
  2. using System.Data.Common;
  3. using System.Data.SQLite;
  4. using System.Linq;
  5. using System.Threading;
  6.  
  7. namespace SQLiteConcurrencyTest
  8. {
  9.     internal class Program
  10.     {
  11.         private static void Main(string[] args)
  12.         {
  13.             new Test();
  14.         }
  15.     }
  16.  
  17.     internal class Test
  18.     {
  19.         private const int Range = 5000;
  20.  
  21.         private static readonly string[] TestStrings =
  22.         {
  23.             "Lorem Ipsum",
  24.             "Phasellus luctus velit in magna facilisis, vitae fringilla velit fringilla. Nulla tempor eget lectus nec facilisis. Fusce gravida lacinia molestie.",
  25.             "Nulla facilisi. Etiam pulvinar lorem et lectus condimentum suscipit. Ut faucibus nulla consectetur, ullamcorper urna eleifend, gravida libero."
  26.         };
  27.  
  28.         [ThreadStatic] private static Random _random;
  29.  
  30.         public Test()
  31.         {
  32.             // Create new, empty file
  33.             SQLiteConnection.CreateFile("test.db3");
  34.  
  35.             using (DbConnection localConnection = CreateConnection())
  36.             {
  37.                 DoQuery(localConnection, "CREATE TABLE test ( [key] int NOT NULL, [value] text NOT NULL, PRIMARY KEY ([key]))");
  38.             }
  39.  
  40.             // Create and start worker threads
  41.             for (int i = 0; i < 3; i++)
  42.             {
  43.                 new Thread(InsertOrReplace).Start();
  44.             }
  45.  
  46.             new Thread(Select).Start();
  47.         }
  48.  
  49.         private DbConnection CreateConnection()
  50.         {
  51.             var connectionStringBuilder = new SQLiteConnectionStringBuilder
  52.                                           {
  53.                                               DataSource = "test.db3",
  54.                                               Password = "something",
  55.                                               SyncMode = SynchronizationModes.Off,
  56.                                              
  57.                                               // Setting JournalMode to Wal eliminates the problem.
  58.                                              
  59.                                               // JournalMode = SQLiteJournalModeEnum.Wal,
  60.  
  61.                                               // OR setting CacheSize to 0 greatly reduces the problem.
  62.                                               // However, exceptions still occur from time to time.
  63.  
  64.                                               //CacheSize = 0
  65.                                           };
  66.  
  67.             return new SQLiteConnection(connectionStringBuilder.ToString()).OpenAndReturn();
  68.         }
  69.        
  70.         private void InsertOrReplace()
  71.         {
  72.             Console.WriteLine("Start Insert Or Replace thread...");
  73.  
  74.             _random = new Random();
  75.  
  76.             using (DbConnection localConnection = CreateConnection())
  77.             {
  78.                 for (int j = 0; j < 2000; j++)
  79.                 {
  80.                     DoQuery(localConnection,
  81.                         String.Format("INSERT OR REPLACE INTO test (key, value) VALUES ({0}, '{1}{2}')", _random.Next(Range), TestStrings[_random.Next(TestStrings.Count())], _random.Next(Range)));
  82.                 }
  83.             }
  84.  
  85.             Console.WriteLine("End Insert Or Replace thread...");
  86.         }
  87.  
  88.         private void Select()
  89.         {
  90.             Console.WriteLine("Start Select thread...");
  91.  
  92.             _random = new Random();
  93.  
  94.             using (DbConnection localConnection = CreateConnection())
  95.             {
  96.                 for (int i = 0; i < 30000; i++)
  97.                 {
  98.                     DoQuery(localConnection, String.Format("SELECT * FROM test WHERE key={0}", _random.Next(Range)));
  99.                 }
  100.             }
  101.             Console.WriteLine("End Select thread...");
  102.         }
  103.  
  104.         private void DoQuery(DbConnection conn, String query)
  105.         {
  106.             DbCommand command = conn.CreateCommand();
  107.             command.CommandText = query;
  108.             command.ExecuteNonQuery();
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement