Пример #1
0
        private async Task <AutoRefreshDBConnection> GenerateNewConnection()
        {
            AutoRefreshDBConnection sqlconn = new AutoRefreshDBConnection();
            await sqlconn.Start();

            return(sqlconn);
        }
Пример #2
0
        public Task <AutoRefreshDBConnection> CheckOut()
        {
            return(Task.Run(() =>
            {
                AutoRefreshDBConnection sqlconn = null;

                lock (pool_lock)
                {
                    if (db_connection_string_cache != MCv2Persistance.Instance.Config.DatabaseConfiguration.DatabaseConnectionProperties.ConnectionString)
                    {
                        //the connection string has changed. all connections are now invalid.

                        foreach (AutoRefreshDBConnection sqlconn_b in db_connections_checked_out)
                        {
                            if (sqlconn_b != null)
                            {
                                sqlconn_b.Dispose();
                            }
                        }

                        db_connections_checked_out.Clear();

                        foreach (AutoRefreshDBConnection sqlconn_b in db_connection_pool)
                        {
                            if (sqlconn_b != null)
                            {
                                sqlconn_b.Dispose();
                            }
                        }

                        db_connection_pool.Clear();

                        db_connection_string_cache = MCv2Persistance.Instance.Config.DatabaseConfiguration.DatabaseConnectionProperties.ConnectionString;
                    }

                    if (db_connection_pool.Count() > 0)
                    {
                        sqlconn = db_connection_pool.First();
                        db_connection_pool.Remove(sqlconn);
                        db_connections_checked_out.Add(sqlconn);
                    }
                    else
                    {
                        sqlconn = new Func <AutoRefreshDBConnection>(() =>
                        {
                            var x = GenerateNewConnection();
                            x.Wait();
                            return x.Result;
                        }).Invoke();
                        db_connections_checked_out.Add(sqlconn);
                    }
                }

                return sqlconn;
            }));
        }
Пример #3
0
        public void CheckIn(AutoRefreshDBConnection sqlconn)
        {
            lock (pool_lock)
            {
                int maintain_num_connections = MCv2Persistance.Instance.Config.DatabaseConfiguration.NumCachedDBConnections;

                db_connections_checked_out.Remove(sqlconn);

                if (db_connection_pool.Count() >= maintain_num_connections)
                {
                    sqlconn.Dispose();
                }
                else
                {
                    db_connection_pool.Add(sqlconn);
                }
            }
        }