Skip to content

Commit 67a3a4d

Browse files
author
Igor Sereda
committed
minor updates and javadoc for shared-cache memory connection
1 parent b189a35 commit 67a3a4d

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

java/com/almworks/sqlite4java/SQLiteConnection.java

Lines changed: 25 additions & 11 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,10 +173,12 @@ public final class SQLiteConnection {
168173
*/
169174
private int myOpenFlags;
170175

171-
/**
172-
* Flag to preserve the cache shared mode when connection was created
173-
*/
174-
private boolean sharedCache;
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+
}
175182

176183
/**
177184
* Creates a connection to the database located in the specified file.
@@ -180,9 +187,7 @@ public final class SQLiteConnection {
180187
* @param dbfile database file, or null to create an in-memory database
181188
*/
182189
public SQLiteConnection(File dbfile) {
183-
this.sharedCache = false;
184-
myFile = dbfile;
185-
Internal.logInfo(this, "instantiated [" + myFile + "]");
190+
this(dbfile, false);
186191
}
187192

188193
/**
@@ -192,12 +197,21 @@ public SQLiteConnection(File dbfile) {
192197
* @see #SQLiteConnection(java.io.File)
193198
*/
194199
public SQLiteConnection() {
195-
this(null);
200+
this(null, false);
196201
}
197202

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+
*/
198213
public SQLiteConnection(boolean sharedCache) {
199-
this(null);
200-
this.sharedCache = sharedCache;
214+
this(null, sharedCache);
201215
}
202216

203217
/**
@@ -1598,7 +1612,7 @@ private void configureConnection(SWIGTYPE_p_sqlite3 handle) {
15981612

15991613
private String getSqliteDbName() {
16001614
if (myFile == null) {
1601-
if (sharedCache) {
1615+
if (mySharedCacheMemoryConnection) {
16021616
return "file::memory:?cache=shared";
16031617
} else {
16041618
return ":memory:";

0 commit comments

Comments
 (0)