Add context management to cursor and connection