getConnection method

Future<OracleConnection> getConnection()

Get a connection from the pool

Implementation

Future<OracleConnection> getConnection() async {
  _ensureNotDisposed();

  final connMemoryManager = MemoryManager();

  try {
    final connectionPtr = connMemoryManager.allocate<Pointer<dpiConn>>(sizeOf<Pointer<dpiConn>>());
    final errorInfo = connMemoryManager.allocate<dpiErrorInfo>(sizeOf<dpiErrorInfo>());

    // Acquire connection from pool
    final result = _dpiOracle.dpiPool_acquireConnection(
      _poolPtr.value,
      nullptr,
      0,
      nullptr,
      0,
      nullptr,
      connectionPtr,
    );

    if (result == DPI_FAILURE) {
      _dpiOracle.dpiContext_getError(_context, errorInfo);
      final errorMsg = StringUtils.fromNativeUtf8(errorInfo.ref.message.cast<Char>());
      final errorCode = errorInfo.ref.code;

      throw OraclePoolException(
        'Failed to acquire connection from pool',
        errorCode: errorCode,
        errorMessage: errorMsg,
      );
    }

    return OracleConnection.fromPool(
      _dpiOracle,
      _context,
      connectionPtr,
      _config,
      connMemoryManager,
    );
  } catch (e) {
    connMemoryManager.dispose();
    if (e is OracleException) {
      rethrow;
    }
    throw OraclePoolException('Failed to get connection: $e');
  }
}