@Override public void saveTimetableCourses(final List<Course> courses) { SQLiteDatabase db = mDbHelper.getWritableDatabase(); db.beginTransactionWithListener(new SQLiteTransactionListener() { @Override public void onBegin() { } @Override public void onCommit() { Log.d(tag, "save courses " + courses.size() + "commit successful"); } @Override public void onRollback() { Log.d(tag, "save courses " + courses.size() + "rollback"); } }); for (Course course : courses) { db.insert(CoursePersistenceContract.CourseTimetableEntry.TABLE_NAME, null , convertTimetableCourseToCv(course)); } db.setTransactionSuccessful(); db.endTransaction(); }
/** * {@inheritDoc} * * NOTE: Not presently supported, will throw an UnsupportedOperationException */ @Override public void beginTransactionWithListener( SQLiteTransactionListener transactionListener) { // TODO not supported in SQLCipher for Android throw new UnsupportedOperationException("I kinna do it, cap'n!"); }
/** * {@inheritDoc} * * NOTE: Not presently supported, will throw an UnsupportedOperationException */ @Override public void beginTransactionWithListenerNonExclusive( SQLiteTransactionListener transactionListener) { // TODO not supported in SQLCipher for Android throw new UnsupportedOperationException("I kinna do it, cap'n!"); }
private void beginTransaction(SQLiteTransactionListener transactionListener, int mode) { acquireReference(); try { getThreadSession().beginTransaction(mode, transactionListener, getThreadDefaultConnectionFlags(false /*readOnly*/), null); } finally { releaseReference(); } }
private boolean yieldTransactionUnchecked(long sleepAfterYieldDelayMillis, CancellationSignal cancellationSignal) { if (cancellationSignal != null) { cancellationSignal.throwIfCanceled(); } if (!mConnectionPool.shouldYieldConnection(mConnection, mConnectionFlags)) { return false; } final int transactionMode = mTransactionStack.mMode; final SQLiteTransactionListener listener = mTransactionStack.mListener; final int connectionFlags = mConnectionFlags; endTransactionUnchecked(cancellationSignal, true); // might throw if (sleepAfterYieldDelayMillis > 0) { try { Thread.sleep(sleepAfterYieldDelayMillis); } catch (InterruptedException ex) { // we have been interrupted, that's all we need to do } } beginTransactionUnchecked(transactionMode, listener, connectionFlags, cancellationSignal); // might throw return true; }
private Transaction obtainTransaction(int mode, SQLiteTransactionListener listener) { Transaction transaction = mTransactionPool; if (transaction != null) { mTransactionPool = transaction.mParent; transaction.mParent = null; transaction.mMarkedSuccessful = false; transaction.mChildFailed = false; } else { transaction = new Transaction(); } transaction.mMode = mode; transaction.mListener = listener; return transaction; }
@Override public void beginTransactionWithListener(SQLiteTransactionListener transactionListener) { mDelegate.beginTransactionWithListener(transactionListener); }
@Override public void beginTransactionWithListenerNonExclusive( SQLiteTransactionListener transactionListener) { mDelegate.beginTransactionWithListenerNonExclusive(transactionListener); }
private void beginTransactionUnchecked(int transactionMode, SQLiteTransactionListener transactionListener, int connectionFlags, CancellationSignal cancellationSignal) { if (cancellationSignal != null) { cancellationSignal.throwIfCanceled(); } if (mTransactionStack == null) { acquireConnection(null, connectionFlags, cancellationSignal); // might throw } try { // Set up the transaction such that we can back out safely // in case we fail part way. if (mTransactionStack == null) { // Execute SQL might throw a runtime exception. switch (transactionMode) { case TRANSACTION_MODE_IMMEDIATE: mConnection.execute("BEGIN IMMEDIATE;", null, cancellationSignal); // might throw break; case TRANSACTION_MODE_EXCLUSIVE: mConnection.execute("BEGIN EXCLUSIVE;", null, cancellationSignal); // might throw break; default: mConnection.execute("BEGIN;", null, cancellationSignal); // might throw break; } } // Listener might throw a runtime exception. if (transactionListener != null) { try { transactionListener.onBegin(); // might throw } catch (RuntimeException ex) { if (mTransactionStack == null) { mConnection.execute("ROLLBACK;", null, cancellationSignal); // might throw } throw ex; } } // Bookkeeping can't throw, except an OOM, which is just too bad... Transaction transaction = obtainTransaction(transactionMode, transactionListener); transaction.mParent = mTransactionStack; mTransactionStack = transaction; } finally { if (mTransactionStack == null) { releaseConnection(); // might throw } } }
private void endTransactionUnchecked(CancellationSignal cancellationSignal, boolean yielding) { if (cancellationSignal != null) { cancellationSignal.throwIfCanceled(); } final Transaction top = mTransactionStack; boolean successful = (top.mMarkedSuccessful || yielding) && !top.mChildFailed; RuntimeException listenerException = null; final SQLiteTransactionListener listener = top.mListener; if (listener != null) { try { if (successful) { listener.onCommit(); // might throw } else { listener.onRollback(); // might throw } } catch (RuntimeException ex) { listenerException = ex; successful = false; } } mTransactionStack = top.mParent; recycleTransaction(top); if (mTransactionStack != null) { if (!successful) { mTransactionStack.mChildFailed = true; } } else { try { if (successful) { mConnection.execute("COMMIT;", null, cancellationSignal); // might throw } else { mConnection.execute("ROLLBACK;", null, cancellationSignal); // might throw } } finally { releaseConnection(); // might throw } } if (listenerException != null) { throw listenerException; } }
@Override public void beginTransactionWithListener(SQLiteTransactionListener transactionListener) { throw new UnsupportedOperationException(); }
@Override public void beginTransactionWithListenerNonExclusive(SQLiteTransactionListener transactionListener) { throw new UnsupportedOperationException(); }
/** * Begins a transaction. * <p> * Transactions may nest. If the transaction is not in progress, * then a database connection is obtained and a new transaction is started. * Otherwise, a nested transaction is started. * </p><p> * Each call to {@link #beginTransaction} must be matched exactly by a call * to {@link #endTransaction}. To mark a transaction as successful, * call {@link #setTransactionSuccessful} before calling {@link #endTransaction}. * If the transaction is not successful, or if any of its nested * transactions were not successful, then the entire transaction will * be rolled back when the outermost transaction is ended. * </p> * * @param transactionMode The transaction mode. One of: {@link #TRANSACTION_MODE_DEFERRED}, * {@link #TRANSACTION_MODE_IMMEDIATE}, or {@link #TRANSACTION_MODE_EXCLUSIVE}. * Ignored when creating a nested transaction. * @param transactionListener The transaction listener, or null if none. * @param connectionFlags The connection flags to use if a connection must be * acquired by this operation. Refer to {@link SQLiteConnectionPool}. * @param cancellationSignal A signal to cancel the operation in progress, or null if none. * * @throws IllegalStateException if {@link #setTransactionSuccessful} has already been * called for the current transaction. * @throws SQLiteException if an error occurs. * @throws OperationCanceledException if the operation was canceled. * * @see #setTransactionSuccessful * @see #yieldTransaction * @see #endTransaction */ public void beginTransaction(int transactionMode, SQLiteTransactionListener transactionListener, int connectionFlags, CancellationSignal cancellationSignal) { throwIfTransactionMarkedSuccessful(); beginTransactionUnchecked(transactionMode, transactionListener, connectionFlags, cancellationSignal); }
/** * Begins a transaction in DEFERRED mode. * * @param transactionListener listener that should be notified when the transaction begins, * commits, or is rolled back, either explicitly or by a call to * {@link #yieldIfContendedSafely}. */ public void beginTransactionWithListenerDeferred( SQLiteTransactionListener transactionListener) { beginTransaction(transactionListener, SQLiteSession.TRANSACTION_MODE_DEFERRED); }
/** * Begins a transaction in EXCLUSIVE mode. * <p> * Transactions can be nested. * When the outer transaction is ended all of * the work done in that transaction and all of the nested transactions will be committed or * rolled back. The changes will be rolled back if any transaction is ended without being * marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed. * </p> * <p>Here is the standard idiom for transactions: * * <pre> * db.beginTransactionWithListener(listener); * try { * ... * db.setTransactionSuccessful(); * } finally { * db.endTransaction(); * } * </pre> * * @param transactionListener listener that should be notified when the transaction begins, * commits, or is rolled back, either explicitly or by a call to * {@link #yieldIfContendedSafely}. */ @Override public void beginTransactionWithListener(SQLiteTransactionListener transactionListener) { beginTransaction(transactionListener, SQLiteSession.TRANSACTION_MODE_EXCLUSIVE); }
/** * Begins a transaction in IMMEDIATE mode. Transactions can be nested. When * the outer transaction is ended all of the work done in that transaction * and all of the nested transactions will be committed or rolled back. The * changes will be rolled back if any transaction is ended without being * marked as clean (by calling setTransactionSuccessful). Otherwise they * will be committed. * <p> * Here is the standard idiom for transactions: * * <pre> * db.beginTransactionWithListenerNonExclusive(listener); * try { * ... * db.setTransactionSuccessful(); * } finally { * db.endTransaction(); * } * </pre> * * @param transactionListener listener that should be notified when the * transaction begins, commits, or is rolled back, either * explicitly or by a call to {@link #yieldIfContendedSafely}. */ @Override public void beginTransactionWithListenerNonExclusive( SQLiteTransactionListener transactionListener) { beginTransaction(transactionListener, SQLiteSession.TRANSACTION_MODE_IMMEDIATE); }
/** * Begins a transaction in EXCLUSIVE mode. * <p> * Transactions can be nested. * When the outer transaction is ended all of * the work done in that transaction and all of the nested transactions will be committed or * rolled back. The changes will be rolled back if any transaction is ended without being * marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed. * </p> * <p>Here is the standard idiom for transactions: * * <pre> * db.beginTransactionWithListener(listener); * try { * ... * db.setTransactionSuccessful(); * } finally { * db.endTransaction(); * } * </pre> * * @param transactionListener listener that should be notified when the transaction begins, * commits, or is rolled back, either explicitly or by a call to * {@link #yieldIfContendedSafely}. */ void beginTransactionWithListener(SQLiteTransactionListener transactionListener);
/** * Begins a transaction in IMMEDIATE mode. Transactions can be nested. When * the outer transaction is ended all of the work done in that transaction * and all of the nested transactions will be committed or rolled back. The * changes will be rolled back if any transaction is ended without being * marked as clean (by calling setTransactionSuccessful). Otherwise they * will be committed. * <p> * Here is the standard idiom for transactions: * * <pre> * db.beginTransactionWithListenerNonExclusive(listener); * try { * ... * db.setTransactionSuccessful(); * } finally { * db.endTransaction(); * } * </pre> * * @param transactionListener listener that should be notified when the * transaction begins, commits, or is rolled back, either * explicitly or by a call to {@link #yieldIfContendedSafely}. */ void beginTransactionWithListenerNonExclusive(SQLiteTransactionListener transactionListener);