Beispiel #1
0
        /// <summary>
        /// Closes the connection.
        /// </summary>
        /// <param name="name">The name.</param>
        public void CloseConnection(string name)
        {
            // retrieve connection
            var cnxn = GetConnection(name);

            if (cnxn == null)
            {
                return;
            }

            // check transaction
            var trx = DbTransactionRegistry.RetrieveTransaction(cnxn);

            if (trx != null)
            {
                trx.Rollback();
                DbTransactionRegistry.RemoveTransaction(cnxn);
            }

            // close connection
            if (cnxn.State == ConnectionState.Open)
            {
                cnxn.Close();
            }

            // remove connection
            Connections.Remove(name);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the transaction.
        /// </summary>
        /// <param name="level">The level.</param>
        /// <returns></returns>
        public TransactionContext GetTransaction(IsolationLevel level)
        {
            if (Transaction == null)
            {
                Transaction = new TransactionContext();
                foreach (DbConnection cnxn in Connections.Values)
                {
                    if (cnxn == null)
                    {
                        continue;
                    }
                    DbTransactionRegistry.RegisterNewTransaction(cnxn, level);
                }
            }

            return(Transaction);
        }
Beispiel #3
0
        /// <summary>
        /// Finishes the transaction.
        /// </summary>
        /// <param name="commit">if set to <c>true</c> [commit].</param>
        public void FinishTransaction(bool commit)
        {
            var trxn = Transaction;

            if (trxn == null)
            {
                return;
            }

            foreach (var cxPair in Connections)
            {
                var cnxn = cxPair.Value;
                if (cnxn == null)
                {
                    continue;
                }

                var dbTrx = DbTransactionRegistry.RetrieveTransaction(cnxn);
                if (dbTrx == null)
                {
                    continue;
                }

                if (cnxn.State == ConnectionState.Open)
                {
                    if (commit)
                    {
                        dbTrx.Commit();
                    }
                    else
                    {
                        dbTrx.Rollback();
                    }
                }

                DbTransactionRegistry.RemoveTransaction(cnxn);
            }

            Transaction = null;
        }
        /// <summary>
        /// Gets the connection.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public DbConnection GetConnection(string name)
        {
            // get context
            ConnectionManagerContext ctx = Context;

            // check if already open
            var cnxn = ctx.GetConnection(name);

            if ((cnxn != null) && (cnxn.State == ConnectionState.Open))
            {
                return(cnxn);
            }

            // prepare connection info
            cnxn = PerpareConnection(name);

            // open connection
            try
            {
                cnxn.Open();

                // enroll in transaction
                if (ctx.Transaction != null)
                {
                    DbTransactionRegistry.RegisterNewTransaction(cnxn);
                }

                SetConnection(name, cnxn);

                return(cnxn);
            }
            catch (Exception ex)
            {
                throw new DataException("Unable to connect to database", ex);
            }
        }