예제 #1
0
        public DatabasePool(Func <PooledDatabase> createFunc, int maximumRetained = 0, EvictionSettings evictSetting = null)
        {
            if (maximumRetained == 0)
            {
                maximumRetained = Environment.ProcessorCount * 2;
            }
#if NET45
            currentDatabaseLocal = new ThreadLocal <PooledDatabase>();
#else
            currentDatabaseLocal = new AsyncLocal <PooledDatabase>();
#endif


            Pool = new ObjectPool <PooledDatabase>(maximumRetained, () =>
            {
                PooledDatabase db = createFunc();
                db.SetPool(this); //设置池
                return(db);
            }
                                                   , evictSetting, null);
        }
예제 #2
0
        public DatabasePool(DatabasePoolPolicy policy)
        {
            if (policy == null)
            {
                throw new ArgumentException("policy can not be null!");
            }
            //DatabasePoolPolicy policy = new DatabasePoolPolicy();
            //if (policyConfig != null)
            //{
            //    policyConfig(policy);
            //}
#if NET45
            currentDatabaseLocal = new ThreadLocal <PooledDatabase>();
#else
            currentDatabaseLocal = new AsyncLocal <PooledDatabase>();
#endif

            int maximumRetained = policy.MaxPoolSize;

            if (maximumRetained == 0)
            {
                maximumRetained = Environment.ProcessorCount * 2;
            }

            var createDelegate = policy.CreateFactory;

            var asyncCreateDelegate = policy.AsyncCreateFactory;


            Pool = new ObjectPool <PooledDatabase>(maximumRetained, () =>
            {
                PooledDatabase db   = createDelegate();//createFunc();
                db.OnCreateResource = (pooledObj) =>
                {
                    policy.OnCreateResource?.Invoke(pooledObj);
                };
                db.OnGetResource = (pooledObj) =>
                {
                    policy.OnGetResource?.Invoke(pooledObj);
                };
                db.OnReturnResource = (pooledObj) =>
                {
                    policy.OnReturnResource?.Invoke(pooledObj);
                };
                db.OnReleaseResource = (pooledObj) =>
                {
                    policy.OnReleaseResource?.Invoke(pooledObj);

                    var resource = pooledObj as PooledDatabase;
                    resource?.CloseReally();
                };
                db.OnResetState = (pooledObj) =>
                {
                    policy.OnResetState?.Invoke(pooledObj);

                    var resource = pooledObj as PooledDatabase;
                    resource?.CloseReally();
                };

                db.OnEvictResource = (pooledObj) =>
                {
                    policy.OnEvictResource?.Invoke(pooledObj);

                    var resource = pooledObj as PooledDatabase;
                    resource?.CloseReally();
                };
                db.OnValidateObject = (pooledObj) =>
                {
                    return(policy.OnValidateObject != null ? policy.OnValidateObject(pooledObj) : true);
                };
                db.SetPool(this); //设置池
                return(db);
            }
                                                   , async(c, b) =>
            {
                PooledDatabase db   = await asyncCreateDelegate(c, b);//createFunc();
                db.OnCreateResource = (pooledObj) =>
                {
                    policy.OnCreateResource?.Invoke(pooledObj);
                };
                db.OnGetResource = (pooledObj) =>
                {
                    policy.OnGetResource?.Invoke(pooledObj);
                };
                db.OnReturnResource = (pooledObj) =>
                {
                    policy.OnReturnResource?.Invoke(pooledObj);
                };
                db.OnReleaseResource = (pooledObj) =>
                {
                    policy.OnReleaseResource?.Invoke(pooledObj);
                    var resource = pooledObj as PooledDatabase;
                    resource?.CloseReally();
                };
                db.OnResetState = (pooledObj) =>
                {
                    policy.OnResetState?.Invoke(pooledObj);
                    var resource = pooledObj as PooledDatabase;
                    resource?.CloseReally();
                };

                db.OnEvictResource = (pooledObj) =>
                {
                    policy.OnEvictResource?.Invoke(pooledObj);
                    var resource = pooledObj as PooledDatabase;
                    resource?.CloseReally();
                };
                db.OnValidateObject = (pooledObj) =>
                {
                    return(policy.OnValidateObject != null ? policy.OnValidateObject(pooledObj) : true);
                };
                db.SetPool(this); //设置池
                return(db);
            }
                                                   , policy.EvictionSettings, policy.EvictionTimer, policy.EnableDiagnostics);
        }