start.c (3779B)
1 /* 2 This file is part of TALER 3 (C) 2014--2025 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Lesser 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 src/backenddb/start.c 18 * @brief implementation of the start, rollback and commit functions 19 * @author Christian Grothoff 20 */ 21 #include <gnunet/gnunet_util_lib.h> 22 #include "merchantdb_lib.h" 23 #include "merchant-database/start.h" 24 #include "merchant-database/preflight.h" 25 #include "helper.h" 26 27 28 enum GNUNET_GenericReturnValue 29 TALER_MERCHANTDB_start ( 30 struct TALER_MERCHANTDB_PostgresContext *pg, 31 const char *name) 32 { 33 struct GNUNET_PQ_ExecuteStatement es[] = { 34 GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL SERIALIZABLE"), 35 GNUNET_PQ_EXECUTE_STATEMENT_END 36 }; 37 38 GNUNET_assert (NULL != name); 39 GNUNET_PQ_reconnect_if_down (pg->conn); 40 TALER_MERCHANTDB_preflight (pg); 41 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 42 "Starting merchant DB transaction `%s'\n", 43 name); 44 if (GNUNET_OK != 45 GNUNET_PQ_exec_statements (pg->conn, 46 es)) 47 { 48 TALER_LOG_ERROR ("Failed to start transaction\n"); 49 GNUNET_break (0); 50 return GNUNET_SYSERR; 51 } 52 pg->transaction_name = name; 53 return GNUNET_OK; 54 } 55 56 57 enum GNUNET_GenericReturnValue 58 TALER_MERCHANTDB_start_read_committed ( 59 struct TALER_MERCHANTDB_PostgresContext *pg, 60 const char *name) 61 { 62 struct GNUNET_PQ_ExecuteStatement es[] = { 63 GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL READ COMMITTED"), 64 GNUNET_PQ_EXECUTE_STATEMENT_END 65 }; 66 67 GNUNET_assert (NULL != name); 68 check_connection (pg); 69 TALER_MERCHANTDB_preflight (pg); 70 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 71 "Starting merchant DB transaction %s (READ COMMITTED)\n", 72 name); 73 if (GNUNET_OK != 74 GNUNET_PQ_exec_statements (pg->conn, 75 es)) 76 { 77 TALER_LOG_ERROR ("Failed to start transaction\n"); 78 GNUNET_break (0); 79 return GNUNET_SYSERR; 80 } 81 pg->transaction_name = name; 82 return GNUNET_OK; 83 } 84 85 86 void 87 TALER_MERCHANTDB_rollback ( 88 struct TALER_MERCHANTDB_PostgresContext *pg) 89 { 90 struct GNUNET_PQ_ExecuteStatement es[] = { 91 GNUNET_PQ_make_execute ("ROLLBACK"), 92 GNUNET_PQ_EXECUTE_STATEMENT_END 93 }; 94 95 if (NULL == pg->transaction_name) 96 return; 97 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 98 "Rolling back merchant DB transaction `%s'\n", 99 pg->transaction_name); 100 GNUNET_break (GNUNET_OK == 101 GNUNET_PQ_exec_statements (pg->conn, 102 es)); 103 pg->transaction_name = NULL; 104 } 105 106 107 enum GNUNET_DB_QueryStatus 108 TALER_MERCHANTDB_commit (struct TALER_MERCHANTDB_PostgresContext *pg) 109 { 110 struct GNUNET_PQ_QueryParam params[] = { 111 GNUNET_PQ_query_param_end 112 }; 113 enum GNUNET_DB_QueryStatus qs; 114 115 GNUNET_break (NULL != pg->transaction_name); 116 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 117 "Committing transaction `%s'\n", 118 pg->transaction_name); 119 PREPARE (pg, 120 "do_commit", 121 "COMMIT"); 122 qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 123 "do_commit", 124 params); 125 pg->transaction_name = NULL; 126 return qs; 127 }