1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sql/transaction.h"
7 #include "base/check.h"
8 #include "base/sequence_checker.h"
9 #include "sql/database.h"
13 Transaction::Transaction(Database* database) : database_(*database) {
17 Transaction::~Transaction() {
18 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
21 << "Begin() not called immediately after Transaction creation";
22 #endif // DCHECK_IS_ON()
25 database_->RollbackTransaction();
28 bool Transaction::Begin() {
29 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
31 DCHECK(!begin_called_) << __func__ << " already called";
33 #endif // DCHECK_IS_ON()
36 is_active_ = database_->BeginTransaction();
40 void Transaction::Rollback() {
41 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
43 DCHECK(begin_called_) << __func__ << " called before Begin()";
44 DCHECK(!commit_called_) << __func__ << " called after Commit()";
45 DCHECK(!rollback_called_) << __func__ << " called twice";
46 rollback_called_ = true;
47 #endif // DCHECK_IS_ON()
49 DCHECK(is_active_) << __func__ << " called after Begin() failed";
52 database_->RollbackTransaction();
55 bool Transaction::Commit() {
56 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
58 DCHECK(begin_called_) << __func__ << " called before Begin()";
59 DCHECK(!rollback_called_) << __func__ << " called after Rollback()";
60 DCHECK(!commit_called_) << __func__ << " called after Commit()";
61 commit_called_ = true;
62 #endif // DCHECK_IS_ON()
64 DCHECK(is_active_) << __func__ << " called after Begin() failed";
66 return database_->CommitTransaction();