If the application has an open connection for a long time, it is recommended to check whether the connection is still valid, before using it. Call of Ping method has single round trip on Oracle server.
It is not a good idea to keep a connection open for long time. Connection pool automatically pings all connections within, thus closing old connections and opening new ones is encouraged.
If the connection being pinged is not initialized or has the state other than ConnectionState.Open, the method throws an exception.
public void ExecuteSafely(OracleConnection connection) { OracleCommand command = new OracleCommand("INSERT INTO Dept(DeptNo, DName) Values('50', 'DEVELOPMENT')"); command.Connection = connection; try { if (connection.Ping()) { command.ExecuteNonQuery(); Console.WriteLine("Successfully executed"); } else { connection.Close(); Console.WriteLine("Unable to ping the server"); } } catch { connection.Close(); Console.WriteLine("Error during query execution"); } }
Public Sub ExecuteSafely(ByVal connection As OracleConnection) Dim command As New OracleCommand("INSERT INTO Dept(DeptNo, DName) Values('50', 'DEVELOPMENT')") command.Connection = connection Try If (connection.Ping()) Then command.ExecuteNonQuery() Console.WriteLine("Successfully executed") Else connection.Close() Console.WriteLine("Unable to ping the server") End If Catch connection.Close() Console.WriteLine("Error during query execution") End Try End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2