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:
|
|
|
|
|
Transaction(Connection & conn_)
|
|
|
|
|
: conn(conn_), finished(false)
|
|
|
|
|
{
|
|
|
|
|
conn.query("START TRANSACTION").execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~Transaction()
|
|
|
|
|
{
|
|
|
|
|
if (!finished)
|
|
|
|
|
rollback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void commit()
|
|
|
|
|
{
|
|
|
|
|
conn.query("COMMIT").execute();
|
|
|
|
|
finished = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rollback()
|
|
|
|
|
{
|
|
|
|
|
conn.query("ROLLBACK").execute();
|
|
|
|
|
finished = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Connection & conn;
|
|
|
|
|
bool finished;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|