Exemplo n.º 1
0
        /// <summary>
        /// Initializes the shared connection.
        /// </summary>
        /// <param name="sharedConnectionString">The shared connection string.</param>
        /// <returns></returns>
        public DbConnection InitializeSharedConnection(string sharedConnectionString)
        {
            var reuse = CurrentSharedConnection != null;

            try
            {
                if (CurrentSharedConnection == null)
                {
                    CurrentSharedConnection = CreateConnection(sharedConnectionString);
                }
                if (CurrentSharedConnection.State == ConnectionState.Closed)
                {
                    CurrentSharedConnection.Open();
                }
            }
            catch (Exception e)
            {
                if (CurrentSharedConnection == null)
                {
                    throw new Exception("Error initializing shared connection. CurrentSharedConnection is null.", e);
                }
                throw new Exception(string.Format("Error initializing shared connection. Connection state: [{0}], Reusing Connection: [{1}]", CurrentSharedConnection.State, reuse), e);
                //throw new Exception(string.Format("Error initializing shared connection. Connection state: [{0}], Connection string: [{1}], Reusing Connection: [{2}]", CurrentSharedConnection.State, CurrentSharedConnection.ConnectionString, reuse), e);
            }
            return(CurrentSharedConnection);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Resets the shared connection.
 /// </summary>
 public void ResetSharedConnection()
 {
     try
     {
         if (CurrentSharedConnection != null && CurrentSharedConnection.State != ConnectionState.Closed)
         {
             CurrentSharedConnection.Close();
         }
     }
     catch (Exception)
     {
         // intentionally ignoring exceptions.
     }
     try
     {
         if (CurrentSharedConnection != null)
         {
             CurrentSharedConnection.Dispose();
         }
     }
     finally
     {
         CurrentSharedConnection = null;
     }
     try
     {
         if (CurrentSharedTransaction != null)
         {
             CurrentSharedTransaction.Dispose();
         }
     }
     finally
     {
         CurrentSharedTransaction = null;
     }
 }