Subject error writing data to the connection


My application connects to a database at the start and stays connected while the application is running. The problem occurs when the connection is lost. It just stays disconnected until I restart my application.

The error message is :

Unable to complete network request to host ".......".
Error writing data to the connection.
An existing connection was forcibly closed by the remote host.

If I disconnect and connect that solves the problem as well, but how can I do that automatically? How can I detect that the connection has been lost?