1 //
2 // Copyright (C) 2004-2008 Maciej Sobczak
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #define SOCI_SOURCE
9 #include "soci/transaction.h"
10 #include "soci/error.h"
11 
12 using namespace soci;
13 
transaction(session & sql)14 transaction::transaction(session& sql)
15     : handled_(false), sql_(sql)
16 {
17     sql_.begin();
18 }
19 
~transaction()20 transaction::~transaction()
21 {
22     if (handled_ == false)
23     {
24         try
25         {
26             rollback();
27         }
28         catch (...)
29         {}
30     }
31 }
32 
commit()33 void transaction::commit()
34 {
35     if (handled_)
36     {
37         throw soci_error("The transaction object cannot be handled twice.");
38     }
39 
40     sql_.commit();
41     handled_ = true;
42 }
43 
rollback()44 void transaction::rollback()
45 {
46     if (handled_)
47     {
48         throw soci_error("The transaction object cannot be handled twice.");
49     }
50 
51     sql_.rollback();
52     handled_ = true;
53 }
54