Skip to content

Commit 6880fed

Browse files
fix transactions and make reset transactoinal
1 parent 3a84d04 commit 6880fed

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/database.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,11 @@ namespace db {
257257
qr = std::move(sql_query_queue.front());
258258
sql_query_queue.pop();
259259
}
260-
auto results = query(qr.format, qr.parameters);
261-
if (qr.callback) {
262-
qr.callback(results);
260+
if (!qr.format.empty()) {
261+
auto results = query(qr.format, qr.parameters);
262+
if (qr.callback) {
263+
qr.callback(results);
264+
}
263265
}
264266
/**
265267
* If a transaction is waiting to be executed, fit it atomically into
@@ -269,7 +271,7 @@ namespace db {
269271
* transaction_in_progress atomic bool is true. This prevents other threads
270272
* running queries that end up inside the transaction.
271273
*/
272-
if (transaction_in_progress.load() && transaction_function) {
274+
if (transaction_in_progress && transaction_function) {
273275
holds_transaction_lock = true;
274276
transaction_function();
275277
holds_transaction_lock = false;
@@ -309,7 +311,7 @@ namespace db {
309311

310312
void transaction(std::function<bool()> closure) {
311313

312-
if (transaction_in_progress.load()) {
314+
if (transaction_in_progress) {
313315
throw std::runtime_error("Transaction already in progress");
314316
}
315317

@@ -336,15 +338,17 @@ namespace db {
336338
/**
337339
* Re-enable the SQL queue so that queries can happen again
338340
*/
339-
transaction_in_progress.exchange(false, std::memory_order_relaxed);
341+
transaction_in_progress = false;
340342
};
341343

342344
/**
343345
* Set the atomic bool that indicates a transaction should be executed.
344346
* The transaction is inserted into the stream of SQL queries inside the
345-
* SQL thread as one atomic operation.
347+
* SQL thread as one atomic operation. We also have to send a blank callback
348+
* query to signal the condition variable and make the SQL queue advance.
346349
*/
347-
transaction_in_progress.exchange(true, std::memory_order_relaxed);
350+
transaction_in_progress = true;
351+
query_callback("", {}, [](auto){});
348352
}
349353

350354
bool close() {
@@ -404,7 +408,7 @@ namespace db {
404408
* If any thread except the queue thread attempts to run a synchronous query whilst
405409
* a transaction is running, it must wait until the transaction completes.
406410
*/
407-
while (transaction_in_progress.load() && !holds_transaction_lock) {
411+
while (transaction_in_progress && !holds_transaction_lock) {
408412
std::this_thread::sleep_for(1ms);
409413
}
410414

src/game_player.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,18 @@ dpp::task<void> delete_live_player(const dpp::interaction_create_t& event) {
208208
live_players.erase(f);
209209
}
210210
}
211-
co_await db::co_query("DELETE FROM game_users WHERE user_id = ?", { event.command.usr.id });
212-
co_await db::co_query("DELETE FROM game_default_users WHERE user_id = ?", { event.command.usr.id });
213-
co_await db::co_query("DELETE FROM game_default_spells WHERE user_id = ?", { event.command.usr.id });
214-
co_await db::co_query("DELETE FROM game_bank WHERE owner_id = ?", { event.command.usr.id });
215-
co_await db::co_query("DELETE FROM game_owned_items WHERE user_id = ?", { event.command.usr.id });
216-
co_await db::co_query("DELETE FROM timed_flags WHERE user_id = ?", { event.command.usr.id });
217-
co_await db::co_query("DELETE FROM potion_drops WHERE user_id = ?", { event.command.usr.id });
218-
co_await db::co_query("DELETE FROM kv_store WHERE user_id = ?", { event.command.usr.id });
211+
db::transaction([event]() -> bool {
212+
db::query("DELETE FROM game_users WHERE user_id = ?", { event.command.usr.id });
213+
db::query("DELETE FROM game_default_users WHERE user_id = ?", { event.command.usr.id });
214+
db::query("DELETE FROM game_default_spells WHERE user_id = ?", { event.command.usr.id });
215+
db::query("DELETE FROM game_bank WHERE owner_id = ?", { event.command.usr.id });
216+
db::query("DELETE FROM game_owned_items WHERE user_id = ?", { event.command.usr.id });
217+
db::query("DELETE FROM timed_flags WHERE user_id = ?", { event.command.usr.id });
218+
db::query("DELETE FROM potion_drops WHERE user_id = ?", { event.command.usr.id });
219+
db::query("DELETE FROM kv_store WHERE user_id = ?", { event.command.usr.id });
220+
return true;
221+
});
222+
co_return;
219223
}
220224

221225
player get_live_player(const dpp::interaction_create_t& event, bool update_event) {

0 commit comments

Comments
 (0)