public void enlist_in_transaction_scope()
        {
            using (var scope = new TransactionScope())
            {
                using (var session = theStore.OpenSession(SessionOptions.ForCurrentTransaction()))
                {
                    session.Store(Target.Random(), Target.Random());
                    session.SaveChanges();
                }

                // should not yet be committed
                using (var session = theStore.QuerySession())
                {
                    //See https://github.com/npgsql/npgsql/issues/1483 - Npgsql by default is enlisting
                    session.Query <Target>().Count().ShouldBe(102);
                }

                scope.Complete();
            }

            // should be 2 additional targets
            using (var session = theStore.QuerySession())
            {
                session.Query <Target>().Count().ShouldBe(102);
            }
        }
Esempio n. 2
0
        public void enlist_in_transaction_scope()
        {
            using (var scope = new TransactionScope())
            {
                using (var session = theStore.OpenSession(SessionOptions.ForCurrentTransaction()))
                {
                    session.Store(Target.Random(), Target.Random());
                    session.SaveChanges();
                }

                // should not yet be committed
                using (var session = theStore.QuerySession())
                {
                    session.Query <Target>().Count().ShouldBe(100);
                }

                scope.Complete();
            }

            // should be 2 additional targets
            using (var session = theStore.QuerySession())
            {
                session.Query <Target>().Count().ShouldBe(102);
            }
        }
Esempio n. 3
0
        static async Task Main(string[] args)
        {
            showProcessData();


            using var store = DocumentStore.For(opts =>
            {
                opts.Connection(ConnectionSource.ConnectionString);
                opts.AutoCreateSchemaObjects = AutoCreate.All;
            });

            showProcessData();


            var parentItem = new Parent
            {
                Children = new List <Child>
                {
                    new Child()
                }
            };



            while (true)
            {
                // using (var session = store.OpenSession())
                // {
                //     session.Store(new Target());
                //     session.Store(new Target());
                //     session.Store(new Target());
                //     session.Store(new Target());
                //
                //     session.Events.StartStream(new AEvent(), new BEvent(), new CEvent());
                //
                //     await session.SaveChangesAsync();
                // }

                using (var session = store.OpenSession(SessionOptions.ForCurrentTransaction()))
                {
                    session.Store(parentItem);
                    await session.SaveChangesAsync();
                }

                showProcessData();

                using (var session = store.OpenSession(SessionOptions.ForCurrentTransaction()))
                {
                    await session.Query <Parent>().FirstOrDefaultAsync(p => p.Children.Any(c => c.Id == Guid.Empty));
                }

                showProcessData();

                GC.Collect();

                await Task.Delay(1.Seconds());
            }
        }
        public async Task AppendAsync(TType aggregateRoot)
        {
            _logger.LogInformation("EventsRepository {id}", aggregateRoot.Id);

            using var session = _store.OpenSession(SessionOptions.ForCurrentTransaction());

            var aggregateRootId  = aggregateRoot.Id;
            var aggregateVersion = (int)aggregateRoot.Version;
            var events           = aggregateRoot.Events;

            session.Events.StartStream <TType>(aggregateRootId, events);

            await session.SaveChangesAsync();
        }
Esempio n. 5
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. 6
0
        public void do_not_blow_up_with_too_many_open_connections()
        {
            for (int i = 0; i < 1000; i++)
            {
                // this reaches 200, than crashes

                using (var scope = new TransactionScope())
                {
                    using (var session = theStore.OpenSession(SessionOptions.ForCurrentTransaction()))
                    {
                        session.Store(new EntityToSave());
                        session.SaveChanges();
                    }

                    scope.Complete();
                }
            }

            using (var session = theStore.QuerySession())
            {
                session.Query <EntityToSave>().Count().ShouldBe(1000);
            }
        }
Esempio n. 7
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
                });
            }
        }
Esempio n. 8
0
    public static IDocumentSession CreateDocumentSession(DocumentStore store)
    {
        var session = store.OpenSession(SessionOptions.ForCurrentTransaction());

        return(session);
    }