History log of /frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
Revision Date Author Comments (<<< Hide modified files) (Show modified files >>>)
a7771df3696954f0e279407e8894a916a7cb26cc 08-May-2012 Jeff Brown <jeffbrown@google.com> Move CancellationSignal to android.os package.

Bug: 6427830
Change-Id: I39451bb1e1d4a8d976ed1c671234f0c8c61658dd
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
76070d1690ef9ba2675bdc34f1405968a2123112 19-Apr-2012 Jeff Brown <jeffbrown@google.com> Ensure that db connection reconfiguration can change the db.

If the db connection had been marked read-only, then certain
operations performed during reconfiguration, such as setting the
locale, could fail. So we need to make sure to clear the
read-only flag ahead of time.

Bug: 6296752
Change-Id: I98e19aff6386497b7fc321e04ca560fc1817a781
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
72eebb6b6d4b16bcba415236dd182a3ba9a72951 16-Apr-2012 Makoto Onuki <omakoto@google.com> Add # of changed rows to executeForChangedRowCount log

Change-Id: I240849f8e90490bbb6f73bbf7047e71254c83f0c
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
96496adb611ced49ed1c2c778c616d1f8a5d0e6b 23-Mar-2012 Jeff Brown <jeffbrown@google.com> Provide an API for enabling foreign key constraints.

Also provide a lifecycle method on SQLiteOpenHelper so that
applications can configure things like this before the onCreate,
onUpgrade, onDowngrade and onOpen callbacks run.

Change-Id: If3d1396720bd2e032dd9e034733fb1ff9a9733dd
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
47847f3f4dcf2a0dbea0bc0e4f02528e21d37a88 23-Mar-2012 Jeff Brown <jeffbrown@google.com> Support enabling WAL using a flag when DB is opened.

Using enableWriteAheadLogging() to enable WAL is inefficient because
we previously disabled WAL mode when the database was opened.
Switching from WAL to PERSIST then back to WAL is inefficient
and could slow down application launch time. It would be better
to leave the database in WAL mode when we open it to begin with.

To do that, we need to know ahead of time whether we will want to
have WAL enabled for the newly opened database.

Using this flag also reduces the chance that we will encounter
an error enabling WAL mode due to there being other open connections
to the database.

Bug: 6124556
Change-Id: I38ec7a528baeda9f1ef77e25e88b3ca4b6296200
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
d67c8c67899481682657d41a61f3846b8d77d165 22-Mar-2012 Jeff Brown <jeffbrown@google.com> Work around problems changing the database journal mode.

Because we always disable WAL mode when a database is first opened
(even if we intend to re-enable it), we can encounter problems if
there is another open connection to the database somewhere.
This can happen for a variety of reasons such as an application opening
the same database in multiple processes at the same time or if there is a
crashing content provider service that the ActivityManager has
removed from its registry but whose process hasn't quite died yet
by the time it is restarted in a new process.

If we don't change the journal mode, nothing really bad happens.
In the worst case, an application that enables WAL might not actually
get it, although it can still use connection pooling.

Bug: 6124556
Change-Id: Ia2ffdbbc8f82721b170f3bf71bd5242dfd56d9ac
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
1d9f742e001ed8280fa93fd9ba0b1125ce6d00ae 15-Mar-2012 Jeff Brown <jeffbrown@google.com> Port the SQLite locale setting code to Java.

Make the database opening code more robust in the case of
read-only database connections.

Check whether a PRAGMA needs to be issues before doing it.
Mostly it's harmless but it can grab a transaction on the
database unnecessarily.

Change-Id: Iab2cdc96c785e767f82966b00597e19337163f2f
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
03bd302aebbb77f4f95789a269c8a5463ac5a840 06-Mar-2012 Jeff Brown <jeffbrown@google.com> Don't close the database until all references released.

SQLiteDatabase.close() should call releaseReference() rather than
closing the database immediately. SQLiteDatabase should also hold
a reference to itself while performing certain operations to
ensure that they complete normally even if another thread closes
the database at the same time.

Fixed a couple of missing or redundant uses of acquireReference()
related to CursorWindows.

To be honest, the reference counting performed by SQLiteClosable should
not be needed, but we're stuck with it in the API.

