- 修改 连接池内部 Ping Timeout 值暂定 1秒;

This commit is contained in:
28810 2019-03-08 10:51:13 +08:00
parent 167c64c7f1
commit eb6244e0ba
5 changed files with 126 additions and 45 deletions

View File

@ -28,7 +28,7 @@ namespace FreeSql.MySql {
public void Return(Object<DbConnection> obj, Exception exception, bool isRecreate = false) { public void Return(Object<DbConnection> obj, Exception exception, bool isRecreate = false) {
if (exception != null && exception is MySqlException) { 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); base.Return(obj, isRecreate);
} }
@ -66,8 +66,8 @@ namespace FreeSql.MySql {
} }
public bool OnCheckAvailable(Object<DbConnection> obj) { public bool OnCheckAvailable(Object<DbConnection> obj) {
if ((obj.Value as MySqlConnection).Ping() == false) obj.Value.Open(); if (obj.Value.State == ConnectionState.Closed) obj.Value.Open();
return (obj.Value as MySqlConnection).Ping(); return obj.Value.Ping(true);
} }
public DbConnection OnCreate() { public DbConnection OnCreate() {
@ -84,7 +84,7 @@ namespace FreeSql.MySql {
if (_pool.IsAvailable) { 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 { try {
obj.Value.Open(); obj.Value.Open();
@ -100,7 +100,7 @@ namespace FreeSql.MySql {
if (_pool.IsAvailable) { 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 { try {
await obj.Value.OpenAsync(); await obj.Value.OpenAsync();
@ -128,4 +128,34 @@ namespace FreeSql.MySql {
_pool.unavailableHandler?.Invoke(); _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<bool> 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;
}
}
}
} }

View File

@ -82,10 +82,7 @@ namespace FreeSql.Oracle {
public bool OnCheckAvailable(Object<DbConnection> obj) { public bool OnCheckAvailable(Object<DbConnection> obj) {
if (obj.Value.State == ConnectionState.Closed) obj.Value.Open(); if (obj.Value.State == ConnectionState.Closed) obj.Value.Open();
var cmd = obj.Value.CreateCommand(); return obj.Value.Ping(true);
cmd.CommandText = "select 1";
cmd.ExecuteNonQuery();
return true;
} }
public DbConnection OnCreate() { public DbConnection OnCreate() {
@ -118,7 +115,7 @@ namespace FreeSql.Oracle {
if (_pool.IsAvailable) { 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 { try {
await obj.Value.OpenAsync(); await obj.Value.OpenAsync();
@ -149,14 +146,29 @@ namespace FreeSql.Oracle {
static class DbConnectionExtensions { 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 { try {
var cmd = that.CreateCommand(); PingCommand(that).ExecuteNonQuery();
cmd.CommandText = "select 1 from dual";
cmd.ExecuteNonQuery();
return true; return true;
} catch { } catch {
try { that.Close(); } catch { } if (that.State != ConnectionState.Closed) try { that.Close(); } catch { }
if (isThrow) throw;
return false;
}
}
async public static Task<bool> 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; return false;
} }
} }

View File

@ -77,10 +77,7 @@ namespace FreeSql.PostgreSQL {
public bool OnCheckAvailable(Object<DbConnection> obj) { public bool OnCheckAvailable(Object<DbConnection> obj) {
if (obj.Value.State == ConnectionState.Closed) obj.Value.Open(); if (obj.Value.State == ConnectionState.Closed) obj.Value.Open();
var cmd = obj.Value.CreateCommand(); return obj.Value.Ping(true);
cmd.CommandText = "select 1";
cmd.ExecuteNonQuery();
return true;
} }
public DbConnection OnCreate() { public DbConnection OnCreate() {
@ -113,7 +110,7 @@ namespace FreeSql.PostgreSQL {
if (_pool.IsAvailable) { 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 { try {
await obj.Value.OpenAsync(); await obj.Value.OpenAsync();
@ -144,14 +141,29 @@ namespace FreeSql.PostgreSQL {
static class DbConnectionExtensions { 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 { try {
var cmd = that.CreateCommand(); PingCommand(that).ExecuteNonQuery();
cmd.CommandText = "select 1";
cmd.ExecuteNonQuery();
return true; return true;
} catch { } catch {
try { that.Close(); } catch { } if (that.State != ConnectionState.Closed) try { that.Close(); } catch { }
if (isThrow) throw;
return false;
}
}
async public static Task<bool> 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; return false;
} }
} }

View File

@ -71,10 +71,7 @@ namespace FreeSql.SqlServer {
public bool OnCheckAvailable(Object<DbConnection> obj) { public bool OnCheckAvailable(Object<DbConnection> obj) {
if (obj.Value.State == ConnectionState.Closed) obj.Value.Open(); if (obj.Value.State == ConnectionState.Closed) obj.Value.Open();
var cmd = obj.Value.CreateCommand(); return obj.Value.Ping(true);
cmd.CommandText = "select 1";
cmd.ExecuteNonQuery();
return true;
} }
public DbConnection OnCreate() { public DbConnection OnCreate() {
@ -107,7 +104,7 @@ namespace FreeSql.SqlServer {
if (_pool.IsAvailable) { 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 { try {
await obj.Value.OpenAsync(); 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 { try {
var cmd = that.CreateCommand(); PingCommand(that).ExecuteNonQuery();
cmd.CommandText = "select 1";
cmd.ExecuteNonQuery();
return true; return true;
} catch { } catch {
if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } if (that.State != ConnectionState.Closed) try { that.Close(); } catch { }
if (isThrow) throw;
return false;
}
}
async public static Task<bool> 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; return false;
} }
} }

View File

@ -31,7 +31,7 @@ namespace FreeSql.Sqlite {
public void Return(Object<DbConnection> obj, Exception exception, bool isRecreate = false) { public void Return(Object<DbConnection> obj, Exception exception, bool isRecreate = false) {
if (exception != null && exception is SQLiteException) { 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); base.Return(obj, isRecreate);
} }
@ -78,8 +78,8 @@ namespace FreeSql.Sqlite {
} }
public bool OnCheckAvailable(Object<DbConnection> obj) { public bool OnCheckAvailable(Object<DbConnection> obj) {
if ((obj.Value as SQLiteConnection).Ping() == false) obj.Value.OpenAndAttach(Attaches); if (obj.Value.State == ConnectionState.Closed) obj.Value.OpenAndAttach(Attaches);
return (obj.Value as SQLiteConnection).Ping(); return obj.Value.Ping(true);
} }
public DbConnection OnCreate() { public DbConnection OnCreate() {
@ -96,7 +96,7 @@ namespace FreeSql.Sqlite {
if (_pool.IsAvailable) { 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 { try {
obj.Value.OpenAndAttach(Attaches); obj.Value.OpenAndAttach(Attaches);
@ -112,7 +112,7 @@ namespace FreeSql.Sqlite {
if (_pool.IsAvailable) { 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 { try {
await obj.Value.OpenAndAttachAsync(Attaches); await obj.Value.OpenAndAttachAsync(Attaches);
@ -140,16 +140,31 @@ namespace FreeSql.Sqlite {
_pool.unavailableHandler?.Invoke(); _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 { try {
var cmd = that.CreateCommand(); PingCommand(that).ExecuteNonQuery();
cmd.CommandText = "select 1";
cmd.ExecuteNonQuery();
return true; return true;
} catch { } catch {
if (that.State != ConnectionState.Closed) try { that.Close(); } catch { } if (that.State != ConnectionState.Closed) try { that.Close(); } catch { }
if (isThrow) throw;
return false;
}
}
async public static Task<bool> 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; return false;
} }
} }