public class ThreadSafeDatabase
Confines a database and its cursors to a single thread.
A Database is not thread safe, and neither are the cursors it hands out. Wrapping one in this
class routes every call through one worker thread, so several application threads can share a
connection without coordinating.
Database db = new ThreadSafeDatabase(Database.openOrCreate("shared.db"));
The cost is that every call is a thread handoff, so a tight loop over a large result set is meaningfully slower than using a connection per thread. Prefer one database per thread when the threads do not actually need to share state.
This class used to be deprecated, on the grounds that platform specific behaviour had defeated it. That behaviour has since been fixed: the iOS port no longer closes SQLite handles from the garbage collector thread, and it opens each connection in serialised mode rather than trying to configure the whole process.
Constructors
public ThreadSafeDatabase(Database db) | Wraps the given database with a threadsafe version |
Methods
Inherited fields
Inherited methods
From Database
isCustomPathSupported, isLegacyBehavior, setLegacyBehavior, openOrCreate, exists, delete, isDatabaseBeingDeleted, openDatabaseCount, getDatabasePath, openOrCreate, isEncryptionSupported, isEncrypted, encrypt, decrypt, forgetManagedKey, beforeFirst, count, isBlobQueryParameterSupported, wasNull, supportsWasNull, toPragmaLiteral, noteScriptTransactionControl, hasAttachments, reserveAttachments, reserveAttachments, requireAttachmentsHeld, noteConnectionClosed, noteFirstStatementTransactionControl, noteEngineTransactionState, transactionControlKeyword, beginTransactionMode, requireQueryStatement, normalizeDatabaseKey, normalizeDatabasePathKey, registerOpenDatabase, releaseOpenDatabase, requireSoleConnectionForKeyChange, releaseKeyChangeClaim, checkNoTransactionForKeyChange, supportsNestedTransactions, checkBeginTransaction, checkEndTransaction, markTransactionEnded, abandonFailedCommit, coerceToText
Constructor details
ThreadSafeDatabase
public ThreadSafeDatabase(Database db)Parameters
dbDatabase- the database
Method details
getThread
public EasyThread getThread()Returns
beginTransaction
public void beginTransaction()
throws IOExceptionStarts a transaction.
Transactions are flat. Calling this while a transaction is already open throws, and committing or rolling back returns the connection to autocommit. Closing a database with an open transaction rolls it back.
Throws
IOException- if the database is not open, or a transaction is already in progress
commitTransaction
public void commitTransaction()
throws IOExceptionCommits current transaction
NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.
Throws
IOException- if database is not opened or transaction was not started
rollbackTransaction
public void rollbackTransaction()
throws IOExceptionRolls back current transaction
NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.
Throws
IOException- if database is not opened or transaction was not started
isInTransaction
public boolean isInTransaction()Returns
#beginTransaction() and its commit or rollbackchangeKey
public void changeKey(DatabaseConfig config)
throws IOExceptionChanges the key of this open database, or removes it entirely.
Passing a plaintext config decrypts the database. The engine performs the conversion as a
single transaction and preserves schema metadata such as PRAGMA user_version.
Ports that support encryption override this. The default implementation reports that the
platform cannot do it; it is deliberately concrete rather than abstract, because Database
is public and is subclassed outside this repository.
Parameters
configDatabaseConfig- the new key, or
DatabaseConfig#plain()to decrypt
Throws
IOException- if the key cannot be changed
close
public void close()
throws IOExceptionCloses the database on the worker, and shuts the worker down.
Idempotent, and synchronous: it returns with the database closed, so a delete() on the
next line does not race it. If the worker was stopped from outside – getThread() is
public, and both kill() and killWhenIdle() on it are calls anybody can make – this
waits for the work that worker had already accepted before closing the database itself,
rather than closing it underneath an operation that is still running.
Throws
execute
public void execute(String sql)
throws IOExceptionParameters
sqlString- the sql to execute
Throws
execute
public void execute(String sql, String[] params)
throws IOExceptionParameters
sqlString- the sql to execute
paramsString[]- to bind to the query where the ‘?’ exists
Throws
executeQuery
public Cursor executeQuery(String sql, String[] params)
throws IOExceptionParameters
sqlString- the sql to execute
paramsString[]- to bind to the query where the ‘?’ exists
Returns
Throws
executeQuery
public Cursor executeQuery(String sql)
throws IOExceptionParameters
sqlString- the sql to execute
Returns
Throws
executeQuery
public Cursor executeQuery(String sql, Object... params)
throws IOExceptionParameters
sqlString- the sql to execute
paramsObject...- to bind to the query where the ‘?’ exists, supported object types are String, byte[], Double, Long and null
Returns
Throws
execute
public void execute(String sql, Object... params)
throws IOExceptionParameters
sqlString- the sql to execute
paramsObject...- to bind to the query where the ‘?’ exists, supported object types are String, byte[], Double, Long and null