Bug: 6104842
Change-Id: I3444a697409905d4a36b56418dc7766f5ba76b59
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
8dc3cc2e13b500e368f5ba1aacfaf0eddbce668c 02-Mar-2012 Jeff Brown <jeffbrown@google.com> Allow the SQLite sync mode to be set independently for WAL.

This change leaves the sync mode at FULL for both WAL and non-WAL
but makes it easy to change it for one but not the other.

To reduce the number of synchronous writes, it might make sense to
change the sync mode for non-WAL to NORMAL instead of FULL which
should be just as safe.

On the other hand, the sync mode for WAL should probably remain FULL
because there may be an impact on transaction durability otherwise.

Initial experiments show that there might not be a significant
performance benefit to using NORMAL, but we may revisit this later.

Change-Id: Ifcd55bedcfefa6600974c2295ca5d4163b408cbf
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
5936ff097eff2c736af2e43fd4a8f7db0ddcfb5a 01-Mar-2012 Jeff Brown <jeffbrown@google.com> Externalize more SQLite configuration options.

Moved more configuration into config.xml so we can tweak settings
like the default journal mode, WAL auto-checkpoint interval and
so on.

This change itself should not introduce any functional differences.

Change-Id: Id6c95fa25b116ce47e8ae49cd8a80d52b1c0dd80
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
4c1241df8f8b7fd5ec3dff6c7e0f66271248e76e 03-Feb-2012 Jeff Brown <jeffbrown@google.com> Rename CancellationSignal using preferred spelling.

Bug: 5943637
Change-Id: I12a339f285f4db58e79acb5fd8ec2fc1acda5265
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
75ea64fc54f328d37b115cfb1ded1e45c30380ed 26-Jan-2012 Jeff Brown <jeffbrown@google.com> Implement a cancelation mechanism for queries.

Added new API to enable cancelation of SQLite and content provider
queries by means of a CancelationSignal object. The application
creates a CancelationSignal object and passes it as an argument
to the query. The cancelation signal can then be used to cancel
the query while it is executing.

If the cancelation signal is raised before the query is executed,
then it is immediately terminated.

Change-Id: If2c76e9a7e56ea5e98768b6d4f225f0a1ca61c61
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
4b57553e693c9705e8363d3e0e9d881261b3e6fa 20-Jan-2012 Jeff Brown <jeffbrown@google.com> Initialize SQLite as part of the android runtime.

This ensures that the SQLite library is always correctly configured
and initialized before other framework or application code has
a chance to use it. This is important because initialization has
to happen at most once so we need to get it right and prevent races.

This change makes it possible to omit the SQLite auto-initialization
workaround from the SQLite library, potentially saving a few cycles
here and there.

Change-Id: Ifbed8685ee44aa1e9c0b391e233b0257fa738e4f
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
2a293b61cb0efbf24994d74ed980f58b820bb35a 19-Jan-2012 Jeff Brown <jeffbrown@google.com> Clean up SQLite debugging code.

Deleted a bunch of dead / useless code.
Raised number of logged operations in dumpsys dbinfo to 20.

Change-Id: I88344ff57a978f200c1f0172141d91e430caa1a9
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
a9be4154e8dac0de3db5ee42e878beb0639e70e6 19-Jan-2012 Jeff Brown <jeffbrown@google.com> Fix issues with reentrant SQLite triggers.

Modified SQLiteConnection and SQLiteSession to support
reentrant execution of SQLite operations, as might occur
when a custom function is invoked by a trigger.

Bug: 5884809
Change-Id: I253d828b2801bd06b1bbda7caa7da3f040a642bb
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java
e5360fbf3efe85427f7e7f59afe7bbeddb4949ac 01-Nov-2011 Jeff Brown <jeffbrown@google.com> Rewrite SQLite database wrappers.

The main theme of this change is encapsulation. This change
preserves all existing functionality but the implementation
is now much cleaner.

Instead of a "database lock", access to the database is treated
as a resource acquisition problem. If a thread's owns a database
connection, then it can access the database; otherwise, it must
acquire a database connection first, and potentially wait for other
threads to give up theirs. The SQLiteConnectionPool encapsulates
the details of how connections are created, configured, acquired,
released and disposed.

