public SqlTransaction2(SqlConnection2 conn, MySqlTransaction tran, TimeSpan timeout)
 {
     Conn        = conn;
     Transaction = tran;
     RunTime     = DateTime.Now;
     Timeout     = timeout;
 }
Exemple #2
0
        public SqlConnection2 GetConnection()
        {
            SqlConnection2 conn = null;
            int            tid  = Thread.CurrentThread.ManagedThreadId;

            if (FreeConnections.Count > 0)
            {
                lock (_lock)
                    conn = FreeConnections.Dequeue();
            }
            if (conn == null && AllConnections.Count < MaxPoolSize)
            {
                conn = new SqlConnection2 {
                    ThreadId      = tid,
                    SqlConnection = new MySqlConnection(ConnectionString)
                };
                lock (_lock)
                    AllConnections.Add(conn);
            }
            if (conn == null)
            {
                ManualResetEvent wait = new ManualResetEvent(false);
                lock (_lock_GetConnectionQueue)
                    GetConnectionQueue.Enqueue(wait);
                if (wait.WaitOne(TimeSpan.FromSeconds(10)))
                {
                    return(GetConnection());
                }
                return(null);
            }
            conn.ThreadId   = tid;
            conn.LastActive = DateTime.Now;
            Interlocked.Increment(ref conn.UseSum);
            return(conn);
        }
Exemple #3
0
        public void ReleaseConnection(SqlConnection2 conn)
        {
            conn.SqlConnection.Close();
            lock (_lock)
                FreeConnections.Enqueue(conn);

            if (GetConnectionQueue.Count > 0)
            {
                ManualResetEvent wait = null;
                lock (_lock_GetConnectionQueue)
                    wait = GetConnectionQueue.Dequeue();
                if (wait != null)
                {
                    wait.Set();
                }
            }
        }
        private PrepareCommandReturnInfo PrepareCommand(MySqlCommand cmd, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms)
        {
            cmd.CommandType = cmdType;
            cmd.CommandText = cmdText;

            if (cmdParms != null)
            {
                foreach (MySqlParameter parm in cmdParms)
                {
                    if (parm == null)
                    {
                        continue;
                    }
                    if (parm.Value == null)
                    {
                        parm.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(parm);
                }
            }

            SqlConnection2   conn = null;
            MySqlTransaction tran = CurrentThreadTransaction;

            if (tran == null)
            {
                conn           = this.Pool.GetConnection();
                cmd.Connection = conn.SqlConnection;
            }
            else
            {
                cmd.Connection  = tran.Connection;
                cmd.Transaction = tran;
            }

            AutoCommitTransaction();
            return(new PrepareCommandReturnInfo {
                Conn = conn, Tran = tran
            });
        }