do Transaction::commit/rollback only on the same group for which a begin was called
This commit is contained in:
parent
dd04ee84b7
commit
21117e24c0
|
@ -52,6 +52,7 @@ DbConnector::DbConnector()
|
|||
log = nullptr;
|
||||
log_queries = false;
|
||||
transaction_index = 0;
|
||||
transaction_group = 0;
|
||||
}
|
||||
|
||||
DbConnector::~DbConnector()
|
||||
|
@ -239,6 +240,7 @@ bool DbConnector::rollback_one_transaction(size_t index)
|
|||
{
|
||||
status = DbConnector::query("ROLLBACK");
|
||||
transaction_index = 0;
|
||||
transaction_group += 1;
|
||||
}
|
||||
|
||||
return status;
|
||||
|
@ -307,6 +309,7 @@ bool DbConnector::commit_one_transaction(size_t index)
|
|||
{
|
||||
status = DbConnector::query("COMMIT");
|
||||
transaction_index = 0;
|
||||
transaction_group += 1;
|
||||
}
|
||||
|
||||
return status;
|
||||
|
@ -366,6 +369,13 @@ size_t DbConnector::get_transaction_index()
|
|||
}
|
||||
|
||||
|
||||
size_t DbConnector::get_transaction_group()
|
||||
{
|
||||
return transaction_group;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
DbExpression * DbConnector::get_expression()
|
||||
{
|
||||
|
|
|
@ -118,12 +118,20 @@ public:
|
|||
virtual bool commit(size_t index);
|
||||
|
||||
/*
|
||||
* return current transaction index
|
||||
* return current transaction index (within a group)
|
||||
* first transaction has index equal to one
|
||||
* returns zero if there are no any transactions
|
||||
*/
|
||||
virtual size_t get_transaction_index();
|
||||
|
||||
/*
|
||||
* return current transaction group index
|
||||
* a group index is incremented when you commit or rollback a transaction
|
||||
* (if you are closing a nested transaction then the group index is not incremented)
|
||||
*
|
||||
*/
|
||||
virtual size_t get_transaction_group();
|
||||
|
||||
|
||||
virtual void get_value(const char * value_str, char & field_value, const FT & field_type = FT::default_type);
|
||||
virtual void get_value(const char * value_str, unsigned char & field_value, const FT & field_type = FT::default_type);
|
||||
|
@ -176,6 +184,7 @@ protected:
|
|||
pt::Log * log;
|
||||
bool log_queries;
|
||||
size_t transaction_index;
|
||||
size_t transaction_group;
|
||||
|
||||
|
||||
virtual void allocate_default_expression() = 0;
|
||||
|
|
|
@ -39,15 +39,6 @@
|
|||
namespace morm
|
||||
{
|
||||
|
||||
Transaction::Transaction(ModelConnector * model_connector)
|
||||
{
|
||||
this->model_connector = model_connector;
|
||||
is_transaction_started = false;
|
||||
is_transaction_successful = true;
|
||||
transaction_index = 0;
|
||||
begin();
|
||||
}
|
||||
|
||||
|
||||
Transaction::Transaction(ModelConnector * model_connector, bool auto_begin_transaction)
|
||||
{
|
||||
|
@ -55,6 +46,7 @@ Transaction::Transaction(ModelConnector * model_connector, bool auto_begin_trans
|
|||
is_transaction_started = false;
|
||||
is_transaction_successful = true;
|
||||
transaction_index = 0;
|
||||
transaction_group = 0;
|
||||
|
||||
if( auto_begin_transaction )
|
||||
{
|
||||
|
@ -77,7 +69,7 @@ Transaction::Transaction(Transaction &&)
|
|||
|
||||
Transaction::~Transaction()
|
||||
{
|
||||
if( is_transaction_started )
|
||||
if( is_current_group() && is_transaction_started )
|
||||
{
|
||||
rollback();
|
||||
}
|
||||
|
@ -87,7 +79,7 @@ Transaction::~Transaction()
|
|||
|
||||
bool Transaction::is_started()
|
||||
{
|
||||
return is_transaction_started;
|
||||
return is_current_group() && is_transaction_started;
|
||||
}
|
||||
|
||||
|
||||
|
@ -111,7 +103,7 @@ bool Transaction::begin()
|
|||
bool status = false;
|
||||
pt::Log * log = get_logger();
|
||||
|
||||
if( is_transaction_started )
|
||||
if( is_current_group() && is_transaction_started )
|
||||
{
|
||||
if( log )
|
||||
{
|
||||
|
@ -129,6 +121,7 @@ bool Transaction::begin()
|
|||
{
|
||||
status = db_connector->begin();
|
||||
transaction_index = db_connector->get_transaction_index();
|
||||
transaction_group = db_connector->get_transaction_group();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -163,29 +156,38 @@ bool Transaction::begin_if_needed()
|
|||
bool Transaction::rollback()
|
||||
{
|
||||
bool status = false;
|
||||
pt::Log * log = get_logger();
|
||||
|
||||
if( is_transaction_started )
|
||||
if( is_current_group() )
|
||||
{
|
||||
if( model_connector )
|
||||
if( is_transaction_started )
|
||||
{
|
||||
DbConnector * db_connector = model_connector->get_db_connector();
|
||||
|
||||
if( db_connector )
|
||||
if( model_connector )
|
||||
{
|
||||
status = db_connector->rollback(transaction_index);
|
||||
DbConnector * db_connector = model_connector->get_db_connector();
|
||||
|
||||
if( db_connector )
|
||||
{
|
||||
status = db_connector->rollback(transaction_index);
|
||||
}
|
||||
}
|
||||
|
||||
// set it to false even if rollback failed
|
||||
is_transaction_started = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( log )
|
||||
{
|
||||
(*log) << pt::Log::log1 << "Morm: not doing rollback, a transaction was not started - skipping" << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
|
||||
// set it to false even if rollack failed
|
||||
is_transaction_started = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
pt::Log * log = get_logger();
|
||||
|
||||
if( log )
|
||||
{
|
||||
(*log) << pt::Log::log1 << "Morm: not doing rollback, a transaction was not started - skipping" << pt::Log::logend;
|
||||
(*log) << pt::Log::log1 << "Morm: not doing rollback, this transaction has already been completed" << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -196,29 +198,38 @@ bool Transaction::rollback()
|
|||
bool Transaction::commit()
|
||||
{
|
||||
bool status = false;
|
||||
pt::Log * log = get_logger();
|
||||
|
||||
if( is_transaction_started )
|
||||
if( is_current_group() )
|
||||
{
|
||||
if( model_connector )
|
||||
if( is_transaction_started )
|
||||
{
|
||||
DbConnector * db_connector = model_connector->get_db_connector();
|
||||
|
||||
if( db_connector )
|
||||
if( model_connector )
|
||||
{
|
||||
status = db_connector->commit(transaction_index);
|
||||
DbConnector * db_connector = model_connector->get_db_connector();
|
||||
|
||||
if( db_connector )
|
||||
{
|
||||
status = db_connector->commit(transaction_index);
|
||||
}
|
||||
}
|
||||
|
||||
// set it to false even if commit failed
|
||||
is_transaction_started = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( log )
|
||||
{
|
||||
(*log) << pt::Log::log1 << "Morm: not doing commit, a transaction was not started - skipping" << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
|
||||
// set it to false even if commit failed
|
||||
is_transaction_started = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
pt::Log * log = get_logger();
|
||||
|
||||
if( log )
|
||||
{
|
||||
(*log) << pt::Log::log1 << "Morm: not doing commit, a transaction was not started - skipping" << pt::Log::logend;
|
||||
(*log) << pt::Log::log1 << "Morm: not doing commit, this transaction has already been completed" << pt::Log::logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -252,6 +263,21 @@ pt::Log * Transaction::get_logger()
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -47,8 +47,7 @@ class Transaction
|
|||
{
|
||||
public:
|
||||
|
||||
Transaction(ModelConnector * model_connector);
|
||||
Transaction(ModelConnector * model_connector, bool auto_begin_transaction);
|
||||
Transaction(ModelConnector * model_connector, bool auto_begin_transaction = true);
|
||||
virtual ~Transaction();
|
||||
|
||||
virtual bool is_started();
|
||||
|
@ -62,6 +61,7 @@ public:
|
|||
virtual bool commit();
|
||||
virtual bool finish();
|
||||
|
||||
virtual bool is_current_group();
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -69,6 +69,7 @@ protected:
|
|||
bool is_transaction_started;
|
||||
bool is_transaction_successful;
|
||||
size_t transaction_index;
|
||||
size_t transaction_group;
|
||||
|
||||
virtual pt::Log * get_logger();
|
||||
|
||||
|
|
Loading…
Reference in New Issue