One new feature is that SQLiteConnectionPool can make scheduling
decisions about which thread should next acquire a database
connection when there is contention among threads. The factors
considered include wait queue ordering (fairness among peers),
whether the connection is needed for an interactive operation
(unfairness on behalf of the UI), and whether the primary connection
is needed or if any old connection will do. Thus one goal of the
new SQLiteConnectionPool is to improve the utilization of
database connections.

To emulate some quirks of the old "database lock," we introduce
the concept of the primary database connection. The primary
database connection is the one that is typically used to perform
write operations to the database. When a thread holds the primary
database connection, it effectively prevents other threads from
modifying the database (although they can still read). What's
more, those threads will block when they try to acquire the primary
connection, which provides the same kind of mutual exclusion
features that the old "database lock" had. (In truth, we
probably don't need to be requiring use of the primary database
connection in as many places as we do now, but we can seek to refine
that behavior in future patches.)

Another significant change is that native sqlite3_stmt objects
(prepared statements) are fully encapsulated by the SQLiteConnection
object that owns them. This ensures that the connection can
finalize (destroy) all extant statements that belong to a database
connection when the connection is closed. (In the original code,
this was very complicated because the sqlite3_stmt objects were
managed by SQLiteCompiledSql objects which had different lifetime
from the original SQLiteDatabase that created them. Worse, the
SQLiteCompiledSql finalizer method couldn't actually destroy the
sqlite3_stmt objects because it ran on the finalizer thread and
therefore could not guarantee that it could acquire the database
lock in order to do the work. This resulted in some rather
tortured logic involving a list of pending finalizable statements
and a high change of deadlocks or leaks.)

Because sqlite3_stmt objects never escape the confines of the
SQLiteConnection that owns them, we can also greatly simplify
the design of the SQLiteProgram, SQLiteQuery and SQLiteStatement
objects. They no longer have to wrangle a native sqlite3_stmt
object pointer and manage its lifecycle. So now all they do
is hold bind arguments and provide a fancy API.

All of the JNI glue related to managing database connections
and performing transactions is now bound to SQLiteConnection
(rather than being scattered everywhere). This makes sense because
SQLiteConnection owns the native sqlite3 object, so it is the
only class in the system that can interact with the native
SQLite database directly. Encapsulation for the win.

One particularly tricky part of this change is managing the
ownership of SQLiteConnection objects. At any given time,
a SQLiteConnection is either owned by a SQLiteConnectionPool
or by a SQLiteSession. SQLiteConnections should never be leaked,
but we handle that case too (and yell about it with CloseGuard).

A SQLiteSession object is responsible for acquiring and releasing
a SQLiteConnection object on behalf of a single thread as needed.
For example, the session acquires a connection when a transaction
begins and releases it when finished. If the session cannot
acquire a connection immediately, then the requested operation
blocks until a connection becomes available.

SQLiteSessions are thread-local. A SQLiteDatabase assigns a
distinct session to each thread that performs database operations.
This is very very important. First, it prevents two threads
from trying to use the same SQLiteConnection at the same time
(because two threads can't share the same session).
Second, it prevents a single thread from trying to acquire two
SQLiteConnections simultaneously from the same database (because
a single thread can't have two sessions for the same database which,
in addition to being greedy, could result in a deadlock).

There is strict layering between the various database objects,
objects at lower layers are not aware of objects at higher layers.
Moreover, objects at higher layers generally own objects at lower
layers and are responsible for ensuring they are properly disposed
when no longer needed (good for the environment).

API layer: SQLiteDatabase, SQLiteProgram, SQLiteQuery, SQLiteStatement.
Session layer: SQLiteSession.
Connection layer: SQLiteConnectionPool, SQLiteConnection.
Native layer: JNI glue.

By avoiding cyclic dependencies between layers, we make the
architecture much more intelligible, maintainable and robust.

Finally, this change adds a great deal of new debugging information.
It is now possible to view a list of the most recent database
operations including how long they took to run using
"adb shell dumpsys dbinfo". (Because most of the interesting
work happens in SQLiteConnection, it is easy to add debugging
instrumentation to track all database operations in one place.)

Change-Id: Iffb4ce72d8bcf20b4e087d911da6aa84d2f15297
/frameworks/base/core/java/android/database/sqlite/SQLiteConnection.java