Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Data.SQLite;
- using System.Diagnostics;
- using System.Threading;
- namespace SqliteTest
- {
- public class BlockingSQLiteConnection : IDbConnection
- {
- private static readonly object GlobalLock = new object();
- private readonly SQLiteConnection _sqLiteConnection;
- public string ConnectionString { get { return _sqLiteConnection.ConnectionString; } set { _sqLiteConnection.ConnectionString = value; } }
- public int ConnectionTimeout { get { return _sqLiteConnection.ConnectionTimeout; } }
- public string Database { get { return _sqLiteConnection.Database; } }
- public ConnectionState State { get { return _sqLiteConnection.State; } }
- private Stopwatch sw;
- public BlockingSQLiteConnection(string connectionString)
- {
- Monitor.Enter(GlobalLock);
- try
- {
- sw = Stopwatch.StartNew();
- _sqLiteConnection = new SQLiteConnection(connectionString);
- }
- catch
- {
- Monitor.Exit(GlobalLock);
- throw;
- }
- }
- public void Dispose()
- {
- _sqLiteConnection.Dispose();
- Console.WriteLine(" -> " + sw.ElapsedMilliseconds);
- Monitor.Exit(GlobalLock);
- }
- public IDbTransaction BeginTransaction()
- {
- return _sqLiteConnection.BeginTransaction();
- }
- public IDbTransaction BeginTransaction(IsolationLevel isolationLevel)
- {
- return _sqLiteConnection.BeginTransaction(isolationLevel);
- }
- public void Close()
- {
- _sqLiteConnection.Clone();
- }
- public void ChangeDatabase(string databaseName)
- {
- _sqLiteConnection.ChangeDatabase(databaseName);
- }
- IDbCommand IDbConnection.CreateCommand()
- {
- return _sqLiteConnection.CreateCommand();
- }
- public SQLiteCommand CreateCommand()
- {
- return _sqLiteConnection.CreateCommand();
- }
- public void Open()
- {
- _sqLiteConnection.Open();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment