문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top