Anarkin

BlockingSQLiteConnection

Dec 11th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SQLite;
  4. using System.Diagnostics;
  5. using System.Threading;
  6.  
  7. namespace SqliteTest
  8. {
  9.     public class BlockingSQLiteConnection : IDbConnection
  10.     {
  11.         private static readonly object GlobalLock = new object();
  12.  
  13.         private readonly SQLiteConnection _sqLiteConnection;
  14.  
  15.         public string ConnectionString { get { return _sqLiteConnection.ConnectionString; } set { _sqLiteConnection.ConnectionString = value; } }
  16.         public int ConnectionTimeout { get { return _sqLiteConnection.ConnectionTimeout; } }
  17.         public string Database { get { return _sqLiteConnection.Database; } }
  18.         public ConnectionState State { get { return _sqLiteConnection.State; } }
  19.  
  20.         private Stopwatch sw;
  21.  
  22.         public BlockingSQLiteConnection(string connectionString)
  23.         {
  24.             Monitor.Enter(GlobalLock);
  25.             try
  26.             {
  27.                 sw = Stopwatch.StartNew();
  28.                 _sqLiteConnection = new SQLiteConnection(connectionString);
  29.             }
  30.             catch
  31.             {
  32.                 Monitor.Exit(GlobalLock);
  33.                 throw;
  34.             }
  35.         }
  36.  
  37.         public void Dispose()
  38.         {
  39.             _sqLiteConnection.Dispose();
  40.             Console.WriteLine(" -> " + sw.ElapsedMilliseconds);
  41.             Monitor.Exit(GlobalLock);
  42.         }
  43.  
  44.         public IDbTransaction BeginTransaction()
  45.         {
  46.             return _sqLiteConnection.BeginTransaction();
  47.         }
  48.  
  49.         public IDbTransaction BeginTransaction(IsolationLevel isolationLevel)
  50.         {
  51.             return _sqLiteConnection.BeginTransaction(isolationLevel);
  52.         }
  53.  
  54.         public void Close()
  55.         {
  56.             _sqLiteConnection.Clone();
  57.         }
  58.  
  59.         public void ChangeDatabase(string databaseName)
  60.         {
  61.             _sqLiteConnection.ChangeDatabase(databaseName);
  62.         }
  63.  
  64.         IDbCommand IDbConnection.CreateCommand()
  65.         {
  66.             return _sqLiteConnection.CreateCommand();
  67.         }
  68.  
  69.         public SQLiteCommand CreateCommand()
  70.         {
  71.             return _sqLiteConnection.CreateCommand();
  72.         }
  73.        
  74.         public void Open()
  75.         {
  76.             _sqLiteConnection.Open();
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment