PageRenderTime 142ms CodeModel.GetById 81ms app.highlight 7ms RepoModel.GetById 53ms app.codeStats 0ms

/mordor/pq/transaction.cpp

http://github.com/mozy/mordor
C++ | 105 lines | 93 code | 10 blank | 2 comment | 6 complexity | 88fbff6ad3887710a1f10017e298b896 MD5 | raw file
  1// Copyright (c) 2010 - Mozy, Inc.
  2
  3#include "transaction.h"
  4
  5#include "mordor/assert.h"
  6
  7#include "connection.h"
  8
  9namespace Mordor {
 10namespace PQ {
 11
 12Transaction::Transaction(Connection &connection, IsolationLevel isolationLevel)
 13: m_connection(connection),
 14  m_active(true)
 15{
 16    switch (isolationLevel) {
 17        case DEFAULT:
 18            m_connection.execute("BEGIN");
 19            break;
 20        case SERIALIZABLE:
 21            m_connection.execute("BEGIN ISOLATION LEVEL SERIALIZABLE");
 22            break;
 23        case READ_COMMITTED:
 24            m_connection.execute("BEGIN ISOLATION LEVEL READ COMMITTED");
 25            break;
 26        default:
 27            MORDOR_NOTREACHED();
 28    }
 29}
 30
 31Transaction::Transaction(Connection &connection, IsolationLevel isolationLevel,
 32                         Mode mode)
 33: m_connection(connection),
 34  m_active(true)
 35{
 36    switch (isolationLevel) {
 37        case DEFAULT:
 38            switch (mode) {
 39                case READ_WRITE:
 40                    m_connection.execute("BEGIN READ WRITE");
 41                    break;
 42                case READ_ONLY:
 43                    m_connection.execute("BEGIN READ ONLY");
 44                    break;
 45                default:
 46                    MORDOR_NOTREACHED();
 47            }
 48            break;
 49        case SERIALIZABLE:
 50            switch (mode) {
 51                case READ_WRITE:
 52                    m_connection.execute("BEGIN ISOLATION LEVEL SERIALIZABLE READ WRITE");
 53                    break;
 54                case READ_ONLY:
 55                    m_connection.execute("BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY");
 56                    break;
 57                default:
 58                    MORDOR_NOTREACHED();
 59            }
 60            break;
 61        case READ_COMMITTED:
 62            switch (mode) {
 63                case READ_WRITE:
 64                    m_connection.execute("BEGIN ISOLATION LEVEL READ COMMITTED READ WRITE");
 65                    break;
 66                case READ_ONLY:
 67                    m_connection.execute("BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY");
 68                    break;
 69                default:
 70                    MORDOR_NOTREACHED();
 71            }
 72            break;
 73        default:
 74            MORDOR_NOTREACHED();
 75    }
 76}
 77
 78Transaction::~Transaction()
 79{
 80    if (m_active) {
 81        try {
 82            m_connection.execute("ROLLBACK");
 83        } catch (...) {
 84            // No exceptions in destructors
 85        }
 86    }
 87}
 88
 89void
 90Transaction::commit()
 91{
 92    MORDOR_ASSERT(m_active);
 93    m_connection.execute("COMMIT");
 94    m_active = false;
 95}
 96
 97void
 98Transaction::rollback()
 99{
100    MORDOR_ASSERT(m_active);
101    m_connection.execute("ROLLBACK");
102    m_active = false;
103}
104
105}}