Skip to content

Commit

Permalink
完善功能;添加移除无效的功能;作用不大
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyuttt committed Dec 13, 2018
1 parent 9ec57c3 commit b718d5d
Show file tree
Hide file tree
Showing 14 changed files with 1,517 additions and 3 deletions.
Binary file modified .vs/Hikari/v15/.suo
Binary file not shown.
Binary file modified .vs/Hikari/v15/Server/sqlite3/storage.ide
Binary file not shown.
Binary file modified .vs/Hikari/v15/Server/sqlite3/storage.ide-shm
Binary file not shown.
Binary file modified .vs/Hikari/v15/Server/sqlite3/storage.ide-wal
Binary file not shown.
52 changes: 52 additions & 0 deletions Hikari/Manager/ManagerPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.IO;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace Hikari.Manager
{
Expand All @@ -36,6 +37,9 @@ namespace Hikari.Manager

public class ManagerPool
{
/// <summary>
/// 单例
/// </summary>
public readonly static ManagerPool Instance = new ManagerPool();
private readonly object lock_obj = new object();
private Dictionary<string, HikariDataSource> dicSource = new Dictionary<string, HikariDataSource>();
Expand Down Expand Up @@ -74,8 +78,56 @@ private ManagerPool()
{
PoolDriverXML = Path.Combine("DBPoolCfg", "DBType.xml");
DirverDir = "Dirvers";
CheckValiate();
}

/// <summary>
/// 监测已经关闭的
/// 只是移除无效的,和性能无关
/// </summary>
private void CheckValiate()
{
Task.Factory.StartNew(() =>
{
Thread.Sleep(1000000);
List<IDbConnection> lst = new List<IDbConnection>(100);
foreach(var item in dicCons)
{
//遍历查找已经关闭的对象
HikariConnection hikari = item.Value as HikariConnection;
if(hikari!=null)
{

if(hikari.IsClosed)
{
//没有加锁,直接移除会导致功能受影响
lst.Add(item.Value);
}
}

}
//已经关闭的
if (lst.Count > 0)
{
//避免刚刚进入的被替换;主要是解决线程不是新线程的问题
int[] keys = new int[dicCons.Count];
dicCons.Keys.CopyTo(keys, 0);
IDbConnection connection = null;
foreach(int key in keys)
{
if( dicCons.TryGetValue(key,out connection))
{
if(lst.Contains(connection))
{
dicCons.TryRemove(key, out connection);
}
}
}
}
//
CheckValiate();//递归返回
});
}


/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion Hikari/ProxyConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public void ChangeDatabase(string databaseName)
/// </summary>
public void Close()
{
if(IsClosed)
{
//已经关闭的不再关闭
return;
}
isClosed = true;
poolEntry.Recycle(DateTime.Now.Ticks);
}
Expand All @@ -77,7 +82,6 @@ public IDbCommand CreateCommand()
public void Open()
{
isClosed = false;

delegateCon.Open();
}

Expand Down
9 changes: 9 additions & 0 deletions Hikari/bin/Debug/netstandard2.0/DBPoolCfg/DBType.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<DBType>
<HY>
<DriverDLL>HY</DriverDLL>
</HY>
<MySql>
<DriverDLL>MySql.Data</DriverDLL>
</MySql>
</DBType>
13 changes: 13 additions & 0 deletions Hikari/bin/Debug/netstandard2.0/DBPoolCfg/Hikari.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ConnectString=Server= 127.0.0.1; Port = 5432; User Id = postgres; Password = 1234; Database = postgres;Pooling=true;
ConnectionTimeout=3000
IdleTimeout=600000
MaxLifetime=1800000
MinimumIdle=3
MaximumPoolSize=10
PoolName=jinyu
ConnectionInitSql=
ValidationTimeout=
LeakDetectionThreshold=0
DBType=
DriverDLL=
DriverDir=DBDrivers
Loading

0 comments on commit b718d5d

Please sign in to comment.