2005-11-08 05:16:15 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-11-08 05:16:15 +03:00
|
|
|
|
2010-06-01 16:38:00 +04:00
|
|
|
#ifndef MOZSTORAGEHELPER_H
|
|
|
|
#define MOZSTORAGEHELPER_H
|
2005-11-08 05:16:15 +03:00
|
|
|
|
2021-01-26 12:19:44 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2017-12-07 03:52:51 +03:00
|
|
|
#include "nsString.h"
|
2014-09-10 14:46:14 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2021-03-04 07:38:06 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2009-05-13 01:08:49 +04:00
|
|
|
|
2021-03-03 21:53:14 +03:00
|
|
|
#include "mozilla/storage/SQLiteMutex.h"
|
2005-11-08 05:16:15 +03:00
|
|
|
#include "mozIStorageConnection.h"
|
2008-10-14 02:45:40 +04:00
|
|
|
#include "mozIStorageStatement.h"
|
2014-09-10 14:46:14 +04:00
|
|
|
#include "mozIStoragePendingStatement.h"
|
2021-03-25 13:19:44 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2005-11-08 05:16:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class wraps a transaction inside a given C++ scope, guaranteeing that
|
|
|
|
* the transaction will be completed even if you have an exception or
|
|
|
|
* return early.
|
|
|
|
*
|
2014-09-10 14:46:14 +04:00
|
|
|
* A common use is to create an instance with aCommitOnComplete = false
|
|
|
|
* (rollback), then call Commit() on this object manually when your function
|
|
|
|
* completes successfully.
|
2005-11-08 05:16:15 +03:00
|
|
|
*
|
2021-03-03 21:53:14 +03:00
|
|
|
* @note nested transactions are not supported by Sqlite, only nested
|
|
|
|
* savepoints, so if a transaction is already in progress, this object creates
|
|
|
|
* a nested savepoint to the existing transaction which is considered as
|
|
|
|
* anonymous savepoint itself. However, aType and aAsyncCommit are ignored
|
|
|
|
* in the case of nested savepoints.
|
2013-06-27 17:00:59 +04:00
|
|
|
*
|
2014-09-10 14:46:14 +04:00
|
|
|
* @param aConnection
|
|
|
|
* The connection to create the transaction on.
|
|
|
|
* @param aCommitOnComplete
|
|
|
|
* Controls whether the transaction is committed or rolled back when
|
|
|
|
* this object goes out of scope.
|
|
|
|
* @param aType [optional]
|
2018-03-01 09:44:40 +03:00
|
|
|
* The transaction type, as defined in mozIStorageConnection. Uses the
|
|
|
|
* default transaction behavior for the connection if unspecified.
|
2014-09-10 14:46:14 +04:00
|
|
|
* @param aAsyncCommit [optional]
|
|
|
|
* Whether commit should be executed asynchronously on the helper thread.
|
|
|
|
* This is a special option introduced as an interim solution to reduce
|
|
|
|
* main-thread fsyncs in Places. Can only be used on main-thread.
|
|
|
|
*
|
|
|
|
* WARNING: YOU SHOULD _NOT_ WRITE NEW MAIN-THREAD CODE USING THIS!
|
|
|
|
*
|
|
|
|
* Notice that async commit might cause synchronous statements to fail
|
|
|
|
* with SQLITE_BUSY. A possible mitigation strategy is to use
|
|
|
|
* PRAGMA busy_timeout, but notice that might cause main-thread jank.
|
|
|
|
* Finally, if the database is using WAL journaling mode, other
|
|
|
|
* connections won't see the changes done in async committed transactions
|
|
|
|
* until commit is complete.
|
|
|
|
*
|
|
|
|
* For all of the above reasons, this should only be used as an interim
|
|
|
|
* solution and avoided completely if possible.
|
2005-11-08 05:16:15 +03:00
|
|
|
*/
|
2014-09-10 14:46:14 +04:00
|
|
|
class mozStorageTransaction {
|
2021-03-03 21:53:14 +03:00
|
|
|
using SQLiteMutexAutoLock = mozilla::storage::SQLiteMutexAutoLock;
|
|
|
|
|
2005-11-08 05:16:15 +03:00
|
|
|
public:
|
2014-09-10 14:46:14 +04:00
|
|
|
mozStorageTransaction(
|
|
|
|
mozIStorageConnection* aConnection, bool aCommitOnComplete,
|
2018-03-01 09:44:40 +03:00
|
|
|
int32_t aType = mozIStorageConnection::TRANSACTION_DEFAULT,
|
2014-09-10 14:46:14 +04:00
|
|
|
bool aAsyncCommit = false)
|
2005-11-08 05:16:15 +03:00
|
|
|
: mConnection(aConnection),
|
2021-03-04 07:38:06 +03:00
|
|
|
mType(aType),
|
2021-03-03 21:53:14 +03:00
|
|
|
mNestingLevel(0),
|
2011-10-17 18:59:28 +04:00
|
|
|
mHasTransaction(false),
|
2005-11-08 05:16:15 +03:00
|
|
|
mCommitOnComplete(aCommitOnComplete),
|
2014-09-10 14:46:14 +04:00
|
|
|
mCompleted(false),
|
2021-03-04 07:38:06 +03:00
|
|
|
mAsyncCommit(aAsyncCommit) {}
|
2014-09-10 14:46:14 +04:00
|
|
|
|
|
|
|
~mozStorageTransaction() {
|
|
|
|
if (mConnection && mHasTransaction && !mCompleted) {
|
|
|
|
if (mCommitOnComplete) {
|
|
|
|
mozilla::DebugOnly<nsresult> rv = Commit();
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"A transaction didn't commit correctly");
|
2014-09-10 14:46:14 +04:00
|
|
|
} else {
|
|
|
|
mozilla::DebugOnly<nsresult> rv = Rollback();
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"A transaction didn't rollback correctly");
|
2014-09-10 14:46:14 +04:00
|
|
|
}
|
2005-11-08 05:16:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 07:38:06 +03:00
|
|
|
/**
|
|
|
|
* Starts the transaction.
|
|
|
|
*/
|
|
|
|
nsresult Start() {
|
|
|
|
// XXX We should probably get rid of mHasTransaction and use mConnection
|
|
|
|
// for checking if a transaction has been started. However, we need to
|
|
|
|
// first stop supporting null mConnection and also move aConnection from
|
|
|
|
// the constructor to Start.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mHasTransaction);
|
|
|
|
|
|
|
|
// XXX We should probably stop supporting null mConnection.
|
|
|
|
|
|
|
|
// XXX We should probably get rid of mCompleted and allow to start the
|
|
|
|
// transaction again if it was already committed or rolled back.
|
|
|
|
if (!mConnection || mCompleted) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
SQLiteMutexAutoLock lock(mConnection->GetSharedDBMutex());
|
|
|
|
|
|
|
|
// We nee to speculatively set the nesting level to be able to decide
|
|
|
|
// if this is a top level transaction and to be able to generate the
|
|
|
|
// savepoint name.
|
|
|
|
TransactionStarted(lock);
|
|
|
|
|
|
|
|
// If there's a failure we need to revert the speculatively set nesting
|
|
|
|
// level on the connection.
|
|
|
|
auto autoFinishTransaction =
|
|
|
|
mozilla::MakeScopeExit([&] { TransactionFinished(lock); });
|
|
|
|
|
|
|
|
nsAutoCString query;
|
|
|
|
|
|
|
|
if (TopLevelTransaction(lock)) {
|
|
|
|
query.Assign("BEGIN");
|
|
|
|
int32_t type = mType;
|
|
|
|
if (type == mozIStorageConnection::TRANSACTION_DEFAULT) {
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(mConnection->GetDefaultTransactionType(&type));
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case mozIStorageConnection::TRANSACTION_IMMEDIATE:
|
|
|
|
query.AppendLiteral(" IMMEDIATE");
|
|
|
|
break;
|
|
|
|
case mozIStorageConnection::TRANSACTION_EXCLUSIVE:
|
|
|
|
query.AppendLiteral(" EXCLUSIVE");
|
|
|
|
break;
|
|
|
|
case mozIStorageConnection::TRANSACTION_DEFERRED:
|
|
|
|
query.AppendLiteral(" DEFERRED");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unknown transaction type");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query.Assign("SAVEPOINT sp"_ns + IntToCString(mNestingLevel));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mConnection->ExecuteSimpleSQL(query);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
autoFinishTransaction.release();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-11-08 05:16:15 +03:00
|
|
|
/**
|
|
|
|
* Commits the transaction if one is in progress. If one is not in progress,
|
|
|
|
* this is a NOP since the actual owner of the transaction outside of our
|
2014-09-10 14:46:14 +04:00
|
|
|
* scope is in charge of finally committing or rolling back the transaction.
|
2005-11-08 05:16:15 +03:00
|
|
|
*/
|
|
|
|
nsresult Commit() {
|
2021-03-04 07:38:06 +03:00
|
|
|
// XXX Assert instead of returning NS_OK if the transaction hasn't been
|
|
|
|
// started.
|
2014-09-10 14:46:14 +04:00
|
|
|
if (!mConnection || mCompleted || !mHasTransaction) return NS_OK;
|
2021-03-03 21:53:14 +03:00
|
|
|
|
|
|
|
SQLiteMutexAutoLock lock(mConnection->GetSharedDBMutex());
|
|
|
|
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(CurrentTransaction(lock));
|
|
|
|
#else
|
|
|
|
if (!CurrentTransaction(lock)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCompleted = true;
|
2014-09-10 14:46:14 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2021-03-03 21:53:14 +03:00
|
|
|
|
|
|
|
if (TopLevelTransaction(lock)) {
|
|
|
|
// TODO (bug 559659): this might fail with SQLITE_BUSY, but we don't
|
|
|
|
// handle it, thus the transaction might stay open until the next COMMIT.
|
|
|
|
if (mAsyncCommit) {
|
|
|
|
nsCOMPtr<mozIStoragePendingStatement> ps;
|
|
|
|
rv = mConnection->ExecuteSimpleSQLAsync("COMMIT"_ns, nullptr,
|
|
|
|
getter_AddRefs(ps));
|
|
|
|
} else {
|
|
|
|
rv = mConnection->ExecuteSimpleSQL("COMMIT"_ns);
|
|
|
|
}
|
2014-09-10 14:46:14 +04:00
|
|
|
} else {
|
2021-03-03 21:53:14 +03:00
|
|
|
rv = mConnection->ExecuteSimpleSQL("RELEASE sp"_ns +
|
|
|
|
IntToCString(mNestingLevel));
|
2014-09-10 14:46:14 +04:00
|
|
|
}
|
|
|
|
|
2021-03-04 07:38:06 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-05-13 20:53:57 +04:00
|
|
|
|
2021-03-04 07:38:06 +03:00
|
|
|
TransactionFinished(lock);
|
|
|
|
|
|
|
|
return NS_OK;
|
2005-11-08 05:16:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-10 14:46:14 +04:00
|
|
|
* Rolls back the transaction if one is in progress. If one is not in
|
|
|
|
* progress, this is a NOP since the actual owner of the transaction outside
|
|
|
|
* of our scope is in charge of finally rolling back the transaction.
|
2005-11-08 05:16:15 +03:00
|
|
|
*/
|
|
|
|
nsresult Rollback() {
|
2021-03-04 07:38:06 +03:00
|
|
|
// XXX Assert instead of returning NS_OK if the transaction hasn't been
|
|
|
|
// started.
|
2014-09-10 14:46:14 +04:00
|
|
|
if (!mConnection || mCompleted || !mHasTransaction) return NS_OK;
|
2021-03-03 21:53:14 +03:00
|
|
|
|
|
|
|
SQLiteMutexAutoLock lock(mConnection->GetSharedDBMutex());
|
|
|
|
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(CurrentTransaction(lock));
|
|
|
|
#else
|
|
|
|
if (!CurrentTransaction(lock)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCompleted = true;
|
2008-12-09 01:14:14 +03:00
|
|
|
|
2018-07-16 19:56:02 +03:00
|
|
|
nsresult rv;
|
2008-12-09 01:14:14 +03:00
|
|
|
|
2021-03-03 21:53:14 +03:00
|
|
|
if (TopLevelTransaction(lock)) {
|
|
|
|
// TODO (bug 1062823): from Sqlite 3.7.11 on, rollback won't ever return
|
|
|
|
// a busy error, so this handling can be removed.
|
|
|
|
do {
|
|
|
|
rv = mConnection->ExecuteSimpleSQL("ROLLBACK"_ns);
|
|
|
|
if (rv == NS_ERROR_STORAGE_BUSY) (void)PR_Sleep(PR_INTERVAL_NO_WAIT);
|
|
|
|
} while (rv == NS_ERROR_STORAGE_BUSY);
|
|
|
|
} else {
|
|
|
|
const auto nestingLevelCString = IntToCString(mNestingLevel);
|
|
|
|
rv = mConnection->ExecuteSimpleSQL(
|
|
|
|
"ROLLBACK TO sp"_ns + nestingLevelCString + "; RELEASE sp"_ns +
|
|
|
|
nestingLevelCString);
|
|
|
|
}
|
|
|
|
|
2021-03-04 07:38:06 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
TransactionFinished(lock);
|
2009-05-13 20:53:57 +04:00
|
|
|
|
2021-03-04 07:38:06 +03:00
|
|
|
return NS_OK;
|
2005-11-08 05:16:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2021-03-03 21:53:14 +03:00
|
|
|
void TransactionStarted(const SQLiteMutexAutoLock& aProofOfLock) {
|
|
|
|
MOZ_ASSERT(mConnection);
|
|
|
|
MOZ_ASSERT(!mHasTransaction);
|
|
|
|
MOZ_ASSERT(mNestingLevel == 0);
|
|
|
|
mHasTransaction = true;
|
|
|
|
mNestingLevel = mConnection->IncreaseTransactionNestingLevel(aProofOfLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CurrentTransaction(const SQLiteMutexAutoLock& aProofOfLock) const {
|
|
|
|
MOZ_ASSERT(mConnection);
|
|
|
|
MOZ_ASSERT(mHasTransaction);
|
|
|
|
MOZ_ASSERT(mNestingLevel > 0);
|
|
|
|
return mNestingLevel ==
|
|
|
|
mConnection->GetTransactionNestingLevel(aProofOfLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TopLevelTransaction(const SQLiteMutexAutoLock& aProofOfLock) const {
|
|
|
|
MOZ_ASSERT(mConnection);
|
|
|
|
MOZ_ASSERT(mHasTransaction);
|
|
|
|
MOZ_ASSERT(mNestingLevel > 0);
|
|
|
|
MOZ_ASSERT(CurrentTransaction(aProofOfLock));
|
|
|
|
return mNestingLevel == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransactionFinished(const SQLiteMutexAutoLock& aProofOfLock) {
|
|
|
|
MOZ_ASSERT(mConnection);
|
|
|
|
MOZ_ASSERT(mHasTransaction);
|
|
|
|
MOZ_ASSERT(mNestingLevel > 0);
|
|
|
|
MOZ_ASSERT(CurrentTransaction(aProofOfLock));
|
|
|
|
mConnection->DecreaseTransactionNestingLevel(aProofOfLock);
|
|
|
|
mNestingLevel = 0;
|
|
|
|
mHasTransaction = false;
|
|
|
|
}
|
|
|
|
|
2014-09-10 14:46:14 +04:00
|
|
|
nsCOMPtr<mozIStorageConnection> mConnection;
|
2021-03-04 07:38:06 +03:00
|
|
|
int32_t mType;
|
2021-03-03 21:53:14 +03:00
|
|
|
uint32_t mNestingLevel;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mHasTransaction;
|
|
|
|
bool mCommitOnComplete;
|
|
|
|
bool mCompleted;
|
2014-09-10 14:46:14 +04:00
|
|
|
bool mAsyncCommit;
|
2005-11-08 05:16:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class wraps a statement so that it is guaraneed to be reset when
|
|
|
|
* this object goes out of scope.
|
|
|
|
*
|
|
|
|
* Note that this always just resets the statement. If the statement doesn't
|
|
|
|
* need resetting, the reset operation is inexpensive.
|
|
|
|
*/
|
2013-04-12 07:22:09 +04:00
|
|
|
class MOZ_STACK_CLASS mozStorageStatementScoper {
|
2005-11-08 05:16:15 +03:00
|
|
|
public:
|
2014-08-08 03:47:28 +04:00
|
|
|
explicit mozStorageStatementScoper(mozIStorageStatement* aStatement)
|
2005-11-08 05:16:15 +03:00
|
|
|
: mStatement(aStatement) {}
|
|
|
|
~mozStorageStatementScoper() {
|
|
|
|
if (mStatement) mStatement->Reset();
|
|
|
|
}
|
|
|
|
|
2020-09-29 13:57:19 +03:00
|
|
|
mozStorageStatementScoper(mozStorageStatementScoper&&) = default;
|
|
|
|
mozStorageStatementScoper& operator=(mozStorageStatementScoper&&) = default;
|
|
|
|
mozStorageStatementScoper(const mozStorageStatementScoper&) = delete;
|
|
|
|
mozStorageStatementScoper& operator=(const mozStorageStatementScoper&) =
|
|
|
|
delete;
|
|
|
|
|
2005-11-08 05:16:15 +03:00
|
|
|
/**
|
|
|
|
* Call this to make the statement not reset. You might do this if you know
|
|
|
|
* that the statement has been reset.
|
|
|
|
*/
|
2012-07-30 18:20:58 +04:00
|
|
|
void Abandon() { mStatement = nullptr; }
|
2005-11-08 05:16:15 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<mozIStorageStatement> mStatement;
|
|
|
|
};
|
|
|
|
|
2012-01-06 03:46:35 +04:00
|
|
|
// Use this to make queries uniquely identifiable in telemetry
|
|
|
|
// statistics, especially PRAGMAs. We don't include __LINE__ so that
|
|
|
|
// queries are stable in the face of source code changes.
|
|
|
|
#define MOZ_STORAGE_UNIQUIFY_QUERY_STR "/* " __FILE__ " */ "
|
|
|
|
|
2010-06-01 16:38:00 +04:00
|
|
|
#endif /* MOZSTORAGEHELPER_H */
|