public void Rollback_ShouldDisposeTransactionConnection()
        {
            int disposeCount = 0;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                using (DatabaseConnectionWrapper wrapper = TransactionScopeConnections.GetConnection(db))
                {
                    Assert.IsNotNull(wrapper.Connection);
                    wrapper.Connection.Disposed += (sender, e) => { disposeCount++; };
                }
            }
            for (int i = 0; i < 10 && disposeCount == 0; i++)
            {
                Thread.Sleep(20); // Oracle doesn't dispose right away because of DTC
            }
            Assert.AreEqual(1, disposeCount);
        }
Exemplo n.º 2
0
        public void Rollback_ShouldDisposeTransactionConnection()
        {
            int disposeCount = 0;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                DbConnection connection = TransactionScopeConnections.GetConnection(db);
                connection.Disposed += delegate(object sender,
                                                EventArgs e)
                {
                    disposeCount++;
                };
                Assert.IsNotNull(connection);
            }
            for (int i = 0; i < 10 && disposeCount == 0; i++)
            {
                Thread.Sleep(20); // Oracle doesn't dispose right away because of DTC
            }
            Assert.AreEqual(1, disposeCount);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 현재 Transaction에 해당하는 Connection을 반환한다.
        /// 이미 열려진 Connection을 재활용하거나, TransactionScope가 활성화되어 있다면 새로운 connection을 빌드해서 반환한다.
        /// TransactionScope도 없고, <see cref="Database"/>관련해서 아직 만들어진게 없다면, 새로운 <see cref="DbConnection"/>을 생성하고, Open() 해서 반환한다.
        /// </summary>
        /// <param name="db">instance of <see cref="Database"/></param>
        /// <param name="newConnectionCreated">새로 Connection이 만들어졌는지</param>
        /// <param name="connectionFactory">Connection을 새로 만드는 메소드</param>
        /// <returns>
        /// TransactionScope이 활성화되어 있다면, TransactionScope에서 활성화된 <see cref="DbConnection"/>을 반환,
        /// TransactionScope가 없다면 새로운 <see cref="DbConnection"/>을 생성하여 반환한다.</returns>
        public static DbConnection CreateTransactionScopeConnection(Database db, ref bool newConnectionCreated,
                                                                    Func <Database, DbConnection> connectionFactory = null)
        {
            db.ShouldNotBeNull("db");
            connectionFactory = connectionFactory ?? ((database) => OpenConnection(database));

            // newConnectionCreated = false;
            DbConnection connection;

            // TransactionScope에 참여한 Connection이 있다면 그 Connection을 반환합니다.
            try {
                connection = TransactionScopeConnections.GetConnection(db);
            }
            catch (Exception ex) {
                if (log.IsWarnEnabled)
                {
                    log.WarnException("TransactionScopeConnections.GetConnection() 수행 시에 예외가 발생했습니다. 무시하고 새로운 connection을 생성합니다.", ex);
                }

                connection = null;
            }

            // 새롭게 Connection을 빌드합니다.
            if (connection == null)
            {
                if (IsDebugEnabled)
                {
                    log.Debug("TransactionScope에 속한 connection이 없습니다. 새로운 connection을 생성하고, Open합니다...");
                }

                connection           = connectionFactory(db);
                newConnectionCreated = (connection != null);

                if (IsDebugEnabled)
                {
                    log.Debug("새로운 connection을 생성하고, Open했습니다. newConnectionCreated=[{0}]", newConnectionCreated);
                }
            }

            return(connection);
        }
Exemplo n.º 4
0
        public void Commit_ShouldDisposeTransactionConnection()
        {
            int disposeCount = 0;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                using (DatabaseConnectionWrapper wrapper = TransactionScopeConnections.GetConnection(db))
                {
                    DbConnection connection = wrapper.Connection;
                    connection.Disposed += (sender, e) => { disposeCount++; };

                    Assert.IsNotNull(connection);
                    scope.Complete();
                }
            }
            //for (int i = 0; i < 10 && disposeCount == 0; i++)
            //{
            //    Thread.Sleep(20);						// Oracle doesn't dispose right away because of DTC
            //}
            Assert.AreEqual(1, disposeCount);
        }
Exemplo n.º 5
0
        public void GetConnection_ShouldReturnDifferentConnectionForDifferentConnectionStrings()
        {
            Database db2 = new SqlDatabase(db.ConnectionString.ToString() + ";Persist Security Info=false;");

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    DatabaseConnectionWrapper connection1 = TransactionScopeConnections.GetConnection(db);
                    DatabaseConnectionWrapper connection2 = TransactionScopeConnections.GetConnection(db2);
                    Assert.AreNotSame(connection1, connection2);
                }
            }
            catch (SqlException ex)
            {
                if (ex.Message.Contains("MSDTC"))
                {
                    Assert.Inconclusive("In order to run the test, enable the Distributed Transaction Coordinator service (MSDTC).\r\n{0}", ex.ToString());
                }

                throw;
            }
        }
Exemplo n.º 6
0
 public void GetConnection_ShouldReturnNullWhenNoTransactionActive()
 {
     Assert.IsNull(TransactionScopeConnections.GetConnection(db));
 }
Exemplo n.º 7
0
 private static ConnectionWrapper <DbConnection> GetOpenConnection(string connectionString, IDbFactory factory,
                                                                   bool disposeInnerConnection)
 {
     return(TransactionScopeConnections.GetOpenConnection(connectionString, () => factory.CreateConnection(), disposeInnerConnection));
 }
Exemplo n.º 8
0
        /// <summary>
        ///		Gets a "wrapped" connection that will be not be disposed if a transaction is
        ///		active (created by creating a TransactionScope instance). The
        ///		connection will be disposed when no transaction is active.
        /// </summary>
        /// <returns>Database connection wrapper.</returns>
        protected DatabaseConnectionWrapper GetOpenConnection()
        {
            DatabaseConnectionWrapper connection = TransactionScopeConnections.GetConnection(this);

            return(connection ?? this.GetWrappedConnection());
        }