Now checks if database connection is open before each database operation and re-opens the connection if it is closed.