mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Add mutex to protect @connections : Set
This commit is contained in:
parent
eaddae7d71
commit
1d08caa8a5
1 changed files with 10 additions and 3 deletions
|
@ -7,6 +7,7 @@ module DB
|
|||
class PoolPreparedStatement < PoolStatement
|
||||
# connections where the statement was prepared
|
||||
@connections = Set(WeakRef(Connection)).new
|
||||
@mutex = Mutex.new
|
||||
|
||||
def initialize(db : Database, query : String)
|
||||
super
|
||||
|
@ -25,21 +26,27 @@ module DB
|
|||
|
||||
# WHAT-IF the connection is busy? Should each statement be able to
|
||||
# deallocate itself when the connection is free.
|
||||
@mutex.synchronize do
|
||||
@connections.clear
|
||||
end
|
||||
end
|
||||
|
||||
# builds a statement over a real connection
|
||||
# the connection is registered in `@connections`
|
||||
private def build_statement : Statement
|
||||
clean_connections
|
||||
conn = uninitialized Connection
|
||||
existing = false
|
||||
@mutex.synchronize do
|
||||
conn, existing = @db.checkout_some(@connections)
|
||||
end
|
||||
begin
|
||||
stmt = conn.prepared.build(@query)
|
||||
rescue ex
|
||||
conn.release
|
||||
raise ex
|
||||
end
|
||||
@connections << WeakRef.new(conn) unless existing
|
||||
@mutex.synchronize { @connections << WeakRef.new(conn) } unless existing
|
||||
stmt
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue