Skip to content

Commit

Permalink
数据库自动重连
Browse files Browse the repository at this point in the history
  • Loading branch information
Coloryr committed Nov 26, 2021
1 parent d19cf78 commit f824d23
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 26 deletions.
43 changes: 41 additions & 2 deletions ColoryrServer/Core/DataBase/MSCon.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ColoryrServer.FileSystem;
using ColoryrServer.SDK;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
Expand All @@ -12,12 +13,12 @@ public class MSCon
/// <summary>
/// 连接状态
/// </summary>
public static Dictionary<int, bool> State = new();

private static Dictionary<int, bool> State = new();
/// <summary>
/// 连接池
/// </summary>
private static Dictionary<int, string> ConnectStr = new();
private static ConcurrentDictionary<int, bool> Connecting = new();
private static List<SQLConfig> Config;

/// <summary>
Expand Down Expand Up @@ -49,6 +50,44 @@ private static bool Test(SqlConnection item)
}
}

/// <summary>
/// 尝试重连数据库
/// </summary>
/// <param name="id">数据库ID</param>
/// <returns>是否能连接</returns>
internal static bool Contains(int id)
{
if (State.ContainsKey(id) && State[id])
return true;
else if (Config.Count - 1 >= id)
{
if (Connecting.ContainsKey(id))
return false;
if (!Connecting.TryAdd(id, true))
return false;
var config = Config[id];
if (!config.Enable)
return false;
var pass = Encoding.UTF8.GetString(Convert.FromBase64String(config.Password));
string ConnectString = string.Format(config.Conn, config.IP, config.User, pass);
State.Add(id, false);
var Conn = new SqlConnection(ConnectString);
if (Test(Conn))
{
ConnectStr.Add(id, ConnectString);
State[id] = true;
ServerMain.LogOut($"Mysql数据库{id}已连接");
}
else
{
ServerMain.LogError($"Mysql数据库{id}连接失败");
}
Connecting.TryRemove(id, out var v);
return State[id];
}
return false;
}

/// <summary>
/// MSCon初始化
/// </summary>
Expand Down
43 changes: 41 additions & 2 deletions ColoryrServer/Core/DataBase/MysqlCon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ColoryrServer.SDK;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

Expand All @@ -12,13 +13,13 @@ public class MysqlCon
/// <summary>
/// 连接状态
/// </summary>
public static Dictionary<int, bool> State = new();

private static Dictionary<int, bool> State = new();
/// <summary>
/// 连接池
/// </summary>
private static List<SQLConfig> Config;
private static Dictionary<int, string> ConnectStr = new();
private static ConcurrentDictionary<int, bool> Connecting = new();

/// <summary>
/// 连接测试
Expand Down Expand Up @@ -49,6 +50,44 @@ private static bool Test(MySqlConnection item)
}
}

/// <summary>
/// 尝试重连数据库
/// </summary>
/// <param name="id">数据库ID</param>
/// <returns>是否能连接</returns>
internal static bool Contains(int id)
{
if (State.ContainsKey(id) && State[id])
return true;
else if (Config.Count - 1 >= id)
{
if (Connecting.ContainsKey(id))
return false;
if (!Connecting.TryAdd(id, true))
return false;
var config = Config[id];
if (!config.Enable)
return false;
var pass = Encoding.UTF8.GetString(Convert.FromBase64String(config.Password));
string ConnectString = string.Format(config.Conn, config.IP, config.Port, config.User, pass);
State.Add(id, false);
var Conn = new MySqlConnection(ConnectString);
if (Test(Conn))
{
ConnectStr.Add(id, ConnectString);
State[id] = true;
ServerMain.LogOut($"Mysql数据库{id}已连接");
}
else
{
ServerMain.LogError($"Mysql数据库{id}连接失败");
}
Connecting.TryRemove(id, out var v);
return State[id];
}
return false;
}

/// <summary>
/// Mysql初始化
/// </summary>
Expand Down
43 changes: 41 additions & 2 deletions ColoryrServer/Core/DataBase/OracleCon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ColoryrServer.SDK;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

Expand All @@ -12,12 +13,12 @@ public class OracleCon
/// <summary>
/// 连接状态
/// </summary>
public static Dictionary<int, bool> State = new();

private static Dictionary<int, bool> State = new();
/// <summary>
/// 连接池
/// </summary>
private static List<SQLConfig> Config;
private static ConcurrentDictionary<int, bool> Connecting = new();
private static Dictionary<int, string> ConnectStr = new();

/// <summary>
Expand Down Expand Up @@ -48,6 +49,44 @@ private static bool Test(OracleConnection item)
}
}

/// <summary>
/// 尝试重连数据库
/// </summary>
/// <param name="id">数据库ID</param>
/// <returns>是否能连接</returns>
internal static bool Contains(int id)
{
if (State.ContainsKey(id) && State[id])
return true;
else if (Config.Count - 1 >= id)
{
if (Connecting.ContainsKey(id))
return false;
if (!Connecting.TryAdd(id, true))
return false;
var config = Config[id];
if (!config.Enable)
return false;
var pass = Encoding.UTF8.GetString(Convert.FromBase64String(config.Password));
string ConnectString = string.Format(config.Conn, config.IP, config.Port, config.User, pass);
State.Add(id, false);
var Conn = new OracleConnection(ConnectString);
if (Test(Conn))
{
ConnectStr.Add(id, ConnectString);
State[id] = true;
ServerMain.LogOut($"Mysql数据库{id}已连接");
}
else
{
ServerMain.LogError($"Mysql数据库{id}连接失败");
}
Connecting.TryRemove(id, out var v);
return State[id];
}
return false;
}

/// <summary>
/// Oracle初始化
/// </summary>
Expand Down
14 changes: 4 additions & 10 deletions ColoryrServer/Core/Resources/DatabaseSDK.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ namespace ColoryrServer.SDK
{
this.ID = ID;
this.Database = Database;
if (!MysqlCon.State.ContainsKey(ID))
if (!MysqlCon.Contains(ID))
throw new ErrorDump($"û��Mysql���ݿ�{ID}");
if (MysqlCon.State[ID] == false)
throw new ErrorDump("Mysql���");
if (string.IsNullOrWhiteSpace(Database))
throw new ErrorDump("û��ѡ�����ݿ�");
}
Expand Down Expand Up @@ -143,10 +141,8 @@ namespace ColoryrServer.SDK
{
this.Database = Database;
this.ID = ID;
if (!MSCon.State.ContainsKey(ID))
throw new ErrorDump($"û��Mysql���ݿ�{ID}");
if (MSCon.State[ID] == false)
throw new ErrorDump("MS���ݿ�û������");
if (!MSCon.Contains(ID))
throw new ErrorDump($"û��MS���ݿ�{ID}");
if (string.IsNullOrWhiteSpace(Database))
throw new ErrorDump("û��ѡ�����ݿ�");

Expand Down Expand Up @@ -243,10 +239,8 @@ namespace ColoryrServer.SDK
public Oracle(string Database = "", int ID = 0)
{
this.ID = ID;
if (!OracleCon.State.ContainsKey(ID))
if (!OracleCon.Contains(ID))
throw new ErrorDump($"û��Oracle���ݿ�{ID}");
if (OracleCon.State[ID] == false)
throw new ErrorDump("Oracleû������");
if (string.IsNullOrWhiteSpace(Database))
throw new ErrorDump("û��ѡ�����ݿ�");
this.Database = Database;
Expand Down
14 changes: 4 additions & 10 deletions ColoryrServer/Core/SDK/DatabaseSDK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ public Mysql(string Database, int ID = 0)
{
this.ID = ID;
this.Database = Database;
if (!MysqlCon.State.ContainsKey(ID))
if (!MysqlCon.Contains(ID))
throw new ErrorDump($"没有Mysql数据库{ID}");
if (MysqlCon.State[ID] == false)
throw new ErrorDump("Mysql未就绪");
if (string.IsNullOrWhiteSpace(Database))
throw new ErrorDump("没有选择数据库");
}
Expand Down Expand Up @@ -143,10 +141,8 @@ public MSsql(string Database, int ID = 0)
{
this.Database = Database;
this.ID = ID;
if (!MSCon.State.ContainsKey(ID))
throw new ErrorDump($"没有Mysql数据库{ID}");
if (MSCon.State[ID] == false)
throw new ErrorDump("MS数据库没有链接");
if (!MSCon.Contains(ID))
throw new ErrorDump($"没有MS数据库{ID}");
if (string.IsNullOrWhiteSpace(Database))
throw new ErrorDump("没有选择数据库");

Expand Down Expand Up @@ -243,10 +239,8 @@ public class Oracle
public Oracle(string Database = "", int ID = 0)
{
this.ID = ID;
if (!OracleCon.State.ContainsKey(ID))
if (!OracleCon.Contains(ID))
throw new ErrorDump($"没有Oracle数据库{ID}");
if (OracleCon.State[ID] == false)
throw new ErrorDump("Oracle没有链接");
if (string.IsNullOrWhiteSpace(Database))
throw new ErrorDump("没有选择数据库");
this.Database = Database;
Expand Down

0 comments on commit f824d23

Please sign in to comment.