diff --git a/FreeSql/MySql/MySqlAdo/MySqlConnectionPool.cs b/FreeSql/MySql/MySqlAdo/MySqlConnectionPool.cs index a0f69578..41355ae3 100644 --- a/FreeSql/MySql/MySqlAdo/MySqlConnectionPool.cs +++ b/FreeSql/MySql/MySqlAdo/MySqlConnectionPool.cs @@ -28,7 +28,7 @@ namespace FreeSql.MySql { public void Return(Object obj, Exception exception, bool isRecreate = false) { if (exception != null && exception is MySqlException) { - try { if ((obj.Value as MySqlConnection).Ping() == false) obj.Value.Open(); } catch { base.SetUnavailable(exception); } + try { if (obj.Value.Ping() == false) obj.Value.Open(); } catch { base.SetUnavailable(exception); } } base.Return(obj, isRecreate); } @@ -66,8 +66,8 @@ namespace FreeSql.MySql { } public bool OnCheckAvailable(Object obj) { - if ((obj.Value as MySqlConnection).Ping() == false) obj.Value.Open(); - return (obj.Value as MySqlConnection).Ping(); + if (obj.Value.State == ConnectionState.Closed) obj.Value.Open(); + return obj.Value.Ping(true); } public DbConnection OnCreate() { @@ -84,7 +84,7 @@ namespace FreeSql.MySql { if (_pool.IsAvailable) { - if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (obj.Value as MySqlConnection).Ping() == false) { + if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && obj.Value.Ping() == false) { try { obj.Value.Open(); @@ -100,7 +100,7 @@ namespace FreeSql.MySql { if (_pool.IsAvailable) { - if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (obj.Value as MySqlConnection).Ping() == false) { + if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (await obj.Value.PingAsync()) == false) { try { await obj.Value.OpenAsync(); @@ -128,4 +128,34 @@ namespace FreeSql.MySql { _pool.unavailableHandler?.Invoke(); } } + + static class DbConnectionExtensions { + + static DbCommand PingCommand(DbConnection conn) { + var cmd = conn.CreateCommand(); + cmd.CommandTimeout = 1; + cmd.CommandText = "select 1"; + return cmd; + } + public static bool Ping(this DbConnection that, bool isThrow = false) { + try { + PingCommand(that).ExecuteNonQuery(); + return true; + } catch { + if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; + return false; + } + } + async public static Task PingAsync(this DbConnection that, bool isThrow = false) { + try { + await PingCommand(that).ExecuteNonQueryAsync(); + return true; + } catch { + if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; + return false; + } + } + } } diff --git a/FreeSql/Oracle/OracleAdo/OracleConnectionPool.cs b/FreeSql/Oracle/OracleAdo/OracleConnectionPool.cs index 43ea23b0..e108a45a 100644 --- a/FreeSql/Oracle/OracleAdo/OracleConnectionPool.cs +++ b/FreeSql/Oracle/OracleAdo/OracleConnectionPool.cs @@ -82,10 +82,7 @@ namespace FreeSql.Oracle { public bool OnCheckAvailable(Object obj) { if (obj.Value.State == ConnectionState.Closed) obj.Value.Open(); - var cmd = obj.Value.CreateCommand(); - cmd.CommandText = "select 1"; - cmd.ExecuteNonQuery(); - return true; + return obj.Value.Ping(true); } public DbConnection OnCreate() { @@ -118,7 +115,7 @@ namespace FreeSql.Oracle { if (_pool.IsAvailable) { - if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && obj.Value.Ping() == false) { + if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (await obj.Value.PingAsync()) == false) { try { await obj.Value.OpenAsync(); @@ -149,14 +146,29 @@ namespace FreeSql.Oracle { static class DbConnectionExtensions { - public static bool Ping(this DbConnection that) { + static DbCommand PingCommand(DbConnection conn) { + var cmd = conn.CreateCommand(); + cmd.CommandTimeout = 1; + cmd.CommandText = "select 1 from dual"; + return cmd; + } + public static bool Ping(this DbConnection that, bool isThrow = false) { try { - var cmd = that.CreateCommand(); - cmd.CommandText = "select 1 from dual"; - cmd.ExecuteNonQuery(); + PingCommand(that).ExecuteNonQuery(); return true; } catch { - try { that.Close(); } catch { } + if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; + return false; + } + } + async public static Task PingAsync(this DbConnection that, bool isThrow = false) { + try { + await PingCommand(that).ExecuteNonQueryAsync(); + return true; + } catch { + if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; return false; } } diff --git a/FreeSql/PostgreSQL/PostgreSQLAdo/PostgreSQLConnectionPool.cs b/FreeSql/PostgreSQL/PostgreSQLAdo/PostgreSQLConnectionPool.cs index bd9978b0..3c8f842c 100644 --- a/FreeSql/PostgreSQL/PostgreSQLAdo/PostgreSQLConnectionPool.cs +++ b/FreeSql/PostgreSQL/PostgreSQLAdo/PostgreSQLConnectionPool.cs @@ -77,10 +77,7 @@ namespace FreeSql.PostgreSQL { public bool OnCheckAvailable(Object obj) { if (obj.Value.State == ConnectionState.Closed) obj.Value.Open(); - var cmd = obj.Value.CreateCommand(); - cmd.CommandText = "select 1"; - cmd.ExecuteNonQuery(); - return true; + return obj.Value.Ping(true); } public DbConnection OnCreate() { @@ -113,7 +110,7 @@ namespace FreeSql.PostgreSQL { if (_pool.IsAvailable) { - if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && obj.Value.Ping() == false) { + if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (await obj.Value.PingAsync()) == false) { try { await obj.Value.OpenAsync(); @@ -144,14 +141,29 @@ namespace FreeSql.PostgreSQL { static class DbConnectionExtensions { - public static bool Ping(this DbConnection that) { + static DbCommand PingCommand(DbConnection conn) { + var cmd = conn.CreateCommand(); + cmd.CommandTimeout = 1; + cmd.CommandText = "select 1"; + return cmd; + } + public static bool Ping(this DbConnection that, bool isThrow = false) { try { - var cmd = that.CreateCommand(); - cmd.CommandText = "select 1"; - cmd.ExecuteNonQuery(); + PingCommand(that).ExecuteNonQuery(); return true; } catch { - try { that.Close(); } catch { } + if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; + return false; + } + } + async public static Task PingAsync(this DbConnection that, bool isThrow = false) { + try { + await PingCommand(that).ExecuteNonQueryAsync(); + return true; + } catch { + if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; return false; } } diff --git a/FreeSql/SqlServer/SqlServerAdo/SqlServerConnectionPool.cs b/FreeSql/SqlServer/SqlServerAdo/SqlServerConnectionPool.cs index f49d191f..b53f258c 100644 --- a/FreeSql/SqlServer/SqlServerAdo/SqlServerConnectionPool.cs +++ b/FreeSql/SqlServer/SqlServerAdo/SqlServerConnectionPool.cs @@ -71,10 +71,7 @@ namespace FreeSql.SqlServer { public bool OnCheckAvailable(Object obj) { if (obj.Value.State == ConnectionState.Closed) obj.Value.Open(); - var cmd = obj.Value.CreateCommand(); - cmd.CommandText = "select 1"; - cmd.ExecuteNonQuery(); - return true; + return obj.Value.Ping(true); } public DbConnection OnCreate() { @@ -107,7 +104,7 @@ namespace FreeSql.SqlServer { if (_pool.IsAvailable) { - if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && obj.Value.Ping() == false) { + if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (await obj.Value.PingAsync()) == false) { try { await obj.Value.OpenAsync(); @@ -136,16 +133,31 @@ namespace FreeSql.SqlServer { } } - static class SqlServerConnectionExtensions { + static class DbConnectionExtensions { - public static bool Ping(this DbConnection that) { + static DbCommand PingCommand(DbConnection conn) { + var cmd = conn.CreateCommand(); + cmd.CommandTimeout = 1; + cmd.CommandText = "select 1"; + return cmd; + } + public static bool Ping(this DbConnection that, bool isThrow = false) { try { - var cmd = that.CreateCommand(); - cmd.CommandText = "select 1"; - cmd.ExecuteNonQuery(); + PingCommand(that).ExecuteNonQuery(); return true; } catch { if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; + return false; + } + } + async public static Task PingAsync(this DbConnection that, bool isThrow = false) { + try { + await PingCommand(that).ExecuteNonQueryAsync(); + return true; + } catch { + if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; return false; } } diff --git a/FreeSql/Sqlite/SqliteAdo/SqliteConnectionPool.cs b/FreeSql/Sqlite/SqliteAdo/SqliteConnectionPool.cs index 1612fbfb..da0c9514 100644 --- a/FreeSql/Sqlite/SqliteAdo/SqliteConnectionPool.cs +++ b/FreeSql/Sqlite/SqliteAdo/SqliteConnectionPool.cs @@ -31,7 +31,7 @@ namespace FreeSql.Sqlite { public void Return(Object obj, Exception exception, bool isRecreate = false) { if (exception != null && exception is SQLiteException) { - try { if ((obj.Value as SQLiteConnection).Ping() == false) obj.Value.OpenAndAttach(policy.Attaches); } catch { base.SetUnavailable(exception); } + try { if (obj.Value.Ping() == false) obj.Value.OpenAndAttach(policy.Attaches); } catch { base.SetUnavailable(exception); } } base.Return(obj, isRecreate); } @@ -78,8 +78,8 @@ namespace FreeSql.Sqlite { } public bool OnCheckAvailable(Object obj) { - if ((obj.Value as SQLiteConnection).Ping() == false) obj.Value.OpenAndAttach(Attaches); - return (obj.Value as SQLiteConnection).Ping(); + if (obj.Value.State == ConnectionState.Closed) obj.Value.OpenAndAttach(Attaches); + return obj.Value.Ping(true); } public DbConnection OnCreate() { @@ -96,7 +96,7 @@ namespace FreeSql.Sqlite { if (_pool.IsAvailable) { - if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (obj.Value as SQLiteConnection).Ping() == false) { + if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && obj.Value.Ping() == false) { try { obj.Value.OpenAndAttach(Attaches); @@ -112,7 +112,7 @@ namespace FreeSql.Sqlite { if (_pool.IsAvailable) { - if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (obj.Value as SQLiteConnection).Ping() == false) { + if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (await obj.Value.PingAsync()) == false) { try { await obj.Value.OpenAndAttachAsync(Attaches); @@ -140,16 +140,31 @@ namespace FreeSql.Sqlite { _pool.unavailableHandler?.Invoke(); } } - static class SqliteConnectionExtensions { + static class DbConnectionExtensions { - public static bool Ping(this DbConnection that) { + static DbCommand PingCommand(DbConnection conn) { + var cmd = conn.CreateCommand(); + cmd.CommandTimeout = 1; + cmd.CommandText = "select 1"; + return cmd; + } + public static bool Ping(this DbConnection that, bool isThrow = false) { try { - var cmd = that.CreateCommand(); - cmd.CommandText = "select 1"; - cmd.ExecuteNonQuery(); + PingCommand(that).ExecuteNonQuery(); return true; } catch { if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; + return false; + } + } + async public static Task PingAsync(this DbConnection that, bool isThrow = false) { + try { + await PingCommand(that).ExecuteNonQueryAsync(); + return true; + } catch { + if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } + if (isThrow) throw; return false; } }