public void transaction_controller_implicitly_opens_the_connection()
        {
            using (var ctx = new SqlTransactionCallContext(TestHelper.Monitor))
            {
                var controller = ctx.GetConnectionController(TestHelper.GetConnectionString());
                BeginTranAndCommit(controller);
                controller.Connection.State.Should().Be(ConnectionState.Closed);

                // Explicit openening.
                using (controller.ExplicitOpen())
                {
                    BeginTranAndCommit(controller);
                    controller.Connection.State.Should().Be(ConnectionState.Open);
                }
                controller.Connection.State.Should().Be(ConnectionState.Closed);
            }

            void BeginTranAndCommit(ISqlConnectionTransactionController controller)
            {
                ISqlTransaction tran = controller.BeginTransaction();

                controller.Connection.State.Should().Be(ConnectionState.Open);
                tran.IsNested.Should().BeFalse();
                tran.Status.Should().Be(SqlTransactionStatus.Opened);
                tran.Commit();
                tran.Status.Should().Be(SqlTransactionStatus.Committed);
                tran.Invoking(t => t.Dispose()).Should().NotThrow();
            }
        }
예제 #2
0
        bool RegistrateCrawlerId(IDbConnection connection)
        {
            ISqlTransaction transaction = connection.BeginTransaction();

            string selectQuery = "SELECT COUNT(*) FROM crawler_id WHERE mac_address='" + Helpers.GetMacAddress() + "'";

            List <object>[] data = connection.ExecuteReadQuery(selectQuery);

            bool hasNoCrawlersWithThisMac = data.Length != 0 && data[0].Count > 0 && (long)data[0][0] == 0;

            if (hasNoCrawlersWithThisMac)
            {
                connection.ExecuteNonQuery("INSERT INTO crawler_id(mac_address) VALUES('" + Helpers.GetMacAddress() + "')");
                data = connection.ExecuteReadQuery(selectQuery);

                bool hasOnlyOneCrawlerWithThisMac = data.Length != 0 && data[0].Count > 0 && (long)data[0][0] == 1;

                if (!hasOnlyOneCrawlerWithThisMac)
                {
                    transaction.Rollback();
                    return(false);
                }

                transaction.Commit();
                return(true);
            }

            transaction.Rollback();
            return(false);
        }