Skip to content

Implement connection pool. #451

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

Merged
merged 22 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6881560
Add connection pool for concurrent access
kdubb Nov 29, 2015
42ebd18
Add delegate to connection pool
kdubb Nov 29, 2015
68f46d5
Better names for protocols and classes
kdubb Apr 16, 2016
66b82b0
Use vfs for exclusivity
kdubb Apr 16, 2016
ccc20e5
Added alternate concurrency test
kdubb Apr 16, 2016
18f612f
Fix imports in ConnectionPool.swift
kdubb Apr 16, 2016
2a5908b
Rename `DBConnection` to `DirectConnection`
kdubb Apr 17, 2016
9548d17
Replace pool delegate with setup closures
kdubb Apr 18, 2016
ffb9274
Fix writable connection initialization
kdubb Apr 19, 2016
b7a0eb1
Cleanup connection pool tests
kdubb Apr 19, 2016
6df0272
Merge branch 'connection_pool' of https://github.com/reTXT/SQLite.swi…
nickmshelley Jun 16, 2016
960309e
Implement connection limit.
nickmshelley Jun 16, 2016
6052c31
Fix import problem for standalone.
nickmshelley Jun 22, 2016
e32db09
Shorten testConcurrentAccess2 time as it is making the travis tests t…
nickmshelley Jun 22, 2016
2fd69b8
Tests are still taking too long.
nickmshelley Jun 23, 2016
6d556b6
Merge branch 'master' into connection-pool
nickmshelley Jun 23, 2016
a688d18
Clean up some spacing.
nickmshelley Jul 18, 2016
a1121c4
Change ConnectionPool API and get rid of Connection protocol.
nickmshelley Jul 18, 2016
d2721ed
Merge branch 'master' into connection-pool
nickmshelley Jul 18, 2016
476cdc5
Make sure every thread has at least one read.
nickmshelley Jul 18, 2016
c7db919
Add connection pool documentation.
nickmshelley Jul 19, 2016
66b61dd
Use the right variable names.
nickmshelley Jul 19, 2016
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
30 changes: 29 additions & 1 deletion Documentation/Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Read-Only Databases](#read-only-databases)
- [In-Memory Databases](#in-memory-databases)
- [Thread-Safety](#thread-safety)
- [Connection Pools](#connection-pools)
- [Building Type-Safe SQL](#building-type-safe-sql)
- [Expressions](#expressions)
- [Compound Expressions](#compound-expressions)
Expand Down Expand Up @@ -251,7 +252,7 @@ Every Connection comes equipped with its own serial queue for statement executio

If you maintain multiple connections for a single database, consider setting a timeout (in seconds) and/or a busy handler:

```swift
``` swift
db.busyTimeout = 5

db.busyHandler({ tries in
Expand All @@ -265,6 +266,33 @@ db.busyHandler({ tries in
> _Note:_ The default timeout is 0, so if you see `database is locked` errors, you may be trying to access the same database simultaneously from multiple connections.


### Connection Pools

Connection pools use SQLite WAL mode to allow concurrent reads and writes, which can increase performance. Connection pools are created similar to connections:

``` swift
let pool = try ConnectionPool("path/to/db.sqlite3")
```

Writes are done inside of a readWrite block:

``` swift
pool.readWrite { connection in
try connection.run(users.insert(email <- "alice@mac.com", name <- "Alice"))
}
```

Reads are done inside of a read block:

``` swift
pool.read { connection in
for user in try connection.prepare(users) {
print("id: \(user[id]), email: \(user[email]), name: \(user[name])")
}
}
```


## Building Type-Safe SQL

SQLite.swift comes with a typed expression layer that directly maps [Swift types](https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/SwiftStandardLibraryReference/) to their [SQLite counterparts](https://www.sqlite.org/datatype3.html).
Expand Down
38 changes: 33 additions & 5 deletions SQLite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@
03A65E941C6BB3030062603F /* ValueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE247B331C3F142E00AE3E12 /* ValueTests.swift */; };
03A65E951C6BB3030062603F /* TestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE247B161C3F127200AE3E12 /* TestHelpers.swift */; };
03A65E971C6BB3210062603F /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 03A65E961C6BB3210062603F /* libsqlite3.tbd */; };
AA780B3D1CC201A700E0E95E /* ConnectionPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3B1CC201A700E0E95E /* ConnectionPool.swift */; };
AA780B3E1CC201A700E0E95E /* Dispatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3C1CC201A700E0E95E /* Dispatcher.swift */; };
AA780B411CC202C800E0E95E /* ConnectionPoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3F1CC202B000E0E95E /* ConnectionPoolTests.swift */; };
AA780B421CC202C900E0E95E /* ConnectionPoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3F1CC202B000E0E95E /* ConnectionPoolTests.swift */; };
AA780B431CC202CA00E0E95E /* ConnectionPoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3F1CC202B000E0E95E /* ConnectionPoolTests.swift */; };
AA780B441CC202F300E0E95E /* ConnectionPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3B1CC201A700E0E95E /* ConnectionPool.swift */; };
AA780B451CC202F300E0E95E /* Dispatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3C1CC201A700E0E95E /* Dispatcher.swift */; };
AA780B461CC202F400E0E95E /* ConnectionPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3B1CC201A700E0E95E /* ConnectionPool.swift */; };
AA780B471CC202F400E0E95E /* Dispatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3C1CC201A700E0E95E /* Dispatcher.swift */; };
AA780B481CC202F500E0E95E /* ConnectionPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3B1CC201A700E0E95E /* ConnectionPool.swift */; };
AA780B491CC202F500E0E95E /* Dispatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA780B3C1CC201A700E0E95E /* Dispatcher.swift */; };
19A1717B10CC941ACB5533D6 /* FTS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1730E4390C775C25677D1 /* FTS5.swift */; };
19A171E6FA242F72A308C594 /* FTS5Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1721B8984686B9963B45D /* FTS5Tests.swift */; };
19A17254FBA7894891F7297B /* FTS5Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1721B8984686B9963B45D /* FTS5Tests.swift */; };
Expand Down Expand Up @@ -170,25 +181,28 @@
39548A6D1CA63C740003E3B5 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
39548A6F1CA63C740003E3B5 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
A121AC451CA35C79005A31D1 /* SQLite.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLite.framework; sourceTree = BUILT_PRODUCTS_DIR; };
AA780B3B1CC201A700E0E95E /* ConnectionPool.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = ConnectionPool.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
AA780B3C1CC201A700E0E95E /* Dispatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Dispatcher.swift; sourceTree = "<group>"; };
AA780B3F1CC202B000E0E95E /* ConnectionPoolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionPoolTests.swift; sourceTree = "<group>"; };
EE247AD31C3F04ED00AE3E12 /* SQLite.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLite.framework; sourceTree = BUILT_PRODUCTS_DIR; };
EE247AD61C3F04ED00AE3E12 /* SQLite.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQLite.h; sourceTree = "<group>"; };
EE247AD81C3F04ED00AE3E12 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
EE247ADD1C3F04ED00AE3E12 /* SQLiteTests iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SQLiteTests iOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
EE247AE41C3F04ED00AE3E12 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
EE247AEE1C3F06E900AE3E12 /* Blob.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Blob.swift; sourceTree = "<group>"; };
EE247AEF1C3F06E900AE3E12 /* Connection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Connection.swift; sourceTree = "<group>"; };
EE247AEF1C3F06E900AE3E12 /* Connection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Connection.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
EE247AF01C3F06E900AE3E12 /* fts3_tokenizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = fts3_tokenizer.h; sourceTree = "<group>"; };
EE247AF11C3F06E900AE3E12 /* SQLite-Bridging.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "SQLite-Bridging.m"; sourceTree = "<group>"; };
EE247AF21C3F06E900AE3E12 /* Statement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Statement.swift; sourceTree = "<group>"; };
EE247AF21C3F06E900AE3E12 /* Statement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Statement.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
EE247AF31C3F06E900AE3E12 /* Value.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Value.swift; sourceTree = "<group>"; };
EE247AF51C3F06E900AE3E12 /* FTS4.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS4.swift; sourceTree = "<group>"; };
EE247AF51C3F06E900AE3E12 /* FTS4.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = FTS4.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
EE247AF61C3F06E900AE3E12 /* R*Tree.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "R*Tree.swift"; sourceTree = "<group>"; };
EE247AF71C3F06E900AE3E12 /* Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Foundation.swift; sourceTree = "<group>"; };
EE247AF81C3F06E900AE3E12 /* Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = "<group>"; };
EE247AFA1C3F06E900AE3E12 /* AggregateFunctions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AggregateFunctions.swift; sourceTree = "<group>"; };
EE247AFB1C3F06E900AE3E12 /* Collation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Collation.swift; sourceTree = "<group>"; };
EE247AFC1C3F06E900AE3E12 /* CoreFunctions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreFunctions.swift; sourceTree = "<group>"; };
EE247AFD1C3F06E900AE3E12 /* CustomFunctions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomFunctions.swift; sourceTree = "<group>"; };
EE247AFD1C3F06E900AE3E12 /* CustomFunctions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = CustomFunctions.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
EE247AFE1C3F06E900AE3E12 /* Expression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Expression.swift; sourceTree = "<group>"; };
EE247AFF1C3F06E900AE3E12 /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
EE247B001C3F06E900AE3E12 /* Query.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Query.swift; sourceTree = "<group>"; };
Expand All @@ -198,7 +212,7 @@
EE247B181C3F134A00AE3E12 /* SetterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetterTests.swift; sourceTree = "<group>"; };
EE247B1A1C3F137700AE3E12 /* AggregateFunctionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AggregateFunctionsTests.swift; sourceTree = "<group>"; };
EE247B1B1C3F137700AE3E12 /* BlobTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlobTests.swift; sourceTree = "<group>"; };
EE247B1D1C3F137700AE3E12 /* ConnectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionTests.swift; sourceTree = "<group>"; };
EE247B1D1C3F137700AE3E12 /* ConnectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = ConnectionTests.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
EE247B1E1C3F137700AE3E12 /* CoreFunctionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreFunctionsTests.swift; sourceTree = "<group>"; };
EE247B1F1C3F137700AE3E12 /* CustomFunctionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomFunctionsTests.swift; sourceTree = "<group>"; };
EE247B201C3F137700AE3E12 /* ExpressionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpressionTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -398,6 +412,7 @@
EE247AE11C3F04ED00AE3E12 /* SQLiteTests */ = {
isa = PBXGroup;
children = (
AA780B3F1CC202B000E0E95E /* ConnectionPoolTests.swift */,
EE247B1A1C3F137700AE3E12 /* AggregateFunctionsTests.swift */,
EE247B1B1C3F137700AE3E12 /* BlobTests.swift */,
EE247B1D1C3F137700AE3E12 /* ConnectionTests.swift */,
Expand All @@ -422,6 +437,8 @@
EE247AED1C3F06E900AE3E12 /* Core */ = {
isa = PBXGroup;
children = (
AA780B3B1CC201A700E0E95E /* ConnectionPool.swift */,
AA780B3C1CC201A700E0E95E /* Dispatcher.swift */,
EE91808D1C46E5230038162A /* SQLite-Bridging.h */,
EE247AEE1C3F06E900AE3E12 /* Blob.swift */,
EE247AEF1C3F06E900AE3E12 /* Connection.swift */,
Expand Down Expand Up @@ -792,9 +809,11 @@
03A65E741C6BB2DA0062603F /* Helpers.swift in Sources */,
03A65E831C6BB2FB0062603F /* Operators.swift in Sources */,
03A65E851C6BB2FB0062603F /* Schema.swift in Sources */,
AA780B471CC202F400E0E95E /* Dispatcher.swift in Sources */,
03A65E841C6BB2FB0062603F /* Query.swift in Sources */,
03A65E7C1C6BB2F70062603F /* FTS4.swift in Sources */,
03A65E771C6BB2E60062603F /* Connection.swift in Sources */,
AA780B461CC202F400E0E95E /* ConnectionPool.swift in Sources */,
03A65E7E1C6BB2FB0062603F /* AggregateFunctions.swift in Sources */,
19A17EC0D68BA8C03288ADF7 /* FTS5.swift in Sources */,
);
Expand All @@ -805,6 +824,7 @@
buildActionMask = 2147483647;
files = (
03A65E881C6BB3030062603F /* BlobTests.swift in Sources */,
AA780B431CC202CA00E0E95E /* ConnectionPoolTests.swift in Sources */,
03A65E901C6BB3030062603F /* R*TreeTests.swift in Sources */,
03A65E941C6BB3030062603F /* ValueTests.swift in Sources */,
03A65E8F1C6BB3030062603F /* QueryTests.swift in Sources */,
Expand All @@ -827,6 +847,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
AA780B491CC202F500E0E95E /* Dispatcher.swift in Sources */,
AA780B481CC202F500E0E95E /* ConnectionPool.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -848,9 +870,11 @@
EE247B081C3F06E900AE3E12 /* Value.swift in Sources */,
EE247B121C3F06E900AE3E12 /* Operators.swift in Sources */,
EE247B141C3F06E900AE3E12 /* Schema.swift in Sources */,
AA780B3E1CC201A700E0E95E /* Dispatcher.swift in Sources */,
EE247B131C3F06E900AE3E12 /* Query.swift in Sources */,
EE247B061C3F06E900AE3E12 /* SQLite-Bridging.m in Sources */,
EE247B071C3F06E900AE3E12 /* Statement.swift in Sources */,
AA780B3D1CC201A700E0E95E /* ConnectionPool.swift in Sources */,
EE247B0D1C3F06E900AE3E12 /* AggregateFunctions.swift in Sources */,
19A1717B10CC941ACB5533D6 /* FTS5.swift in Sources */,
);
Expand All @@ -861,6 +885,7 @@
buildActionMask = 2147483647;
files = (
EE247B261C3F137700AE3E12 /* CoreFunctionsTests.swift in Sources */,
AA780B411CC202C800E0E95E /* ConnectionPoolTests.swift in Sources */,
EE247B291C3F137700AE3E12 /* FTS4Tests.swift in Sources */,
EE247B191C3F134A00AE3E12 /* SetterTests.swift in Sources */,
EE247B311C3F141E00AE3E12 /* SchemaTests.swift in Sources */,
Expand Down Expand Up @@ -897,9 +922,11 @@
EE247B641C3F3FDB00AE3E12 /* Helpers.swift in Sources */,
EE247B721C3F3FEC00AE3E12 /* Operators.swift in Sources */,
EE247B741C3F3FEC00AE3E12 /* Schema.swift in Sources */,
AA780B451CC202F300E0E95E /* Dispatcher.swift in Sources */,
EE247B731C3F3FEC00AE3E12 /* Query.swift in Sources */,
EE247B6B1C3F3FEC00AE3E12 /* FTS4.swift in Sources */,
EE247B661C3F3FEC00AE3E12 /* Connection.swift in Sources */,
AA780B441CC202F300E0E95E /* ConnectionPool.swift in Sources */,
EE247B6D1C3F3FEC00AE3E12 /* AggregateFunctions.swift in Sources */,
19A1750CEE9B05267995CF3D /* FTS5.swift in Sources */,
);
Expand All @@ -910,6 +937,7 @@
buildActionMask = 2147483647;
files = (
EE247B561C3F3FC700AE3E12 /* CoreFunctionsTests.swift in Sources */,
AA780B421CC202C900E0E95E /* ConnectionPoolTests.swift in Sources */,
EE247B5A1C3F3FC700AE3E12 /* OperatorsTests.swift in Sources */,
EE247B541C3F3FC700AE3E12 /* BlobTests.swift in Sources */,
EE247B5D1C3F3FC700AE3E12 /* SchemaTests.swift in Sources */,
Expand Down
Loading