mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
avoid messing with finalize cycle in Disposable.
ensure statements are closed when connection is closed
This commit is contained in:
parent
f836bbfccb
commit
34ae9d5775
4 changed files with 39 additions and 5 deletions
|
@ -1,5 +1,8 @@
|
|||
require "./spec_helper"
|
||||
|
||||
class DummyException < Exception
|
||||
end
|
||||
|
||||
describe DB::ResultSet do
|
||||
it "should enumerate records using each" do
|
||||
nums = [] of Int32
|
||||
|
@ -15,4 +18,28 @@ describe DB::ResultSet do
|
|||
|
||||
nums.should eq([3, 4, 1, 2])
|
||||
end
|
||||
|
||||
it "should close ResultSet after query" do
|
||||
with_dummy do |db|
|
||||
the_rs = uninitialized DB::ResultSet
|
||||
db.query "3,4 1,2" do |rs|
|
||||
the_rs = rs
|
||||
end
|
||||
the_rs.closed?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
it "should close ResultSet after query even with exception" do
|
||||
with_dummy do |db|
|
||||
the_rs = uninitialized DB::ResultSet
|
||||
begin
|
||||
db.query "3,4 1,2" do |rs|
|
||||
the_rs = rs
|
||||
raise DummyException.new
|
||||
end
|
||||
rescue DummyException
|
||||
end
|
||||
the_rs.closed?.should be_true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -65,6 +65,15 @@ describe DB::Statement do
|
|||
end
|
||||
end
|
||||
|
||||
it "closing connection should close statement" do
|
||||
stmt = uninitialized DB::Statement
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare "3,4 1,2"
|
||||
stmt.query
|
||||
end
|
||||
stmt.closed?.should be_true
|
||||
end
|
||||
|
||||
it "query with block should not close statement" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare "3,4 1,2"
|
||||
|
|
|
@ -40,6 +40,9 @@ module DB
|
|||
abstract def build_statement(query) : Statement
|
||||
|
||||
protected def do_close
|
||||
@statements_cache.each do |_, stmt|
|
||||
stmt.close
|
||||
end
|
||||
@statements_cache.clear
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,11 +17,6 @@ module DB
|
|||
@closed
|
||||
end
|
||||
|
||||
# :nodoc:
|
||||
def finalize
|
||||
close
|
||||
end
|
||||
|
||||
# Implementors overrides this method to perform resource cleanup
|
||||
protected abstract def do_close
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue