Skip to content

Commit a68e07b

Browse files
committed
Manually call release on the prepared statements
The garbage collector will come back through later and release the opaque nif resource. This will at least reclaim a large portion of the memory taken up by the prepared statement.
1 parent 4b432e3 commit a68e07b

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/exqlite/connection.ex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ defmodule Exqlite.Connection do
259259
This callback is called in the client process.
260260
"""
261261
@impl true
262-
def handle_close(_query, _opts, state) do
262+
def handle_close(query, _opts, state) do
263+
Sqlite3.release(state.db, query.ref)
263264
{:ok, nil, state}
264265
end
265266

@@ -274,10 +275,8 @@ defmodule Exqlite.Connection do
274275
end
275276

276277
@impl true
277-
def handle_deallocate(%Query{} = _query, _cursor, _opts, state) do
278-
# We actually don't need to do anything about the cursor. Since it is a
279-
# prepared statement, it will be garbage collected by erlang when it loses
280-
# references.
278+
def handle_deallocate(%Query{} = query, _cursor, _opts, state) do
279+
Sqlite3.release(state.db, query.ref)
281280
{:ok, nil, state}
282281
end
283282

test/exqlite/connection_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,22 @@ defmodule Exqlite.ConnectionTest do
188188
assert {:ok, conn} == Connection.ping(conn)
189189
end
190190
end
191+
192+
describe ".handle_close/3" do
193+
test "releases the underlying prepared statement" do
194+
{:ok, conn} = Connection.connect(database: :memory)
195+
196+
{:ok, query, _result, conn} =
197+
%Query{statement: "create table users (id integer primary key, name text)"}
198+
|> Connection.handle_execute([], [], conn)
199+
200+
assert {:ok, nil, conn} == Connection.handle_close(query, [], conn)
201+
202+
{:ok, query, conn} =
203+
%Query{statement: "select * from users where id < ?"}
204+
|> Connection.handle_prepare([], conn)
205+
206+
assert {:ok, nil, conn} == Connection.handle_close(query, [], conn)
207+
end
208+
end
191209
end

0 commit comments

Comments
 (0)