Esempio n. 1
0
        public async Task pass_in_current_connection_and_transaction_with_externally_controlled_tx_boundaries_async()
        {
            var newTargets = Target.GenerateRandomData(5).ToArray();

            using (var conn = new NpgsqlConnection(ConnectionSource.ConnectionString))
            {
                await conn.OpenAsync();

                var tx = conn.BeginTransaction();

                var cmd = conn.CreateCommand("delete from mt_doc_target");
                cmd.Transaction = tx;
                await cmd.ExecuteNonQueryAsync();

                using (var session = theStore.OpenSession(SessionOptions.ForTransaction(tx)))
                {
                    session.Store(newTargets);
                    await session.SaveChangesAsync();
                }

                // To prove the isolation here
                using (var query = theStore.QuerySession())
                {
                    (await query.Query <Target>().CountAsync()).ShouldBe(100);
                }

                await tx.CommitAsync();
            }

            // All the old should be gone, then the new put back on top
            using (var query = theStore.QuerySession())
            {
                (await query.Query <Target>().CountAsync()).ShouldBe(5);
            }
        }
Esempio n. 2
0
        public void pass_in_current_connection_and_transaction()
        {
            var newTargets = Target.GenerateRandomData(5).ToArray();

            using (var conn = new NpgsqlConnection(ConnectionSource.ConnectionString))
            {
                conn.Open();
                var tx = conn.BeginTransaction();

                var cmd = conn.CreateCommand("delete from mt_doc_target");
                cmd.Transaction = tx;
                cmd.ExecuteNonQuery();

                // To prove the isolation here
                using (var query = theStore.QuerySession())
                {
                    query.Query <Target>().Count().ShouldBe(100);
                }

                using (var session = theStore.OpenSession(SessionOptions.ForTransaction(tx, true)))
                {
                    session.Store(newTargets);
                    session.SaveChanges();
                }
            }

            // All the old should be gone, then the new put back on top
            using (var query = theStore.QuerySession())
            {
                query.Query <Target>().Count().ShouldBe(5);
            }
        }
Esempio n. 3
0
        public async System.Threading.Tasks.Task for_transaction()
        {
            using var conn = new NpgsqlConnection(ConnectionSource.ConnectionString);
            await conn.OpenAsync().ConfigureAwait(false);

            using var tx = await conn.BeginTransactionAsync();

            var options = SessionOptions.ForTransaction(tx);

            options.Transaction.ShouldBe(tx);
            options.Connection.ShouldBe(conn);
            options.OwnsConnection.ShouldBeFalse();
            options.OwnsTransactionLifecycle.ShouldBeFalse();
        }
Esempio n. 4
0
        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());
            }
        }
Esempio n. 5
0
        // SAMPLE: passing-in-existing-connections-and-transactions
        public void samples(IDocumentStore store, NpgsqlConnection connection, NpgsqlTransaction transaction)
        {
            // Use an existing connection, but Marten still controls the transaction lifecycle
            var session1 = store.OpenSession(new SessionOptions
            {
                Connection = connection
            });

            // Enlist in an existing Npgsql transaction, but
            // choose not to allow the session to own the transaction
            // boundaries
            var session2 = store.OpenSession(new SessionOptions
            {
                Transaction = transaction,
                OwnsTransactionLifecycle = false
            });

            // This is syntactical sugar for the sample above
            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());
            }

            // or this is the long hand way of doing the options above
            using (var scope = new TransactionScope())
            {
                var session5 = store.OpenSession(new SessionOptions
                {
                    EnlistInAmbientTransactionScope = true,
                    OwnsTransactionLifecycle        = false
                });
            }
        }