You can use the 'try...finally' block in Python to ensure that the database connection is always closed.
- Implement a custom function to manage connections
- Manually close the connection in each place
- Rely on Python's automatic garbage collection
- Use a context manager like 'with' statements
The 'try...finally' block ensures that the database connection is closed even if an exception occurs. It's a robust way to manage resource cleanup.
Loading...
Related Quiz
- How can you reload a module that has been modified after it was initially imported?
- In NumPy, the ____ function is used to calculate the element-wise maximum of two arrays.
- How do you execute a block of code only if multiple conditions are all true in Python?
- You are given a task to parse string dates in various formats and convert them to datetime objects in Python. Which module would you use to achieve this?
- To create a tuple with a single item, you need to add a _______ after the item.