Skip to content
Permalink
Browse files

SQLite3: Always close statements.

SQLite3 adapter must make sure to close statements after queries.

Fixes: #13631
  • Loading branch information...
Timur Alperovich
Timur Alperovich committed Jan 8, 2014
1 parent 8d146c8 commit 6f16513b587a0805a23eab99d5f283476b1d6687
@@ -1,3 +1,9 @@
* Ensure SQLite3 statements are closed on errors.

Fixes: #13631

*Timur Alperovich*

* Enable partial indexes for sqlite >= 3.8.0

See http://www.sqlite.org/partialindex.html
@@ -299,9 +299,12 @@ def exec_query(sql, name = nil, binds = [])
# Don't cache statements if they are not prepared
if without_prepared_statement?(binds)
stmt = @connection.prepare(sql)
cols = stmt.columns
records = stmt.to_a
stmt.close
begin
cols = stmt.columns
records = stmt.to_a
ensure
stmt.close
end
stmt = records
else
cache = @statements[sql] ||= {
@@ -401,6 +401,21 @@ def test_respond_to_disable_extension
assert @conn.respond_to?(:disable_extension)
end

def test_statement_closed
db = SQLite3::Database.new(ActiveRecord::Base.
configurations['arunit']['database'])
statement = SQLite3::Statement.new(db,
'CREATE TABLE statement_test (number integer not null)')
statement.stubs(:step).raises(SQLite3::BusyException, 'busy')
statement.stubs(:columns).once.returns([])
statement.expects(:close).once
SQLite3::Statement.stubs(:new).returns(statement)

assert_raise ActiveRecord::StatementInvalid do
@conn.exec_query 'select * from statement_test'
end
end

private

def assert_logged logs

0 comments on commit 6f16513

Please sign in to comment.
You can’t perform that action at this time.