public final class SQLiteConnectionPool
extends java.lang.Object
implements java.io.Closeable
At any given time, a connection is either owned by the pool, or it has been
acquired by a SQLiteSession. When the SQLiteSession is
finished with the connection it is using, it must return the connection
back to the pool.
The pool holds strong references to the connections it owns. However, it only holds weak references to the connections that sessions have acquired from it. Using weak references in the latter case ensures that the connection pool can detect when connections have been improperly abandoned so that it can create new connections to replace them if needed.
The connection pool is thread-safe (but the connections themselves are not).
This code attempts to maintain the invariant that opened connections are
always owned. Unfortunately that means it needs to handle exceptions
all over to ensure that broken connections get cleaned up. Most
operations invokving SQLite can throw SQLiteException or other
runtime exceptions. This is a bit of a pain to deal with because the compiler
cannot help us catch missing exception handling code.
The general rule for this file: If we are making calls out to
SQLiteConnection then we must be prepared to handle any
runtime exceptions it might throw at us. Note that out-of-memory
is an Error, not a RuntimeException. We don't trouble ourselves
handling out of memory because it is hard to do anything at all sensible then
and most likely the VM is about to crash.
| Modifier and Type | Field and Description |
|---|---|
static int |
CONNECTION_FLAG_INTERACTIVE
Connection flag: Connection is being used interactively.
|
static int |
CONNECTION_FLAG_PRIMARY_CONNECTION_AFFINITY
Connection flag: Primary connection affinity.
|
static int |
CONNECTION_FLAG_READ_ONLY
Connection flag: Read-only.
|
| Modifier and Type | Method and Description |
|---|---|
SQLiteConnection |
acquireConnection(java.lang.String sql,
int connectionFlags,
CancellationSignal cancellationSignal)
Acquires a connection from the pool.
|
void |
close()
Closes the connection pool.
|
void |
collectDbStats(java.util.ArrayList<SQLiteDebug.DbStats> dbStatsList)
Collects statistics about database connection memory usage.
|
void |
dump(Printer printer,
boolean verbose)
Dumps debugging information about this connection pool.
|
protected void |
finalize() |
static SQLiteConnectionPool |
open(SQLiteDatabaseConfiguration configuration,
SQLiteConnectionListener connectionListener)
Opens a connection pool for the specified database.
|
void |
reconfigure(SQLiteDatabaseConfiguration configuration)
Reconfigures the database configuration of the connection pool and all of its
connections.
|
void |
releaseConnection(SQLiteConnection connection)
Releases a connection back to the pool.
|
boolean |
shouldYieldConnection(SQLiteConnection connection,
int connectionFlags)
Returns true if the session should yield the connection due to
contention over available database connections.
|
java.lang.String |
toString() |
public static final int CONNECTION_FLAG_READ_ONLY
This flag indicates that the connection will only be used to perform read-only operations.
public static final int CONNECTION_FLAG_PRIMARY_CONNECTION_AFFINITY
This flag indicates that the primary connection is required. This flag helps support legacy applications that expect most data modifying operations to be serialized by locking the primary database connection. Setting this flag essentially implements the old "db lock" concept by preventing an operation from being performed until it can obtain exclusive access to the primary connection.
public static final int CONNECTION_FLAG_INTERACTIVE
This flag indicates that the connection is needed by the UI thread. The connection pool can use this flag to elevate the priority of the database connection request.
protected void finalize()
throws java.lang.Throwable
finalize in class java.lang.Objectjava.lang.Throwablepublic static SQLiteConnectionPool open(SQLiteDatabaseConfiguration configuration, SQLiteConnectionListener connectionListener)
configuration - The database configuration.SQLiteException - if a database error occurs.public void close()
When the connection pool is closed, it will refuse all further requests to acquire connections. All connections that are currently available in the pool are closed immediately. Any connections that are still in use will be closed as soon as they are returned to the pool.
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablejava.lang.IllegalStateException - if the pool has been closed.public void reconfigure(SQLiteDatabaseConfiguration configuration)
Configuration changes are propagated down to connections immediately if they are available or as soon as they are released. This includes changes that affect the size of the pool.
configuration - The new configuration.java.lang.IllegalStateException - if the pool has been closed.public SQLiteConnection acquireConnection(java.lang.String sql, int connectionFlags, CancellationSignal cancellationSignal)
The caller must call releaseConnection(com.couchbase.lite.internal.database.sqlite.SQLiteConnection) to release the connection
back to the pool when it is finished. Failure to do so will result
in much unpleasantness.
sql - If not null, try to find a connection that already has
the specified SQL statement in its prepared statement cache.connectionFlags - The connection request flags.cancellationSignal - A signal to cancel the operation in progress, or null if none.java.lang.IllegalStateException - if the pool has been closed.SQLiteException - if a database error occurs.OperationCanceledException - if the operation was canceled.public void releaseConnection(SQLiteConnection connection)
It is ok to call this method after the pool has closed, to release connections that were still in use at the time of closure.
connection - The connection to release. Must not be null.java.lang.IllegalStateException - if the connection was not acquired
from this pool or if it has already been released.public boolean shouldYieldConnection(SQLiteConnection connection, int connectionFlags)
connection - The connection owned by the session.connectionFlags - The connection request flags.java.lang.IllegalStateException - if the connection was not acquired
from this pool or if it has already been released.public void collectDbStats(java.util.ArrayList<SQLiteDebug.DbStats> dbStatsList)
dbStatsList - The list to populate.public void dump(Printer printer, boolean verbose)
printer - The printer to receive the dump, not null.verbose - True to dump more verbose information.public java.lang.String toString()
toString in class java.lang.Object