Our production server kills inactive connections, so our API needs to restore them when they are needed. The following code works, but it is very repetitive:
The common part for those method is the connection retrieving process. You could create a new static method that is responsible for that called for example retrieve connection that take command as argument and return it if connection is established in other cases throws an exception.