Frage

I am learning Ruby. I am trying to make a connection to MySQL db using mysql gem. One of my concern is connection closing.

How should I ensure the connection closing at unexpected situations such as exception throwing?

War es hilfreich?

Lösung

You mean, something like this?

begin
  db = open_mysql_connection
  # do stuff
ensure
  # this block is always executed, even if exception is raised
  db.close
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top