private void WantTransaction() { if (_tran == null) { _tran = _conn.BeginTransaction(); } }
public SQLiteTransactionalDataSource(SQLiteDataSource dataSource, IsolationLevel?isolationLevel, bool forwardEvents) : base(new SQLiteDataSourceSettings() { DefaultCommandTimeout = dataSource.DefaultCommandTimeout, StrictMode = dataSource.StrictMode, SuppressGlobalEvents = dataSource.SuppressGlobalEvents || forwardEvents, DisableLocks = dataSource.DisableLocks }) { if (dataSource == null) { throw new ArgumentNullException(nameof(dataSource), $"{nameof(dataSource)} is null."); } Name = dataSource.Name; m_BaseDataSource = dataSource; m_Connection = dataSource.CreateConnection(); m_LockToken = SyncLock.WriterLock(); if (isolationLevel == null) { m_Transaction = m_Connection.BeginTransaction(); } else { m_Transaction = m_Connection.BeginTransaction(isolationLevel.Value); } if (forwardEvents) { ExecutionStarted += (sender, e) => dataSource.OnExecutionStarted(e); ExecutionFinished += (sender, e) => dataSource.OnExecutionFinished(e); ExecutionError += (sender, e) => dataSource.OnExecutionError(e); ExecutionCanceled += (sender, e) => dataSource.OnExecutionCanceled(e); } AuditRules = dataSource.AuditRules; UserValue = dataSource.UserValue; }
public void Flush() { if (_tran != null) { _tran.Commit(); _tran = null; _rowCountCommited = _rowCount; } }
/// <summary> /// Returns a data source wrapped around the transaction. /// </summary> /// <param name="connection">The connection.</param> /// <param name="transaction">The transaction.</param> /// <returns>SqlServerOpenDataSource.</returns> /// <exception cref="ArgumentNullException"></exception> public static SQLiteOpenDataSource AsDataSource(this SQLiteConnection connection, SQLiteTransaction transaction) { if (connection == null) throw new ArgumentNullException(nameof(connection), $"{nameof(connection)} is null."); if (connection.State == ConnectionState.Closed) connection.Open(); var dataSourceBase = s_CachedDataSources.GetOrAdd(connection.ConnectionString, cs => new SQLiteDataSource(cs)); return new SQLiteOpenDataSource(dataSourceBase, connection, transaction); }
internal SQLiteOpenDataSource(SQLiteDataSource dataSource, SQLiteConnection connection, SQLiteTransaction transaction) : base(new SQLiteDataSourceSettings() { DefaultCommandTimeout = dataSource.DefaultCommandTimeout, StrictMode = dataSource.StrictMode, SuppressGlobalEvents = dataSource.SuppressGlobalEvents, DisableLocks = dataSource.DisableLocks }) { if (connection == null) { throw new ArgumentNullException(nameof(connection), $"{nameof(connection)} is null."); } m_BaseDataSource = dataSource; m_Connection = connection; m_Transaction = transaction; }
public void InsertSpeedMicrosoft(bool prepare, bool useTransaction) { var connectionString = "Data Source=:memory:"; using (var connection = new Microsoft.Data.Sqlite.SqliteConnection(connectionString)) { connection.Open(); var command = connection.CreateCommand(); command.CommandText = "DROP TABLE IF EXISTS Numbers"; command.ExecuteNonQuery(); command.CommandText = "CREATE TABLE `Numbers` (Key INTEGER, Value REAL, PRIMARY KEY(Key));"; command.ExecuteNonQuery(); if (prepare) { command.CommandText = "INSERT INTO Numbers VALUES (@Key, @Value);"; command.Prepare(); command.Parameters.AddWithValue("@Key", 0); command.Parameters.AddWithValue("@Value", 0); } Microsoft.Data.Sqlite.SqliteTransaction txn = null; if (useTransaction) { txn = connection.BeginTransaction(); command.Transaction = txn; } for (var i = 0; i < NumberOfInserts; i++) { if (prepare) { command.Parameters["@Key"].Value = i; command.Parameters["@Value"].Value = i; } else { command.CommandText = $"INSERT INTO Numbers VALUES ({i}, {i});"; } command.ExecuteNonQuery(); } if (useTransaction) { txn.Commit(); } } }
/// <summary> /// Initializes a new instance of the <see cref="SQLiteTransactionalDataSource" /> class. /// </summary> /// <param name="dataSource">The data source.</param> /// <param name="forwardEvents">if set to <c>true</c> [forward events].</param> /// <param name="connection">The connection.</param> /// <param name="transaction">The transaction.</param> /// <param name="lockToken">The lock token.</param> /// <exception cref="ArgumentNullException"> /// </exception> internal SQLiteTransactionalDataSource(SQLiteDataSource dataSource, bool forwardEvents, SQLiteConnection connection, SQLiteTransaction transaction, IDisposable lockToken) : base(new SQLiteDataSourceSettings() { DefaultCommandTimeout = dataSource.DefaultCommandTimeout, StrictMode = dataSource.StrictMode, SuppressGlobalEvents = dataSource.SuppressGlobalEvents || forwardEvents, DisableLocks = dataSource.DisableLocks }) { if (dataSource == null) { throw new ArgumentNullException(nameof(dataSource), $"{nameof(dataSource)} is null."); } if (connection == null) { throw new ArgumentNullException(nameof(connection), $"{nameof(connection)} is null."); } if (transaction == null) { throw new ArgumentNullException(nameof(transaction), $"{nameof(transaction)} is null."); } if (lockToken == null) { throw new ArgumentNullException(nameof(lockToken), $"{nameof(lockToken)} is null."); } Name = dataSource.Name; m_BaseDataSource = dataSource; m_Connection = connection; m_Transaction = transaction; m_LockToken = lockToken; if (forwardEvents) { ExecutionStarted += (sender, e) => dataSource.OnExecutionStarted(e); ExecutionFinished += (sender, e) => dataSource.OnExecutionFinished(e); ExecutionError += (sender, e) => dataSource.OnExecutionError(e); ExecutionCanceled += (sender, e) => dataSource.OnExecutionCanceled(e); } AuditRules = dataSource.AuditRules; UserValue = dataSource.UserValue; }
/// <summary> /// Creates an open data source using the supplied connection and optional transaction. /// </summary> /// <param name="connection">The connection.</param> /// <param name="transaction">The transaction.</param> /// <returns>SQLiteOpenDataSource.</returns> public SQLiteOpenDataSource CreateOpenDataSource(SQLiteConnection connection, SQLiteTransaction transaction = null) { return(new SQLiteOpenDataSource(this, connection, transaction)); }
/// <summary> /// Returns a data source wrapped around the transaction. /// </summary> /// <param name="connection">The connection.</param> /// <param name="transaction">The transaction.</param> /// <returns>SqlServerOpenDataSource.</returns> /// <exception cref="ArgumentNullException"></exception> public static SQLiteOpenDataSource AsDataSource(this SQLiteConnection connection, SQLiteTransaction transaction) { if (connection == null) { throw new ArgumentNullException(nameof(connection), $"{nameof(connection)} is null."); } if (connection.State == ConnectionState.Closed) { connection.Open(); } var dataSourceBase = s_CachedDataSources.GetOrAdd(connection.ConnectionString, cs => new SQLiteDataSource(cs)); return(new SQLiteOpenDataSource(dataSourceBase, connection, transaction)); }