Properly close database connections and free resources across all supported database backends.
The close()
method properly closes database connections and releases associated resources. This is essential for preventing connection leaks, especially in long-running applications or when working with connection-limited databases.
close() -> None
The close()
method takes no parameters.
Type: None
The method does not return a value. It performs cleanup operations and closes the database connection silently.
Created user with ID: 1 Found 1 user(s) Database connection closed
Use the with
statement for automatic connection management:
Added Laptop with ID: 1 Added Book with ID: 2 Added Coffee Mug with ID: 3 Total products in database: 3 Connection closed automatically via context manager
Manage multiple database connections properly:
Synced 2 users from source to target database Target database now has 2 users Both database connections closed
Ensure connections are closed even when errors occur:
=== Manual cleanup example === Order created successfully: ID 1 Database connection closed in finally block === Context manager example === Customer created with ID: 1 Found 1 customer(s) Connection automatically closed by context manager
Closes the SQLite connection and commits any pending transactions:
Closes MySQL connection and releases server resources:
Closes PostgreSQL connection and returns it to the connection pool:
Closes MongoDB client connection:
Now that you understand connection management, explore:
Learn the basics of setting up and using Akron ORM
โ view getting started guide