@@ -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 () {
0 commit comments