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; }
private void WantTransaction() { if (_tran == null) { _tran = _conn.BeginTransaction(); } }
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(); } } }
private void Initialize() { _logger.LogInformation("Initializing db cache: {ConnectionString}", _config.ConnectionString); using (var transaction = _db.BeginTransaction()) { using (var cmd = new DbCommand(Resources.TableInitCommand, _db)) { cmd.Transaction = transaction; cmd.ExecuteNonQuery(); } using (var cmd = new DbCommand( $"INSERT INTO meta (key, value) " + $"VALUES " + $@"(""version"", {SchemaVersion}), " + $@"(""created"", {DateTimeOffset.UtcNow.Ticks})", _db)) { cmd.Transaction = transaction; cmd.ExecuteNonQuery(); } transaction.Commit(); } }
public void BeginTransaction_works() { // Arrange and act. using (var connection = new Microsoft.Data.Sqlite.SqliteConnection("Data Source=:memory:")) { connection.Open(); using (var transaction = connection.BeginTransaction(System.Data.IsolationLevel.Serializable)) { // Assert. Assert.IsNotNull(transaction); Assert.AreEqual(connection, transaction.Connection); Assert.AreEqual(System.Data.IsolationLevel.Serializable, transaction.IsolationLevel); } // Assert. Assert.AreEqual(System.Data.ConnectionState.Open, connection.State); } }