Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release connection when query cannot be built #65

Merged
merged 3 commits into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions spec/database_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ describe DB::Database do
end
end

it "should return connection to the pool if prepared statement is unable to be built" do
connection = uninitialized DB::Connection
with_dummy "dummy://localhost:1027?initial_pool_size=1" do |db|
connection = DummyDriver::DummyConnection.connections.first
expect_raises do
db.prepared.exec("syntax error")
end
db.pool.is_available?(connection).should be_true
end
end

it "should return connection to the pool if unprepared statement is unable to be built" do
connection = uninitialized DB::Connection
with_dummy "dummy://localhost:1027?initial_pool_size=1" do |db|
connection = DummyDriver::DummyConnection.connections.first
expect_raises do
db.unprepared.exec("syntax error")
end
db.pool.is_available?(connection).should be_true
end
end

describe "prepared_statements connection option" do
it "defaults to true" do
with_dummy "dummy://localhost:1027" do |db|
Expand Down
1 change: 1 addition & 0 deletions spec/dummy_driver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class DummyDriver < DB::Driver
def initialize(connection, @query : String, @prepared : Bool)
@params = Hash(Int32 | String, DB::Any).new
super(connection)
raise query if query == "syntax error"
end

protected def perform_query(args : Enumerable)
Expand Down
10 changes: 8 additions & 2 deletions src/db/pool_prepared_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ module DB
end

# builds a statement over a real connection
# the conneciton is registered in `@connections`
# the connection is registered in `@connections`
private def build_statement
clean_connections
conn, existing = @db.checkout_some(@connections)
begin
stmt = conn.prepared.build(@query)
rescue ex
conn.release
raise ex
end
@connections << WeakRef.new(conn) unless existing
conn.prepared.build(@query)
stmt
end

private def clean_connections
Expand Down
8 changes: 7 additions & 1 deletion src/db/pool_unprepared_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ module DB

# builds a statement over a real connection
private def build_statement
@db.pool.checkout.unprepared.build(@query)
conn = @db.pool.checkout
begin
conn.unprepared.build(@query)
rescue ex
conn.release
raise ex
end
end
end
end