Skip to content

Commit 7b55b3e

Browse files
Jonathan HammIgor Sereda [ALM Works]
authored andcommitted
Merged in issue-90-shared-cache (pull request #10)
Issue 90 shared cache Approved-by: Igor Sereda [ALM Works] <sereda@almworks.com>
2 parents 33fbe7e + 67a3a4d commit 7b55b3e

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

java/com/almworks/sqlite4java/SQLiteConnection.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public final class SQLiteConnection {
5959
*/
6060
private final File myFile;
6161

62+
/**
63+
* Flag to preserve the cache shared mode when connection was created
64+
*/
65+
private final boolean mySharedCacheMemoryConnection;
66+
6267
/**
6368
* An incremental number of the instance, used for debugging purposes.
6469
*/
@@ -168,15 +173,21 @@ public final class SQLiteConnection {
168173
*/
169174
private int myOpenFlags;
170175

176+
private SQLiteConnection(File dbfile, boolean sharedCacheMemoryConnection) {
177+
assert dbfile == null || !sharedCacheMemoryConnection : dbfile + " " + sharedCacheMemoryConnection;
178+
myFile = dbfile;
179+
mySharedCacheMemoryConnection = sharedCacheMemoryConnection;
180+
Internal.logInfo(this, "instantiated [" + myFile + "]");
181+
}
182+
171183
/**
172184
* Creates a connection to the database located in the specified file.
173185
* Database is not opened by the constructor, and the calling thread is insignificant.
174186
*
175187
* @param dbfile database file, or null to create an in-memory database
176188
*/
177189
public SQLiteConnection(File dbfile) {
178-
myFile = dbfile;
179-
Internal.logInfo(this, "instantiated [" + myFile + "]");
190+
this(dbfile, false);
180191
}
181192

182193
/**
@@ -186,7 +197,21 @@ public SQLiteConnection(File dbfile) {
186197
* @see #SQLiteConnection(java.io.File)
187198
*/
188199
public SQLiteConnection() {
189-
this(null);
200+
this(null, false);
201+
}
202+
203+
/**
204+
* <p>Creates a connection to an in-memory temporary database, allowing the user to set the "shared cache" flag.
205+
* All in-memory databases opened with {@code sharedCache} equal to {@code true} will share the same data.
206+
* </p>
207+
*
208+
* <p>Database is not opened by the constructor, and the calling thread is insignificant.</p>
209+
*
210+
* @see #SQLiteConnection()
211+
* @see <a href="https://www.sqlite.org/sharedcache.html">Shared Cache in SQLite</a>
212+
*/
213+
public SQLiteConnection(boolean sharedCache) {
214+
this(null, sharedCache);
190215
}
191216

192217
/**
@@ -1586,7 +1611,14 @@ private void configureConnection(SWIGTYPE_p_sqlite3 handle) {
15861611
}
15871612

15881613
private String getSqliteDbName() {
1589-
return myFile == null ? ":memory:" : myFile.getAbsolutePath();
1614+
if (myFile == null) {
1615+
if (mySharedCacheMemoryConnection) {
1616+
return "file::memory:?cache=shared";
1617+
} else {
1618+
return ":memory:";
1619+
}
1620+
}
1621+
return myFile.getAbsolutePath();
15901622
}
15911623

15921624
int getStatementCount() {

test/com/almworks/sqlite4java/SQLiteConnectionTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import java.util.logging.Level;
66
import java.util.logging.Logger;
77

8+
import static com.almworks.sqlite4java.SQLiteConstants.SQLITE_OPEN_READWRITE;
9+
import static com.almworks.sqlite4java.SQLiteConstants.SQLITE_OPEN_URI;
10+
811
public class SQLiteConnectionTests extends SQLiteConnectionFixture {
912
public void testOpenFile() throws SQLiteException {
1013
SQLiteConnection connection = fileDb();
@@ -211,6 +214,35 @@ public void testIsReadOnly() throws Exception {
211214
}
212215
}
213216

217+
public void testSharedCacheShouldPass() throws Exception {
218+
SQLiteConnection connection1 = new SQLiteConnection(true);
219+
connection1.openV2(SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI);
220+
connection1.exec("create table test(id varchar primary key, value varchar)");
221+
connection1.exec("insert into test(id, value) values('id1', 'value')");
222+
SQLiteConnection connection2 = new SQLiteConnection(true);
223+
connection2.openV2(SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI);
224+
connection2.exec("select * from test");
225+
connection1.dispose();
226+
connection2.dispose();
227+
}
228+
229+
public void testSharedCacheShouldFail() throws Exception {
230+
try {
231+
SQLiteConnection connection1 = new SQLiteConnection();
232+
connection1.openV2(SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI);
233+
connection1.exec("create table test(id varchar primary key, value varchar)");
234+
connection1.exec("insert into test(id, value) values('id1', 'value')");
235+
SQLiteConnection connection2 = new SQLiteConnection();
236+
connection2.openV2(SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI);
237+
connection2.exec("select * from test");
238+
connection1.dispose();
239+
connection2.dispose();
240+
throw new Exception("should throw SQLiteException");
241+
} catch (SQLiteException e) {
242+
//norm
243+
}
244+
}
245+
214246
/**
215247
* Test sqlite3_db_cacheflush(). This is an attempt to produce a state where the flush occurs
216248
* during a write-transaction with dirty pages and an exclusive lock. Producing this state

0 commit comments

Comments
 (0)