mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Merge pull request #65 from crisward/patch-1
Release connection when query cannot be built
This commit is contained in:
commit
f4c1c3626b
4 changed files with 38 additions and 3 deletions
|
@ -149,6 +149,28 @@ describe DB::Database do
|
||||||
end
|
end
|
||||||
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
|
describe "prepared_statements connection option" do
|
||||||
it "defaults to true" do
|
it "defaults to true" do
|
||||||
with_dummy "dummy://localhost:1027" do |db|
|
with_dummy "dummy://localhost:1027" do |db|
|
||||||
|
|
|
@ -99,6 +99,7 @@ class DummyDriver < DB::Driver
|
||||||
def initialize(connection, @query : String, @prepared : Bool)
|
def initialize(connection, @query : String, @prepared : Bool)
|
||||||
@params = Hash(Int32 | String, DB::Any).new
|
@params = Hash(Int32 | String, DB::Any).new
|
||||||
super(connection)
|
super(connection)
|
||||||
|
raise query if query == "syntax error"
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def perform_query(args : Enumerable)
|
protected def perform_query(args : Enumerable)
|
||||||
|
|
|
@ -29,12 +29,18 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# builds a statement over a real connection
|
# builds a statement over a real connection
|
||||||
# the conneciton is registered in `@connections`
|
# the connection is registered in `@connections`
|
||||||
private def build_statement
|
private def build_statement
|
||||||
clean_connections
|
clean_connections
|
||||||
conn, existing = @db.checkout_some(@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
|
@connections << WeakRef.new(conn) unless existing
|
||||||
conn.prepared.build(@query)
|
stmt
|
||||||
end
|
end
|
||||||
|
|
||||||
private def clean_connections
|
private def clean_connections
|
||||||
|
|
|
@ -15,7 +15,13 @@ module DB
|
||||||
|
|
||||||
# builds a statement over a real connection
|
# builds a statement over a real connection
|
||||||
private def build_statement
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue