aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libpqxx/src/transaction.cxx
blob: ff0f469e6ce73c5e62717550449e9c9a5263b845 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/** Implementation of the pqxx::transaction class.
 *
 * pqxx::transaction represents a regular database transaction.
 *
 * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 */
#include "pqxx/compiler-internal.hxx"

#include <stdexcept>

#include "pqxx/connection_base"
#include "pqxx/result"
#include "pqxx/transaction"


pqxx::internal::basic_transaction::basic_transaction(
	connection_base &C,
	const std::string &IsolationLevel,
	readwrite_policy rw) :
  namedclass{"transaction"},
  dbtransaction(C, IsolationLevel, rw)
{
}


void pqxx::internal::basic_transaction::do_commit()
{
  try
  {
    direct_exec("COMMIT");
  }
  catch (const statement_completion_unknown &e)
  {
    // Outcome of "commit" is unknown.  This is a disaster: we don't know the
    // resulting state of the database.
    process_notice(e.what() + std::string{"\n"});
    const std::string msg =
      "WARNING: Commit of transaction '" + name() + "' is unknown. "
	"There is no way to tell whether the transaction succeeded "
	"or was aborted except to check manually.";
    process_notice(msg + "\n");
    throw in_doubt_error{msg};
  }
  catch (const std::exception &e)
  {
    if (not conn().is_open())
    {
      // We've lost the connection while committing.  There is just no way of
      // telling what happened on the other end.  >8-O
      process_notice(e.what() + std::string{"\n"});

      const std::string Msg =
	"WARNING: Connection lost while committing transaction "
	"'" + name() + "'. "
	"There is no way to tell whether the transaction succeeded "
	"or was aborted except to check manually.";

      process_notice(Msg + "\n");
      throw in_doubt_error{Msg};
    }
    else
    {
      // Commit failed--probably due to a constraint violation or something
      // similar.
      throw;
    }
  }
}