2014-04-08 06:51:53 +00:00
|
|
|
|
#pragma once
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
|
|
|
|
|
#include <mysqlxx/Connection.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace mysqlxx
|
|
|
|
|
{
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
/** RAII для транзакции. При инициализации, транзакция стартует.
|
|
|
|
|
* При уничтожении, если не был вызван метод commit(), будет произведёт rollback.
|
|
|
|
|
*/
|
2011-03-04 20:58:19 +00:00
|
|
|
|
class Transaction : private boost::noncopyable
|
|
|
|
|
{
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Transaction(Connection & conn_)
|
|
|
|
|
: conn(conn_), finished(false)
|
|
|
|
|
{
|
|
|
|
|
conn.query("START TRANSACTION").execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~Transaction()
|
|
|
|
|
{
|
2017-08-16 14:51:54 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!finished)
|
|
|
|
|
rollback();
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void commit()
|
|
|
|
|
{
|
|
|
|
|
conn.query("COMMIT").execute();
|
|
|
|
|
finished = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rollback()
|
|
|
|
|
{
|
|
|
|
|
conn.query("ROLLBACK").execute();
|
|
|
|
|
finished = true;
|
|
|
|
|
}
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Connection & conn;
|
|
|
|
|
bool finished;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|