donau-httpd_db.c (4021B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file donau-httpd_db.c 18 * @brief Generic database operations for the donau. 19 * @author Johannes Casaburi 20 */ 21 #include <donau_config.h> 22 #include <pthread.h> 23 #include <jansson.h> 24 #include <gnunet/gnunet_json_lib.h> 25 #include <taler/taler_json_lib.h> 26 #include <taler/taler_mhd_lib.h> 27 #include "donaudb_lib.h" 28 #include "donau-database/preflight.h" 29 #include "donau-database/start.h" 30 #include "donau-database/rollback.h" 31 #include "donau-database/commit.h" 32 #include "donau-httpd_db.h" 33 #include "donau-httpd.h" 34 35 36 enum GNUNET_GenericReturnValue 37 DH_DB_run_transaction (struct MHD_Connection *connection, 38 const char *name, 39 enum MHD_Result *mhd_ret, 40 DH_DB_TransactionCallback cb, 41 void *cb_cls) 42 { 43 if (NULL != mhd_ret) 44 *mhd_ret = -1; /* set to invalid value, to help detect bugs */ 45 if (GNUNET_OK != 46 DONAUDB_preflight (DH_context)) 47 { 48 GNUNET_break (0); 49 if (NULL != mhd_ret) 50 *mhd_ret = TALER_MHD_reply_with_error (connection, 51 MHD_HTTP_INTERNAL_SERVER_ERROR, 52 TALER_EC_GENERIC_DB_SETUP_FAILED, 53 NULL); 54 return GNUNET_SYSERR; 55 } 56 for (unsigned int retries = 0; 57 retries < MAX_TRANSACTION_COMMIT_RETRIES; 58 retries++) 59 { 60 enum GNUNET_DB_QueryStatus qs; 61 62 if (GNUNET_OK != 63 DONAUDB_start (DH_context, 64 name)) 65 { 66 GNUNET_break (0); 67 if (NULL != mhd_ret) 68 *mhd_ret = TALER_MHD_reply_with_error (connection, 69 MHD_HTTP_INTERNAL_SERVER_ERROR, 70 TALER_EC_GENERIC_DB_START_FAILED, 71 NULL); 72 return GNUNET_SYSERR; 73 } 74 qs = cb (cb_cls, 75 connection, 76 mhd_ret); 77 if (0 > qs) 78 { 79 DONAUDB_rollback (DH_context); 80 if (GNUNET_DB_STATUS_HARD_ERROR == qs) 81 return GNUNET_SYSERR; 82 } 83 else 84 { 85 qs = DONAUDB_commit (DH_context); 86 if (GNUNET_DB_STATUS_HARD_ERROR == qs) 87 { 88 DONAUDB_rollback (DH_context); 89 if (NULL != mhd_ret) 90 *mhd_ret = TALER_MHD_reply_with_error (connection, 91 MHD_HTTP_INTERNAL_SERVER_ERROR, 92 TALER_EC_GENERIC_DB_COMMIT_FAILED, 93 NULL); 94 return GNUNET_SYSERR; 95 } 96 if (0 > qs) 97 DONAUDB_rollback (DH_context); 98 } 99 /* make sure callback did not violate invariants! */ 100 GNUNET_assert ( (NULL == mhd_ret) || 101 (-1 == (int) *mhd_ret) ); 102 if (0 <= qs) 103 return GNUNET_OK; 104 } 105 DONAUDB_rollback (DH_context); 106 TALER_LOG_ERROR ("Transaction `%s' commit failed %u times\n", 107 name, 108 MAX_TRANSACTION_COMMIT_RETRIES); 109 if (NULL != mhd_ret) 110 *mhd_ret = TALER_MHD_reply_with_error (connection, 111 MHD_HTTP_INTERNAL_SERVER_ERROR, 112 TALER_EC_GENERIC_DB_SOFT_FAILURE, 113 NULL); 114 return GNUNET_SYSERR; 115 } 116 117 118 /* end of donau-httpd_db.c */