Fix bugs with connection pool not closing connections properly