public SqlTransaction2(Connection2 conn, NpgsqlTransaction tran, TimeSpan timeout) { Conn = conn; Transaction = tran; RunTime = DateTime.Now; Timeout = timeout; }
private PrepareCommandReturnInfo PrepareCommand(NpgsqlCommand cmd, CommandType cmdType, string cmdText, NpgsqlParameter[] cmdParms, ref string logtxt) { DateTime dt = DateTime.Now; cmd.CommandType = cmdType; cmd.CommandText = cmdText; if (cmdParms != null) { foreach (NpgsqlParameter parm in cmdParms) { if (parm == null) { continue; } if (parm.Value == null) { parm.Value = DBNull.Value; } cmd.Parameters.Add(parm); } } Connection2 conn = null; NpgsqlTransaction tran = CurrentThreadTransaction; logtxt += $" PrepareCommand_part1: {DateTime.Now.Subtract(dt).TotalMilliseconds}ms cmdParms: {cmdParms.Length}\r\n"; if (tran == null) { dt = DateTime.Now; conn = this.Pool.GetConnection(); cmd.Connection = conn.SqlConnection; logtxt += $" PrepareCommand_tran==null: {DateTime.Now.Subtract(dt).TotalMilliseconds}ms\r\n"; } else { dt = DateTime.Now; cmd.Connection = tran.Connection; cmd.Transaction = tran; logtxt += $" PrepareCommand_tran!=null: {DateTime.Now.Subtract(dt).TotalMilliseconds}ms\r\n"; } dt = DateTime.Now; AutoCommitTransaction(); logtxt += $" AutoCommitTransaction: {DateTime.Now.Subtract(dt).TotalMilliseconds}ms\r\n"; return(new PrepareCommandReturnInfo { Conn = conn, Tran = tran }); }
public Connection2 GetConnection() { Connection2 conn = null; if (FreeConnections.Count > 0) { lock (_lock) if (FreeConnections.Count > 0) { conn = FreeConnections.Dequeue(); } } if (conn == null && AllConnections.Count < MaxPoolSize) { lock (_lock) if (AllConnections.Count < MaxPoolSize) { conn = new Connection2(); AllConnections.Add(conn); } if (conn != null) { conn.ThreadId = Thread.CurrentThread.ManagedThreadId; conn.SqlConnection = new NpgsqlConnection(ConnectionString); } } 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 = Thread.CurrentThread.ManagedThreadId; conn.LastActive = DateTime.Now; Interlocked.Increment(ref conn.UseSum); return(conn); }
public void ReleaseConnection(Connection2 conn) { conn.SqlConnection.Close(); lock (_lock) FreeConnections.Enqueue(conn); if (GetConnectionQueue.Count > 0) { ManualResetEvent wait = null; lock (_lock_GetConnectionQueue) if (GetConnectionQueue.Count > 0) { wait = GetConnectionQueue.Dequeue(); } if (wait != null) { wait.Set(); } } }
private PrepareCommandReturnInfo PrepareCommand(NpgsqlCommand cmd, CommandType cmdType, string cmdText, NpgsqlParameter[] cmdParms) { cmd.CommandType = cmdType; cmd.CommandText = cmdText; if (cmdParms != null) { foreach (NpgsqlParameter parm in cmdParms) { if (parm == null) { continue; } if (parm.Value == null) { parm.Value = DBNull.Value; } cmd.Parameters.Add(parm); } } Connection2 conn = null; NpgsqlTransaction 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 }); }