File tree Expand file tree Collapse file tree
objectbox-java/src/main/java/io/objectbox/exception
tests/objectbox-java-test/src/main/java/io/objectbox Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2323public interface DbExceptionListener {
2424 /**
2525 * Called when an exception is thrown during a database operation.
26- * Do NOT throw exceptions in this method: behavior is undefined, e.g. all thrown exceptions may be ignored .
26+ * Do NOT throw exceptions in this method: throw exceptions are ignored (but logged to stderr) .
2727 *
2828 * @param e the exception occurred during a database operation
2929 */
Original file line number Diff line number Diff line change 2828import javax .annotation .Nullable ;
2929
3030import io .objectbox .exception .DbException ;
31+ import io .objectbox .exception .DbExceptionListener ;
3132import io .objectbox .exception .DbMaxReadersExceededException ;
3233
33-
34- import static org .junit .Assert .assertArrayEquals ;
35- import static org .junit .Assert .assertEquals ;
36- import static org .junit .Assert .assertFalse ;
37- import static org .junit .Assert .assertNotNull ;
38- import static org .junit .Assert .assertNotSame ;
39- import static org .junit .Assert .assertTrue ;
40- import static org .junit .Assert .fail ;
34+ import static org .junit .Assert .*;
4135
4236public class TransactionTest extends AbstractObjectBoxTest {
4337
@@ -183,6 +177,26 @@ public void testCommitReadTxException() {
183177 }
184178 }
185179
180+ @ Test
181+ public void testCommitReadTxException_exceptionListener () {
182+ final Exception [] exs = {null };
183+ DbExceptionListener exceptionListener = new DbExceptionListener () {
184+ @ Override
185+ public void onDbException (Exception e ) {
186+ exs [0 ] = e ;
187+ }
188+ };
189+ Transaction tx = store .beginReadTx ();
190+ store .setDbExceptionListener (exceptionListener );
191+ try {
192+ tx .commit ();
193+ fail ("Should have thrown" );
194+ } catch (IllegalStateException e ) {
195+ tx .abort ();
196+ assertSame (e , exs [0 ]);
197+ }
198+ }
199+
186200 /*
187201 @Test
188202 public void testTransactionUsingAfterStoreClosed() {
Original file line number Diff line number Diff line change 3333import io .objectbox .TestEntity ;
3434import io .objectbox .TestEntity_ ;
3535import io .objectbox .TxCallback ;
36+ import io .objectbox .exception .DbException ;
37+ import io .objectbox .exception .DbExceptionListener ;
3638import io .objectbox .query .QueryBuilder .StringOrder ;
3739import io .objectbox .relation .MyObjectBox ;
3840import io .objectbox .relation .Order ;
@@ -521,6 +523,26 @@ public void testDateParam() {
521523 query .setParameter (Order_ .date , now );
522524 }
523525
526+ @ Test
527+ public void testFailedUnique_exceptionListener () {
528+ final Exception [] exs = {null };
529+ DbExceptionListener exceptionListener = new DbExceptionListener () {
530+ @ Override
531+ public void onDbException (Exception e ) {
532+ exs [0 ] = e ;
533+ }
534+ };
535+ putTestEntitiesStrings ();
536+ Query <TestEntity > query = box .query ().build ();
537+ store .setDbExceptionListener (exceptionListener );
538+ try {
539+ query .findUnique ();
540+ fail ("Should have thrown" );
541+ } catch (DbException e ) {
542+ assertSame (e , exs [0 ]);
543+ }
544+ }
545+
524546 private QueryFilter <TestEntity > createTestFilter () {
525547 return new QueryFilter <TestEntity >() {
526548 @ Override
You can’t perform that action at this time.
0 commit comments