} // func FindPooledConnection internal IPpsConnectionHandle GetOrCreatePooledConnection(PpsDataSource dataSource, IPpsPrivateDataContext userData, bool throwException) { lock (pooledConnections) { var pooled = FindPooledConnection(dataSource); if (pooled == null) { var handle = dataSource.CreateConnection(userData, throwException); if (handle == null) { return(null); } pooledConnections.Add(new PooledConnection(handle)); Log.Info("New pooled connection: {0}", dataSource.Name); return(handle); } else { //Log.Info("Reuse pooled connection: {0}", dataSource.Name); return(pooled.Handle); } } } // func GetOrCreatePooledConnection
} // proc Dispose #endregion #region -- User connection pool ------------------------------------------- private PooledConnection FindPooledConnection(PpsDataSource dataSource) { lock (pooledConnections) { for (var i = pooledConnections.Count - 1; i >= 0; i--) { var cur = pooledConnections[i]; if (cur.IsAlive) { if (cur.Handle.DataSource == dataSource) { return(cur); } } else { var msg = String.Format("Remove pooled connection: {0} after {1:N0}", cur.DataSourceName, unchecked (Environment.TickCount - cur.CreatedAt)); try { cur.Clear(); pooledConnections.RemoveAt(i); Log.Info(msg); } catch (Exception e) { Log.Except(msg, e); } } } return(null); } } // func FindPooledConnection
} // class PpsStringJoinExpression #endregion public async Task <IPpsConnectionHandle> EnsureConnectionAsync(PpsDataSource source, bool throwException) { var c = privateUser.GetOrCreatePooledConnection(source, this, throwException); if (c == null) { c = source.CreateConnection(this, throwException); } return(c != null && await c.EnsureConnectionAsync(throwException) ? c : null); } // func EnsureConnection
} // func CreateTransaction public async Task <PpsDataTransaction> CreateTransactionAsync(PpsDataSource dataSource = null, bool throwException = true) { dataSource = dataSource ?? MainDataSource; // create the connection var c = await EnsureConnectionAsync(dataSource, throwException); if (c == null) { return(null); } return(dataSource.CreateTransaction(c)); } // func CreateTransaction