close method

Future<void> close()

Close the connection and release all resources.

Closes the database connection and frees all associated memory. Always call this when you're done with a connection to prevent resource leaks.

Example - Basic Usage:

final connection = await OracleConnection.connect(
  dpiOracle: dpiOracle,
  context: context,
  config: config,
);

try {
  await connection.query('SELECT * FROM employees');
} finally {
  await connection.close();  // Always close!
}

Example - With Error Handling:

OracleConnection? connection;
try {
  connection = await OracleConnection.connect(...);
  await connection.executeUpdate('INSERT ...');
  await connection.commit();
} catch (e) {
  await connection?.rollback();
  rethrow;
} finally {
  await connection?.close();
}

Important Notes:

  • Uncommitted changes are automatically rolled back when closing
  • All open statements are automatically closed
  • Close is idempotent - safe to call multiple times
  • After closing, the connection cannot be reused

See also:

Implementation

Future<void> close() async {
  if (_disposed) {
    return;
  }

  try {
    if (_isConnected && !PointerUtils.isNull(_connectionPtr.value)) {
      _dpiOracle.dpiConn_close(
        _connectionPtr.value,
        DPI_MODE_CONN_CLOSE_DEFAULT,
        nullptr,
        0,
      );
    }
  } catch (e) {
    // Best effort close - log but don't throw
  } finally {
    _isConnected = false;
    _disposed = true;
    _memoryManager.dispose();
  }
}