2022-05-26 00:34:17 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of morm
|
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Tomasz Sowa
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "transaction.h"
|
|
|
|
#include "dbconnector.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace morm
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
Transaction::Transaction(ModelConnector * model_connector, bool auto_begin_transaction)
|
|
|
|
{
|
|
|
|
this->model_connector = model_connector;
|
|
|
|
is_transaction_started = false;
|
|
|
|
is_transaction_successful = true;
|
2022-05-26 20:14:14 +02:00
|
|
|
transaction_index = 0;
|
2022-05-27 00:06:56 +02:00
|
|
|
transaction_group = 0;
|
2022-05-26 00:34:17 +02:00
|
|
|
|
|
|
|
if( auto_begin_transaction )
|
|
|
|
{
|
|
|
|
begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-26 00:45:49 +02:00
|
|
|
Transaction::Transaction(const Transaction &)
|
|
|
|
{
|
|
|
|
// at the moment do not allow to copy transactions (make me private)
|
|
|
|
}
|
|
|
|
|
|
|
|
Transaction::Transaction(Transaction &&)
|
|
|
|
{
|
|
|
|
// at the moment do not allow to move transactions (make me private)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-26 00:34:17 +02:00
|
|
|
Transaction::~Transaction()
|
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
if( is_current_group() && is_transaction_started )
|
2022-05-26 00:34:17 +02:00
|
|
|
{
|
|
|
|
rollback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Transaction::is_started()
|
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
return is_current_group() && is_transaction_started;
|
2022-05-26 00:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Transaction::is_successful()
|
|
|
|
{
|
|
|
|
return is_transaction_successful;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Transaction::set_successful(bool is_successful)
|
|
|
|
{
|
|
|
|
is_transaction_successful = is_successful;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Transaction::begin()
|
|
|
|
{
|
2022-05-26 20:14:14 +02:00
|
|
|
bool status = false;
|
2022-05-26 00:34:17 +02:00
|
|
|
pt::Log * log = get_logger();
|
|
|
|
|
2022-05-27 00:06:56 +02:00
|
|
|
if( is_current_group() && is_transaction_started )
|
2022-05-26 00:34:17 +02:00
|
|
|
{
|
|
|
|
if( log )
|
|
|
|
{
|
|
|
|
(*log) << pt::Log::log2 << "Morm: a transaction is already started - rollbacking existing transaction before creating a new one" << pt::Log::logend;
|
|
|
|
}
|
|
|
|
|
|
|
|
rollback(); // what if there is an error here? skip it at the moment
|
|
|
|
}
|
|
|
|
|
2022-05-26 20:14:14 +02:00
|
|
|
if( model_connector )
|
|
|
|
{
|
|
|
|
DbConnector * db_connector = model_connector->get_db_connector();
|
|
|
|
|
|
|
|
if( db_connector )
|
|
|
|
{
|
|
|
|
status = db_connector->begin();
|
|
|
|
transaction_index = db_connector->get_transaction_index();
|
2022-05-27 00:06:56 +02:00
|
|
|
transaction_group = db_connector->get_transaction_group();
|
2022-05-26 20:14:14 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-26 00:34:17 +02:00
|
|
|
|
|
|
|
if( status )
|
|
|
|
{
|
|
|
|
is_transaction_started = true;
|
|
|
|
is_transaction_successful = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
is_transaction_started = false;
|
|
|
|
is_transaction_successful = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Transaction::begin_if_needed()
|
|
|
|
{
|
|
|
|
bool status = true;
|
|
|
|
|
|
|
|
if( !is_transaction_started )
|
|
|
|
{
|
|
|
|
status = begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Transaction::rollback()
|
|
|
|
{
|
|
|
|
bool status = false;
|
2022-05-27 00:06:56 +02:00
|
|
|
pt::Log * log = get_logger();
|
2022-05-26 00:34:17 +02:00
|
|
|
|
2022-05-27 00:06:56 +02:00
|
|
|
if( is_current_group() )
|
2022-05-26 00:34:17 +02:00
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
if( is_transaction_started )
|
2022-05-26 00:34:17 +02:00
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
if( model_connector )
|
|
|
|
{
|
|
|
|
DbConnector * db_connector = model_connector->get_db_connector();
|
2022-05-26 20:14:14 +02:00
|
|
|
|
2022-05-27 00:06:56 +02:00
|
|
|
if( db_connector )
|
|
|
|
{
|
|
|
|
status = db_connector->rollback(transaction_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set it to false even if rollback failed
|
|
|
|
is_transaction_started = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( log )
|
2022-05-26 20:14:14 +02:00
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
(*log) << pt::Log::log1 << "Morm: not doing rollback, a transaction was not started - skipping" << pt::Log::logend;
|
2022-05-26 20:14:14 +02:00
|
|
|
}
|
2022-05-26 00:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( log )
|
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
(*log) << pt::Log::log1 << "Morm: not doing rollback, this transaction has already been completed" << pt::Log::logend;
|
2022-05-26 00:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Transaction::commit()
|
|
|
|
{
|
|
|
|
bool status = false;
|
2022-05-27 00:06:56 +02:00
|
|
|
pt::Log * log = get_logger();
|
2022-05-26 00:34:17 +02:00
|
|
|
|
2022-05-27 00:06:56 +02:00
|
|
|
if( is_current_group() )
|
2022-05-26 00:34:17 +02:00
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
if( is_transaction_started )
|
2022-05-26 00:34:17 +02:00
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
if( model_connector )
|
|
|
|
{
|
|
|
|
DbConnector * db_connector = model_connector->get_db_connector();
|
2022-05-26 20:14:14 +02:00
|
|
|
|
2022-05-27 00:06:56 +02:00
|
|
|
if( db_connector )
|
|
|
|
{
|
|
|
|
status = db_connector->commit(transaction_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set it to false even if commit failed
|
|
|
|
is_transaction_started = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( log )
|
2022-05-26 20:14:14 +02:00
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
(*log) << pt::Log::log1 << "Morm: not doing commit, a transaction was not started - skipping" << pt::Log::logend;
|
2022-05-26 20:14:14 +02:00
|
|
|
}
|
2022-05-26 00:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( log )
|
|
|
|
{
|
2022-05-27 00:06:56 +02:00
|
|
|
(*log) << pt::Log::log1 << "Morm: not doing commit, this transaction has already been completed" << pt::Log::logend;
|
2022-05-26 00:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Transaction::finish()
|
|
|
|
{
|
|
|
|
bool status = false;
|
|
|
|
|
|
|
|
if( is_transaction_successful )
|
|
|
|
status = commit();
|
|
|
|
else
|
|
|
|
status = rollback();
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pt::Log * Transaction::get_logger()
|
|
|
|
{
|
|
|
|
pt::Log * logger = nullptr;
|
|
|
|
|
|
|
|
if( model_connector )
|
|
|
|
{
|
|
|
|
logger = model_connector->get_logger();
|
|
|
|
}
|
|
|
|
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-27 00:06:56 +02:00
|
|
|
bool Transaction::is_current_group()
|
|
|
|
{
|
|
|
|
if( model_connector )
|
|
|
|
{
|
|
|
|
DbConnector * db_connector = model_connector->get_db_connector();
|
|
|
|
|
|
|
|
if( db_connector )
|
|
|
|
{
|
|
|
|
return (db_connector->get_transaction_group() == transaction_group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-26 00:34:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|