@@ -39,6 +39,7 @@ public final class ConnectionPool {
3939 private var unavailableReadConnections = [ DirectConnection] ( )
4040 private let lockQueue : dispatch_queue_t
4141 private var writeConnection : DirectConnection !
42+ private let connectionSemaphore = dispatch_semaphore_create ( 5 )
4243
4344 public var foreignKeys : Bool {
4445 get {
@@ -91,6 +92,7 @@ public final class ConnectionPool {
9192 self . pool. unavailableReadConnections. removeAtIndex ( index)
9293 }
9394 self . pool. availableReadConnections. append ( self . connection)
95+ dispatch_semaphore_signal ( self . pool. connectionSemaphore)
9496 }
9597 }
9698
@@ -151,44 +153,41 @@ public final class ConnectionPool {
151153
152154 var borrowed : BorrowedConnection !
153155
154- repeat {
156+ dispatch_semaphore_wait ( connectionSemaphore, DISPATCH_TIME_FOREVER)
157+ dispatch_sync ( lockQueue) {
155158
156- dispatch_sync ( lockQueue) {
157-
158- // Ensure database is open
159- self . writable
159+ // Ensure database is open
160+ self . writable
161+
162+ let connection : DirectConnection
163+
164+ if let availableConnection = self . availableReadConnections. popLast ( ) {
165+ connection = availableConnection
166+ }
167+ else {
160168
161- let connection : DirectConnection
169+ let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX
162170
163- if let availableConnection = self . availableReadConnections. popLast ( ) {
164- connection = availableConnection
165- }
166- else {
167-
168- let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX
169-
170- connection = try ! DirectConnection ( self . location, flags: flags, dispatcher: ImmediateDispatcher ( ) , vfsName: vfsName)
171- connection. busyTimeout = 2
171+ connection = try ! DirectConnection ( self . location, flags: flags, dispatcher: ImmediateDispatcher ( ) , vfsName: vfsName)
172+ connection. busyTimeout = 2
172173
173- for (type, setupProcessor) in self . internalSetup {
174- if type == . WriteAheadLogging {
175- continue
176- }
177- try ! setupProcessor ( connection)
178- }
179-
180- for setupProcessor in self . setup {
181- try ! setupProcessor ( connection)
174+ for (type, setupProcessor) in self . internalSetup {
175+ if type == . WriteAheadLogging {
176+ continue
182177 }
183-
178+ try ! setupProcessor ( connection )
184179 }
185180
186- self . unavailableReadConnections. append ( connection)
181+ for setupProcessor in self . setup {
182+ try ! setupProcessor ( connection)
183+ }
187184
188- borrowed = BorrowedConnection ( pool: self , connection: connection)
189185 }
190186
191- } while borrowed == nil
187+ self . unavailableReadConnections. append ( connection)
188+
189+ borrowed = BorrowedConnection ( pool: self , connection: connection)
190+ }
192191
193192 return borrowed
194193 }
0 commit comments