public void build_from_connection() { var connection = new NpgsqlConnection(ConnectionSource.ConnectionString); var options = SessionOptions.ForConnection(connection); options.Connection.ShouldBe(connection); options.Timeout.Value.ShouldBe(connection.CommandTimeout); options.OwnsConnection.ShouldBeFalse(); options.OwnsTransactionLifecycle.ShouldBeTrue(); }
public async Task pass_in_current_connection() { var newTargets = Target.GenerateRandomData(5).ToArray(); await using var conn = new NpgsqlConnection(ConnectionSource.ConnectionString); await using var session = theStore.OpenSession(SessionOptions.ForConnection(conn)); session.Store(newTargets); await session.SaveChangesAsync(); }
public async Task build_from_open_connection() { using var connection = new NpgsqlConnection(ConnectionSource.ConnectionString); await connection.OpenAsync(); var options = SessionOptions.ForConnection(connection); options.Connection.ShouldBe(connection); options.Timeout.Value.ShouldBe(connection.CommandTimeout); options.OwnsConnection.ShouldBeFalse(); // if the connection is closed options.OwnsTransactionLifecycle.ShouldBeTrue(); }
public void samples(IDocumentStore store, NpgsqlConnection connection, NpgsqlTransaction transaction) { // Use an existing connection, but Marten still controls the transaction lifecycle var session1 = store.OpenSession(SessionOptions.ForConnection(connection)); // Enlist in an existing Npgsql transaction, but // choose not to allow the session to own the transaction // boundaries var session3 = store.OpenSession(SessionOptions.ForTransaction(transaction)); // Enlist in the current, ambient transaction scope using (var scope = new TransactionScope()) { var session4 = store.OpenSession(SessionOptions.ForCurrentTransaction()); } }
public async Task do_not_blow_up() { StoreOptions(opts => opts.RegisterDocumentType <Target>()); await theStore.Storage.ApplyAllConfiguredChangesToDatabaseAsync(); var transaction = new TransactionScope( TransactionScopeOption.Required, new TransactionOptions { }, TransactionScopeAsyncFlowOption.Enabled); using (transaction) { /* READ * We open the connection here to ensure it enlists in the transaction scope. * If an error occurs, that means the connection is already open and the calling * code probably has a bug or something, or perhaps the code is sending through * the mediator more than once. If the latter is the case, we need to ask why. * If there is a valid reason for it, then perhaps this code needs to be re-worked * a bit to allow for activities to be sent through the mediator more than once, * but that should be a rare exception. * */ var connection = new NpgsqlConnection(ConnectionSource.ConnectionString); connection.Open(); var options = SessionOptions.ForConnection(connection).EnlistInAmbientTransactionScope(); options.IsolationLevel = IsolationLevel.ReadCommitted; options.Tracking = DocumentTracking.None; var martenConnection = await options.InitializeAsync(theStore, CommandRunnerMode.External, CancellationToken.None); var lifetime = martenConnection.ShouldBeOfType <AmbientTransactionLifetime>(); lifetime.OwnsConnection.ShouldBeFalse(); lifetime.Connection.ShouldBe(connection); using var session = theStore.OpenSession(options); session.Store(Target.Random()); await session.SaveChangesAsync(); transaction.Complete(); } }