mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-30 19:42:00 +00:00
a11759c75b
Fix terminate on exception and rollback query fail.
54 lines
969 B
C++
54 lines
969 B
C++
#pragma once
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <mysqlxx/Connection.h>
|
|
|
|
|
|
namespace mysqlxx
|
|
{
|
|
|
|
/** RAII для транзакции. При инициализации, транзакция стартует.
|
|
* При уничтожении, если не был вызван метод commit(), будет произведёт rollback.
|
|
*/
|
|
class Transaction : private boost::noncopyable
|
|
{
|
|
public:
|
|
Transaction(Connection & conn_)
|
|
: conn(conn_), finished(false)
|
|
{
|
|
conn.query("START TRANSACTION").execute();
|
|
}
|
|
|
|
virtual ~Transaction()
|
|
{
|
|
try
|
|
{
|
|
if (!finished)
|
|
rollback();
|
|
}
|
|
catch (...)
|
|
{
|
|
}
|
|
}
|
|
|
|
void commit()
|
|
{
|
|
conn.query("COMMIT").execute();
|
|
finished = true;
|
|
}
|
|
|
|
void rollback()
|
|
{
|
|
conn.query("ROLLBACK").execute();
|
|
finished = true;
|
|
}
|
|
|
|
private:
|
|
Connection & conn;
|
|
bool finished;
|
|
};
|
|
|
|
|
|
}
|