コード例 #1
0
        /// <summary>
        /// Create connection at handle set transaction if necessary
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        protected static ConnectionHolder CreateConnection(string connectionString)
        {
            var connection = new SqlConnection(connectionString);

            connection.Open();

            if (Transaction.Current == null)
            {
                var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
                return(ConnectionHolder.ForTransactionalWork(connection, transaction));
            }
            return(ConnectionHolder.ForNonTransactionalWork(connection));
        }
コード例 #2
0
ファイル: SqlServerStorage.cs プロジェクト: mgibas/Rebus
        /// <summary>
        /// Create connection at handle set transaction if necessary
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        protected static ConnectionHolder CreateConnection(string connectionStringOrConnectionStringName)
        {
            var connectionStringToUse = Rebus.Shared.ConnectionStringUtil.GetConnectionStringToUse(connectionStringOrConnectionStringName);
            var connection            = new SqlConnection(connectionStringToUse);

            connection.Open();

            if (Transaction.Current == null)
            {
                var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
                return(ConnectionHolder.ForTransactionalWork(connection, transaction));
            }
            return(ConnectionHolder.ForNonTransactionalWork(connection));
        }
        ConnectionHolder GetOrCreateConnection()
        {
            if (currentConnection != null)
            {
                return(currentTransaction == null
                    ? ConnectionHolder.ForNonTransactionalWork(currentConnection)
                    : ConnectionHolder.ForTransactionalWork(currentConnection, currentTransaction));
            }

            var newConnection = new SqlConnection(ConnectionStrings.SqlServer);

            newConnection.Open();
            currentConnection = newConnection;
            return(ConnectionHolder.ForNonTransactionalWork(newConnection));
        }
コード例 #4
0
        public void CanPublishWithinTransactionScopeWhenProvidingTransactionLessConnectionHolder()
        {
            // arrange
            var subscriptionStorage = new SqlServerSubscriptionStorage(() =>
            {
                var sqlConnection = new SqlConnection(ConnectionString);
                sqlConnection.Open();
                return(ConnectionHolder.ForNonTransactionalWork(sqlConnection));
            }, SubscriptionsTableName);

            subscriptionStorage.EnsureTableIsCreated();

            var publisher = CreateBus(PublisherInputQueueName, subscriptionStorage);

            var subReceivedEvents = new List <int>();

            var sub = CreateBus(SubscriberInputQueueName, subscriptionStorage)
                      .Handle <SomeEvent>(e => subReceivedEvents.Add(e.EventNumber));

            sub.Bus.Subscribe <SomeEvent>();

            // act
            Thread.Sleep(1.Seconds());

            using (var scope = new TransactionScope())
            {
                publisher.Bus.Publish(new SomeEvent {
                    EventNumber = 1
                });

                scope.Complete();
            }

            Thread.Sleep(1.Seconds());

            // assert
            subReceivedEvents.ShouldBe(new[] { 1 }.ToList());
        }