-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTransaction.cpp
More file actions
105 lines (88 loc) · 2.35 KB
/
Transaction.cpp
File metadata and controls
105 lines (88 loc) · 2.35 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* File: Transaction.cpp
* Created on 27 luglio 2018
*/
#include "Transaction.h"
Transaction::Transaction() {
status = new ThrowStatusWrapper(master->getStatus());
}
void Transaction::setAttachment(Attachment* attachemnt) {
if (this->attachment) {
attachemnt->dispatcher.removeCallBack(attachmentCallbackID);
}
release();
this->attachment = attachemnt;
attachmentCallbackID = attachemnt->dispatcher.addCallBack([this](DBStateEvents evt) {
switch (evt) {
case DBStateEvents::ATTACHMENT_DISCONNECT:
release();
break;
case DBStateEvents::ATTACHMENT_DELETE:
release();
dispatcher.broadcast(DBStateEvents::TRANSACTION_DISCONNECT);
this->attachment = nullptr;
break;
default:
break;
}
});
}
Transaction::~Transaction() {
dispatcher.broadcast(DBStateEvents::TRANSACTION_DELETE);
if (tra_) {
tra_->release();
}
if (status) {
status->dispose();
delete status;
}
if(attachment)
attachment->dispatcher.removeCallBack(attachmentCallbackID);
}
void Transaction::release() {
try {
rollback();
} catch (const FbException& e) {
if (tra_) {
tra_->release();
tra_ = nullptr;
}
char buf[256];
formatExceptionMessage(e, buf, 256);
fprintf(stderr, "%s\n", buf);
}
}
void Transaction::connect() {
if (attachment) {
if (!tra_) {
attachment->connect();
tra_ = attachment->att_->startTransaction(status, 0, nullptr);
}
} else
throw std::logic_error("Transaction: set attachment before connect!");
}
bool Transaction::isConnected() {
return tra_ != nullptr;
}
void Transaction::commit() {
if (tra_) {
tra_->commit(status);
tra_ = nullptr;
dispatcher.broadcast(DBStateEvents::TRANSACTION_DISCONNECT);
}
}
void Transaction::commitRetain() {
if (tra_)
tra_->commitRetaining(status);
}
void Transaction::rollback() {
if (tra_) {
dispatcher.broadcast(DBStateEvents::TRANSACTION_DISCONNECT);
tra_->rollback(status);
tra_ = nullptr;
}
}
void Transaction::rollbackRetaining() {
if (tra_)
tra_->rollbackRetaining(status);
}