25
25
import Foundation
26
26
27
27
28
+ private let vfsName = " unix-excl "
29
+
30
+
28
31
/// Connection pool delegate
29
32
public protocol ConnectionPoolDelegate {
30
33
@@ -44,15 +47,13 @@ public final class ConnectionPool {
44
47
private var unavailableReadConnections = [ DBConnection] ( )
45
48
private let lockQueue : dispatch_queue_t
46
49
private var writeConnection : DBConnection !
47
- private let writeQueue : dispatch_queue_t
48
50
49
51
public var delegate : ConnectionPoolDelegate ?
50
52
51
53
public init ( _ location: DBConnection . Location ) throws {
52
54
self . location = location
53
55
self . lockQueue = dispatch_queue_create ( " SQLite.ConnectionPool.Lock " , DISPATCH_QUEUE_SERIAL)
54
- self . writeQueue = dispatch_queue_create ( " SQLite.ConnectionPool.Write " , DISPATCH_QUEUE_SERIAL)
55
- try writable. execute ( " PRAGMA locking_mode = EXCLUSIVE; PRAGMA journal_mode = WAL; " )
56
+ try writable. execute ( " PRAGMA journal_mode = WAL; " )
56
57
}
57
58
58
59
public var totalReadableConnectionCount : Int {
@@ -114,12 +115,16 @@ public final class ConnectionPool {
114
115
// Acquires a read/write connection to the database
115
116
public var writable : DBConnection {
116
117
117
-
118
118
var writeConnectionInit = dispatch_once_t ( )
119
119
dispatch_once ( & writeConnectionInit) {
120
- let flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_WAL
121
- self . writeConnection = try ! DBConnection ( self . location, flags: flags, dispatcher: ReentrantDispatcher ( " SQLite.WriteConnection " ) )
120
+
121
+ let flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX
122
+ self . writeConnection = try ! DBConnection ( self . location, flags: flags, dispatcher: ReentrantDispatcher ( " SQLite.ConnectionPool.Write " ) , vfsName: vfsName)
122
123
self . writeConnection. busyTimeout = 2
124
+
125
+ if let delegate = self . delegate {
126
+ delegate. pool ( self , didAddConnection: self . writeConnection)
127
+ }
123
128
}
124
129
125
130
return writeConnection
@@ -141,8 +146,9 @@ public final class ConnectionPool {
141
146
}
142
147
else if self . delegate? . poolShouldAddConnection ( self ) ?? true {
143
148
144
- let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL
145
- connection = try ! DBConnection ( self . location, flags: flags, dispatcher: ImmediateDispatcher ( ) )
149
+ let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX
150
+
151
+ connection = try ! DBConnection ( self . location, flags: flags, dispatcher: ImmediateDispatcher ( ) , vfsName: vfsName)
146
152
connection. busyTimeout = 2
147
153
148
154
self . delegate? . pool ( self , didAddConnection: connection)
0 commit comments