OJ's rants

It's not about you, it's about the software

WTF: The Exceptional Connection

| Comments

This would have to be close to the dumbest way of verifying the state of a connection :) Well, that might not be true, but it’s definitely one of the dumbest ways.

This is a funky snippet of code that was extracted from a system I’m doing some work on at the moment:

1
2
3
4
5
6
7
8
public static bool IsValid(OracleConnection conn)
{
  OracleCommand cmd = new OracleCommand("select 1 from dual", conn);
  cmd.CommandType = CommandType.Text;
  cmd.ExecuteReader();
  cmd.Dispose();
  return true;
}

So, if it doesn’t hurl an exception at you, it’s valid! Nice work eh? So if you have to check for exceptions anyway, why not just try using the connection in the first place and handle THAT exception instead?

shudders

WTFness: 9 / 10

Comments