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

While Room used to support OnConflictStrategy.FAIL, it is now deprecated. Google recommends that you use ABORT instead.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.