android.database.sqlite.SQLiteTransactionListener Java Examples

The following examples show how to use android.database.sqlite.SQLiteTransactionListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Database.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void beginTransactionWithListenerNonExclusive(SQLiteTransactionListener listener) {
  safeDb.beginTransactionWithListenerNonExclusive(
    new net.sqlcipher.database.SQLiteTransactionListener() {
      @Override
      public void onBegin() {
        listener.onBegin();
      }

      @Override
      public void onCommit() {
        listener.onCommit();
      }

      @Override
      public void onRollback() {
        listener.onRollback();
      }
    });
}
 
Example #2
Source File: LocalCourseDataSource.java    From ZfsoftCampusAssit with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #3
Source File: Database.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void beginTransactionWithListenerNonExclusive(SQLiteTransactionListener listener) {
	safeDb.beginTransactionWithListenerNonExclusive(new net.sqlcipher.database.SQLiteTransactionListener() {
		@Override
		public void onBegin() {
			listener.onBegin();
		}

		@Override
		public void onCommit() {
			listener.onCommit();
		}

		@Override
		public void onRollback() {
			listener.onRollback();
		}
	});
}
 
Example #4
Source File: Database.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void beginTransactionWithListener(SQLiteTransactionListener listener) {
	safeDb.beginTransactionWithListener(new net.sqlcipher.database.SQLiteTransactionListener() {
		@Override
		public void onBegin() {
			listener.onBegin();
		}

		@Override
		public void onCommit() {
			listener.onCommit();
		}

		@Override
		public void onRollback() {
			listener.onRollback();
		}
	});
}
 
Example #5
Source File: Database.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void beginTransactionWithListener(SQLiteTransactionListener listener) {
  safeDb.beginTransactionWithListener(
    new net.sqlcipher.database.SQLiteTransactionListener() {
      @Override
      public void onBegin() {
        listener.onBegin();
      }

      @Override
      public void onCommit() {
        listener.onCommit();
      }

      @Override
      public void onRollback() {
        listener.onRollback();
      }
    });
}
 
Example #6
Source File: SQLiteDatabase.java    From sqlite-android with Apache License 2.0 5 votes vote down vote up
private void beginTransaction(SQLiteTransactionListener transactionListener, int mode) {
    acquireReference();
    try {
        getThreadSession().beginTransaction(mode, transactionListener,
                getThreadDefaultConnectionFlags(false /*readOnly*/), null);
    } finally {
        releaseReference();
    }
}
 
Example #7
Source File: SQLiteSession.java    From sqlite-android with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #8
Source File: SQLiteSession.java    From sqlite-android with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #9
Source File: SQLiteSession.java    From sqlite-android with Apache License 2.0 4 votes vote down vote up
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
        }
    }
}
 
Example #10
Source File: SQLiteSession.java    From sqlite-android with Apache License 2.0 4 votes vote down vote up
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;
    }
}
 
Example #11
Source File: KriptonSQLiteDatabaseWrapperImpl.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void beginTransactionWithListener(SQLiteTransactionListener listener) {
	safeDb.beginTransactionWithListener(listener);
}
 
Example #12
Source File: KriptonSQLiteDatabaseWrapperImpl.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void beginTransactionWithListenerNonExclusive(SQLiteTransactionListener listener) {
	safeDb.beginTransactionWithListenerNonExclusive(listener);
}
 
Example #13
Source File: SQLiteSession.java    From sqlite-android with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example #14
Source File: SQLiteDatabase.java    From sqlite-android with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #15
Source File: SupportSQLiteDatabase.java    From kripton with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
 
Example #16
Source File: SupportSQLiteDatabase.java    From kripton with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
 
Example #17
Source File: SQLiteDatabase.java    From sqlite-android with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #18
Source File: SQLiteDatabase.java    From sqlite-android with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}