public void Dispose() { DisposeHelper.EnsureAllSteps( () => this.EndTransaction(), //Transaction が生き残っていたら終わらせる () => { //Connection を Pool に返す if (this._connection != null) { this._pool.ReleaseConnection(this._connection.IndexInPool, this._connection.PayOutNumber); } }, () => this._connection = null, //借りてたものをもう使わないという意思表示 () => this._pool = null); //Pool はお外で管理されてるからここでは参照切るだけ }
private async Task <IConnectionWithId <MySqlConnection> > GetConnectionAsync(ConnectionFactoryParameters parameters, CancellationToken cancellationToken) { //取得済ならそのまま使う if (this._connection != null) { return(this._connection.ConnectionWithId); } await this._connectionLock.WaitAsync(cancellationToken).ConfigureAwait(false); try { if (this._connection != null) { return(this._connection.ConnectionWithId); } //未取得の場合は Pool から払い出してもらう //Strategy が破棄されるまで返さないぞ! var connection = await this._pool.GetConnectionAsync( parameters, _maxExecutionTime, nameof(GlobalConnectionPoolTransactionStrategy), cancellationToken) .ConfigureAwait(false); this._connection = connection; if (!string.IsNullOrWhiteSpace(this._overriddenDatabaseName) && connection.ConnectionWithId.Connection.Database != this._overriddenDatabaseName) { await connection.ConnectionWithId.Connection.ChangeDatabaseAsync(this._overriddenDatabaseName, cancellationToken).ConfigureAwait(false); } return((this._connection = connection).ConnectionWithId); } finally { this._connectionLock.Release(); } }