From 2cb37d374c3c0b02d30b1172a10b383fd1156a83 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Fri, 19 Feb 2016 12:39:09 -0300 Subject: [PATCH] ensure db is closed after block avoid GC Warning --- src/db/database.cr | 2 ++ src/db/db.cr | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/db/database.cr b/src/db/database.cr index ef2a459..2f85551 100644 --- a/src/db/database.cr +++ b/src/db/database.cr @@ -22,6 +22,8 @@ module DB # Closes all connection to the database. def close @connection.try &.close + # prevent GC Warning: Finalization cycle involving discovered by mysql implementation + @connection = nil end # :nodoc: diff --git a/src/db/db.cr b/src/db/db.cr index a4e0678..abbdf2c 100644 --- a/src/db/db.cr +++ b/src/db/db.cr @@ -101,8 +101,11 @@ module DB # Same as `#open` but the database is yielded and closed automatically. def self.open(uri : URI | String, &block) build_database(uri).tap do |db| - yield db - db.close + begin + yield db + ensure + db.close + end end end