当前位置: 首页 > news >正文

高端大气的网站模板软件外包

高端大气的网站模板,软件外包,绵阳网站建设哪家好,绍兴企业做网站介绍 以一个简易版的数据库连接池的实现来说明一下 连接池的connection以队列来管理 getConnection的时候,如果队列中connection个数小于50,且暂时无可用的connection(个数为0或者peek看下头部需要先出那个元素还处于不可用状态)…

介绍

以一个简易版的数据库连接池的实现来说明一下
连接池的connection以队列来管理
getConnection的时候,如果队列中connection个数小于50,且暂时无可用的connection(个数为0或者peek看下头部需要先出那个元素还处于不可用状态),就新建连接并建立连接,开始一直新建到50个connection,就是_currentPoolSize =50
如果队列中connection个数大于等于50,且暂时无可用的connection(个数为0或者peek看下头部需要先出那个元素还处于不可用状态),就等着Monitor.Wait(_connectionPoolQueueLock)

returnConnection的时候,使用Monitor.Pulse(_connectionPoolQueueLock) 随机通知一个wait的线程可以继续getConnection了

using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using Mono.Data.Sqlite;namespace demo.unity.sqlite
{public class SQLiteConnectionManager{private Queue<Tuple<SqliteConnection, DateTime>> _connectionPoolQueue;private object _connectionPoolQueueLock = new object();private const int maxPoolSize = 50;private volatile bool _disposed;private int _currentPoolSize;private readonly System.Timers.Timer _cleanupTimer = new System.Timers.Timer(10 * 60 * 1000);public SQLiteConnectionManager(){_connectionPoolQueue = new Queue<Tuple<SqliteConnection, DateTime>>(maxPoolSize);_cleanupTimer.Elapsed += _cleanupTimerElapsed;_cleanupTimer.AutoReset = true;_cleanupTimer.Start();}private void _cleanupTimerElapsed(object sender, System.Timers.ElapsedEventArgs e){lock (_connectionPoolQueueLock){while (_connectionPoolQueue.Count > 0 && (DateTime.UtcNow - _connectionPoolQueue.Peek().Item2).TotalMinutes > 15){var tup = _connectionPoolQueue.Dequeue();tup.Item1.Dispose();_currentPoolSize--;}}}private SqliteConnection _createNewConnection(SqliteConnectionStringBuilder builder){var connection = new SqliteConnection(builder.ConnectionString);connection.Open();return connection;}public SqliteConnection getConnection(SqliteConnectionStringBuilder builder){lock (_connectionPoolQueueLock){// count == 0 or queue.peek no use connectionwhile (_connectionPoolQueue.Count == 0  || _connectionPoolQueue.Peek().Item1.State != ConnectionState.Open){if (_disposed){throw new ObjectDisposedException("The DB connection pool is is already disposed");}if (_currentPoolSize < maxPoolSize){// create and open connectionvar connection = _createNewConnection(builder);_connectionPoolQueue.Enqueue( new Tuple<SqliteConnection, DateTime>(connection, DateTime.UtcNow));_currentPoolSize++;}else{Monitor.Wait(_connectionPoolQueueLock);}}return _connectionPoolQueue.Dequeue().Item1;}}public void returnConnection(SqliteConnection connection){if (connection == null){return;}lock (_connectionPoolQueueLock){_connectionPoolQueue.Enqueue(new Tuple<SqliteConnection, DateTime>(connection, DateTime.UtcNow));Monitor.Pulse(_connectionPoolQueueLock);}}public void dispose(){lock (_connectionPoolQueueLock){_disposed = true;while (_connectionPoolQueue.Count > 0){var tup  = _connectionPoolQueue.Dequeue();tup.Item1?.Dispose();_currentPoolSize--;}// wake up any waiting threadsMonitor.PulseAll(_connectionPoolQueueLock);}_cleanupTimer.Stop();_cleanupTimer.Dispose();}}
}
http://www.ds6.com.cn/news/2660.html

相关文章:

  • 护士注册网站今日军事新闻热点事件
  • 简单的网站架构百度咨询电话人工台
  • 做网站前端用什么电子商务网页制作
  • 自己做刷东西的网站app营销模式有哪些
  • 网站开发需要多少钱app百度seo搜索引擎优化厂家
  • 南宁做网站科技公司竞价账户托管的公司有哪些
  • 淘宝网站开发电商运营公司简介
  • 免费做网站公司推荐正规培训机构有哪些
  • 无法解析服务器域名杭州seo公司排名
  • 做推广的网站有哪些可视化网页制作工具
  • 网站系统介绍青岛网站建设有限公司
  • 做cpa能用什么网站西安百度百科
  • 网站收录500多页网站建设开发价格
  • 旅游电子商务网站建设论文网络热词作文
  • 叠石桥网站建设湖南企业竞价优化
  • 增城网站怎么做seo2345浏览器
  • 石家庄疫情再次爆发宁波seo费用
  • 大连市卫生健康委员会网站信息流优化
  • 南宁哪里有做网站的公司物联网开发
  • 惠州网站建设公司友情链接交换的方法
  • 重庆做网站 帮助中心短期培训学什么好
  • wordpress网站管理插件百度账号找回
  • wordpress post-new.php互联网seo是什么意思
  • 网站安全性设计腾讯推广一次广告多少钱
  • 做赌博游戏网站违法精准营销推广
  • 自己做的网站如何在网络上展示成都seo
  • 大作设计网站官网登录入口湖北网站seo设计
  • 北京做网站开发的公司域名注册网站查询
  • 企业做定制网站的好处sem广告投放是做什么的
  • 乐山网站建设公司无锡seo