public static async Task <bool> SetAccountInfo(string userId, string password) { var conn = baseConn.Clone(); try { NpgsqlCommand cmd = new NpgsqlCommand(Query_SetAccountInfo, conn); cmd.Parameters.Add("id", NpgsqlTypes.NpgsqlDbType.Varchar, 16); cmd.Parameters[0].Value = userId; cmd.Parameters.Add("password", NpgsqlTypes.NpgsqlDbType.Text); cmd.Parameters[1].Value = password; cmd.Parameters.Add("claims", NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Varchar); cmd.Parameters[2].Value = userIdentity; await conn.OpenAsync(); int rowAffected = await cmd.ExecuteNonQueryAsync(); conn.Close(); return(rowAffected > 0); } catch { conn.Close(); return(false); } }
private ArrayList GetPrimaryKeys(String tablename) { if (tablename == String.Empty) { return(null); } String getPKColumns = "select a.attname from pg_catalog.pg_class ct, pg_catalog.pg_class ci, pg_catalog.pg_attribute a, pg_catalog.pg_index i WHERE ct.oid=i.indrelid AND ci.oid=i.indexrelid AND a.attrelid=ci.oid AND i.indisprimary AND ct.relname = :tablename"; ArrayList result = new ArrayList(); NpgsqlConnection metadataConn = _connection.Clone(); NpgsqlCommand c = new NpgsqlCommand(getPKColumns, metadataConn); c.Parameters.Add(new NpgsqlParameter("tablename", NpgsqlDbType.Text)); c.Parameters["tablename"].Value = tablename; NpgsqlDataReader dr = c.ExecuteReader(); while (dr.Read()) { result.Add(dr[0]); } metadataConn.Close(); return(result); }
/// <summary> /// Attempts to ensure, on a best-effort basis, that there are enough connections to meet MinPoolSize. /// This method never throws an exception. /// </summary> void EnsureMinPoolSize(NpgsqlConnection conn) { int missing; lock (this) { missing = _min - (Busy + Idle.Count); if (missing <= 0) { return; } Busy += missing; } for (; missing > 0; missing--) { try { #if NETSTANDARD1_3 var connector = new NpgsqlConnector(conn.Clone()) #else var connector = new NpgsqlConnector((NpgsqlConnection)((ICloneable)conn).Clone()) #endif { ClearCounter = _clearCounter }; // TODO: Think about the timeout here... connector.Open(new NpgsqlTimeout(TimeSpan.Zero), false, CancellationToken.None).Wait(); connector.Reset(); Counters.NumberOfPooledConnections.Increment(); lock (this) { Idle.Push(connector); EnsurePruningTimerState(); Busy--; } } catch (Exception e) { lock (this) Busy -= missing; Log.Warn("Connection error while attempting to ensure MinPoolSize", e); return; } } }
/// <summary> /// Attempts to ensure, on a best-effort basis, that there are enough connections to meet MinPoolSize. /// This method never throws an exception. /// </summary> void EnsureMinPoolSize(NpgsqlConnection conn) { int missing; lock (this) { missing = Min - (Busy + Idle.Count); if (missing <= 0) { return; } Busy += missing; } for (; missing > 0; missing--) { try { #if NET451 || NET45 var connector = new NpgsqlConnector((NpgsqlConnection)((ICloneable)conn).Clone()) #else var connector = new NpgsqlConnector(conn.Clone()) #endif { ClearCounter = _clearCounter }; connector.Open(); connector.Reset(); lock (this) { Idle.Push(connector); EnsurePruningTimerState(); Busy--; } } catch (Exception e) { lock (this) Busy -= missing; Log.Warn("Connection error while attempting to ensure MinPoolSize", e); return; } } }
public void Clone() { var connString = new NpgsqlConnectionStringBuilder(ConnectionString) { Pooling = false }; using (var conn = new NpgsqlConnection(connString)) { ProvideClientCertificatesCallback callback1 = certificates => { }; conn.ProvideClientCertificatesCallback = callback1; RemoteCertificateValidationCallback callback2 = (sender, certificate, chain, errors) => true; conn.UserCertificateValidationCallback = callback2; conn.Open(); #if NET451 using (var conn2 = (NpgsqlConnection)((ICloneable)conn).Clone()) #else using (var conn2 = conn.Clone()) #endif { Assert.That(conn2.ConnectionString, Is.EqualTo(conn.ConnectionString)); Assert.That(conn2.ProvideClientCertificatesCallback, Is.SameAs(callback1)); Assert.That(conn2.UserCertificateValidationCallback, Is.SameAs(callback2)); conn2.Open(); } } }