createConnection method

  1. @override
FutureOr<PostgreSQLConnectionWrapper> createConnection()
override

Creates a connection C for this adapte

Implementation

@override
FutureOr<PostgreSQLConnectionWrapper> createConnection() async {
  var password = await _getPassword();

  var count = ++_connectionCount;

  for (var i = 0; i < 3; ++i) {
    var timeout = i == 0 ? 3 : (i == 1 ? 10 : 30);

    var connection = await _createConnectionImpl(password, timeout);

    if (connection != null) {
      var connUrl = getConnectionURL(connection);
      _log.info(
          'createConnection[#$count $poolAliveElementsSize/$maxConnections]> $connUrl > $connection');

      return connection;
    }

    if (poolSize > 0) {
      var poolConn = peekFromPool();

      if (poolConn != null) {
        return poolConn.resolveMapped((conn) {
          if (conn != null) {
            var connUrl = getConnectionURL(conn);
            _log.severe(
                "Skipping connection retry. Returning connection from pool: $connUrl");
            return conn;
          }

          return _createConnectionImpl(password, timeout).then((conn) {
            if (conn == null) {
              var error = PostgreSQLException(
                  "Error connecting to: $databaseName@$host:$port");

              _log.severe(
                  "Can't connect to PostgreSQL: $databaseName@$host:$port");

              throw error;
            }
            return conn;
          });
        });
      }
    }
  }

  var error =
      PostgreSQLException("Error connecting to: $databaseName@$host:$port");

  _log.severe("Can't connect to PostgreSQL: $databaseName@$host:$port");

  throw error;
}