Mercurial > hg > orthanc-databases
annotate Framework/PostgreSQL/PostgreSQLTransaction.cpp @ 128:39b2f29ddf3c
integration OrthancPostgreSQL-3.1 to mainline
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 08 Feb 2019 19:55:01 +0100 |
parents | 569e17419eae |
children | 260fc55f10cd 4cd7e45b671e |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
67 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "PostgreSQLTransaction.h" | |
23 | |
24 #include "PostgreSQLStatement.h" | |
25 | |
26 #include <Core/Logging.h> | |
27 #include <Core/OrthancException.h> | |
28 | |
29 namespace OrthancDatabases | |
30 { | |
31 PostgreSQLTransaction::PostgreSQLTransaction(PostgreSQLDatabase& database) : | |
32 database_(database), | |
33 isOpen_(false), | |
34 readOnly_(true) | |
35 { | |
36 Begin(); | |
37 } | |
38 | |
39 | |
40 PostgreSQLTransaction::~PostgreSQLTransaction() | |
41 { | |
42 if (isOpen_) | |
43 { | |
44 LOG(WARNING) << "PostgreSQL: An active PostgreSQL transaction was dismissed"; | |
45 | |
46 try | |
47 { | |
48 database_.Execute("ABORT"); | |
49 } | |
50 catch (Orthanc::OrthancException&) | |
51 { | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
52 // Ignore possible exceptions due to connection loss |
0 | 53 } |
54 } | |
55 } | |
56 | |
57 | |
58 void PostgreSQLTransaction::Begin() | |
59 { | |
60 if (isOpen_) | |
61 { | |
62 LOG(ERROR) << "PostgreSQL: Beginning a transaction twice!"; | |
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
64 } | |
65 | |
66 database_.Execute("BEGIN"); | |
67 database_.Execute("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE"); | |
68 readOnly_ = true; | |
69 isOpen_ = true; | |
70 } | |
71 | |
72 | |
73 void PostgreSQLTransaction::Rollback() | |
74 { | |
75 if (!isOpen_) | |
76 { | |
77 LOG(ERROR) << "PostgreSQL: Attempting to rollback a nonexistent transaction. " | |
78 << "Did you remember to call Begin()?"; | |
79 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
80 } | |
81 | |
82 database_.Execute("ABORT"); | |
83 isOpen_ = false; | |
84 } | |
85 | |
86 | |
87 void PostgreSQLTransaction::Commit() | |
88 { | |
89 if (!isOpen_) | |
90 { | |
91 LOG(ERROR) << "PostgreSQL: Attempting to roll back a nonexistent transaction. " | |
92 << "Did you remember to call Begin()?"; | |
93 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
94 } | |
95 | |
96 database_.Execute("COMMIT"); | |
97 isOpen_ = false; | |
98 } | |
99 | |
100 | |
101 IResult* PostgreSQLTransaction::Execute(IPrecompiledStatement& statement, | |
102 const Dictionary& parameters) | |
103 { | |
104 std::auto_ptr<IResult> result(dynamic_cast<PostgreSQLStatement&>(statement).Execute(*this, parameters)); | |
105 | |
106 if (!statement.IsReadOnly()) | |
107 { | |
108 readOnly_ = false; | |
109 } | |
110 | |
116 | 111 return result.release(); |
0 | 112 } |
113 | |
114 | |
115 void PostgreSQLTransaction::ExecuteWithoutResult(IPrecompiledStatement& statement, | |
116 const Dictionary& parameters) | |
117 { | |
118 dynamic_cast<PostgreSQLStatement&>(statement).ExecuteWithoutResult(*this, parameters); | |
119 | |
120 if (!statement.IsReadOnly()) | |
121 { | |
122 readOnly_ = false; | |
123 } | |
124 } | |
125 } |