Fail
@Insert(onConflict = OnConflictStrategy.FAIL)
@Update(onConflict = OnConflictStrategy.FAIL)
What SQLite Does
This strategy maps to INSERT OR FAIL
or UPDATE OR FAIL
statements. These work much like their ABORT
counterparts, in that a SQLiteConstraintException
is thrown, but the transaction remains open.
The difference is in what happens if your UPDATE
statement affects several rows. In that case, rows that were changed prior to the constraint violation remain changed. The row with the constraint violation, and any others after it, are unchanged.
Frankly, this does not seem like a particularly good idea. At least with ABORT
, you have consistent behavior. With FAIL
, some arbitrary amount of data gets changed, and the rest is not, and without doing your own post-FAIL
analysis, you have no idea what to expect.
Effects in Room
Neither @Insert
nor @Update
will affect multiple rows in a single SQL statement. Even if your method accepts multiple entities (via varargs, List
, etc.), they will each be processed in separate SQL statements, wrapped in a transaction. Room will roll back the transaction when it encounters the SQLiteConstraintException
. As a result, FAIL
will behave akin to ABORT
when used with @Insert
and @Update
.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.