GlobalTransactionId BeginTranGTM(GTM_Timestamp *timestamp) { GlobalTransactionId xid = InvalidGlobalTransactionId; CheckConnection(); // TODO Isolation level if (conn) xid = begin_transaction(conn, GTM_ISOLATION_RC, timestamp); /* If something went wrong (timeout), try and reset GTM connection * and retry. This is safe at the beginning of a transaction. */ if (!TransactionIdIsValid(xid)) { CloseGTM(); InitGTM(); if (conn) xid = begin_transaction(conn, GTM_ISOLATION_RC, timestamp); } #ifdef XCP if (xid) IsXidFromGTM = true; #endif currentGxid = xid; return xid; }
int testBegin(){ commit_transaction(); commit_transaction(); begin_transaction(); begin_transaction(); commit_transaction(); begin_transaction(); commit_transaction(); begin_transaction(); begin_transaction(); commit_transaction(); return 0; }
void test_txn5_11() { GlobalTransactionId gxid; int rc; SETUP(); sleep(3); system("killall -9 gtm_standby"); gxid = begin_transaction(conn, GTM_ISOLATION_SERIALIZABLE, timestamp); _ASSERT( gxid != InvalidGlobalTransactionId ); _ASSERT( grep_count(LOG_ACTIVE, "Sending transaction id 3")==1 ); _ASSERT( grep_count(LOG_ACTIVE, "Sending transaction id 3")==1 ); _ASSERT( grep_count(LOG_ACTIVE, "gtm_standby_reconnect_to_standby(): re-connect failed. retry=0")==1 ); _ASSERT( grep_count(LOG_ACTIVE, "gtm_standby_reconnect_to_standby(): re-connect failed. retry=1")==1 ); _ASSERT( grep_count(LOG_ACTIVE, "gtm_standby_reconnect_to_standby(): re-connect failed. retry=2")==1 ); _ASSERT( grep_count(LOG_ACTIVE, "communication error with standby")==1 ); _ASSERT( grep_count(LOG_ACTIVE, "Calling report_xcwatch_gtm_failure()...")==1 ); rc = commit_transaction(conn, gxid); _ASSERT( rc>=0 ); _ASSERT( grep_count(LOG_ACTIVE, "Committing transaction id 3")==1 ); _ASSERT( grep_count(LOG_STANDBY, "Committing transaction id 3")==0 ); TEARDOWN(); }
void test_txn5_06() { GlobalTransactionId gxid; int rc; SETUP(); gxid = begin_transaction(conn, GTM_ISOLATION_SERIALIZABLE, timestamp); _ASSERT( gxid != InvalidGlobalTransactionId ); rc = prepare_transaction(conn, gxid); _ASSERT( rc>=0 ); rc = abort_transaction(conn, gxid); _ASSERT( rc>=0 ); system("./promote.sh"); GTMPQfinish(conn); connect2(); rc = abort_transaction(conn, gxid); _ASSERT( rc>=0 ); TEARDOWN(); }
int run(LOGGER log, struct config config) { sqlite3* db = sqlite_service_connect(log, config.dbpath); curl_global_init(CURL_GLOBAL_SSL); if (!db) { return 1; } if (config.single) { QUIT = 1; } if (prepare_statements(log, db)) { return 2; } spider(log, db, config.hash, config.tree); while(!QUIT) { begin_transaction(log, db); spider(log, db, config.hash, config.tree); commit_transaction(log, db); sleep(config.sleep); } finalize_statements(log); sqlite_service_close(log, db); curl_global_cleanup(); return 0; }
static int reopen_transaction (PsqlDB *db) { if (end_transaction (db) == -1) return -1; if (begin_transaction (db) == -1) return -1; return 0; }
bool import_pos(){ auto oPath = xtd::filesystem::home_directory_path() + "/xtl.db"; auto oDB = xtd::sqlite::database::open_database(oPath); auto oRS = oDB->execute_reader<int>("Select Count(*) From sqlite_master Where Type='table' And Name='scowl_pos';"); if (!oRS->next() || !oRS->get<0>()){ oDB->execute("Create Table scowl_pos (lemma string, pos int);"); } auto oCmd = oDB->prepare<xtd::string, int>("Insert Into scowl_pos (lemma, pos) Values (?, ?);"); std::ifstream in; in.open(XTD_ASSETS_DIR "/scowl/pos/part-of-speech.txt"); if (in.bad()) return false; auto oTrans = oDB->begin_transaction(); while (!in.eof()){ xtd::string sLine; std::getline(in, sLine); if (0==sLine.size()) continue; auto sParts = sLine.split({'\t'}); if (sParts.size() < 2) continue; xtd::string sLemma = sParts[0]; for (char ch : sParts[1]){ if ('|' == ch) continue; (*oCmd)(sLemma, ch); } } oTrans.commit(); return true; }
/** * Native method void removeAllComponents(int suiteId) of * com.sun.midp.midletsuite.DynamicComponentStorage. * <p> * Removes all dynamic components belonging to the given suite. * <p> * If any component is in use, no components are removed, and * an exception is thrown. * * @param suiteId ID of the suite whose components must be removed * * @throws IllegalArgumentException if there is no suite with * the specified ID * @throws MIDletSuiteLockedException is thrown, if any component is * locked */ KNIEXPORT KNI_RETURNTYPE_VOID KNIDECL(com_sun_midp_midletsuite_DynamicComponentStorage_removeAllComponents) { MIDPError status; SuiteIdType suiteId = KNI_GetParameterAsInt(1); do { status = begin_transaction(TRANSACTION_REMOVE_ALL_COMPONENTS, suiteId, NULL); if (status != ALL_OK) { break; } status = delete_components_files(suiteId, UNUSED_COMPONENT_ID); if (status != ALL_OK) { break; } (void)remove_from_component_list_and_save(suiteId, UNUSED_COMPONENT_ID); } while(0); (void)finish_transaction(); if (status == SUITE_LOCKED) { KNI_ThrowNew(midletsuiteLocked, NULL); } else if (status == BAD_PARAMS) { KNI_ThrowNew(midpIllegalArgumentException, "bad component ID"); } else if (status != ALL_OK) { KNI_ThrowNew(midpRuntimeException, (status == OUT_OF_MEMORY) ? "Remove failed" : "Out of memory!"); } KNI_ReturnVoid(); }
/* * Query table for specified rows * _h: structure representing database connection * _k: key names * _op: operators * _v: values of the keys that must match * _c: column names to return * _n: nmber of key=values pairs to compare * _nc: number of columns to return * _o: order by the specified column */ int db_query(db_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, db_key_t* _c, int _n, int _nc, db_key_t _o, db_res_t** _r) { int off, rv; if (!_c) { off = snprintf(sql_buf, SQL_BUF_LEN, "select * from %s ", CON_TABLE(_h)); } else { off = snprintf(sql_buf, SQL_BUF_LEN, "select "); off += print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc); off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "from %s ", CON_TABLE(_h)); } if (_n) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "where "); off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k, _op, _v, _n); } if (_o) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "order by %s", _o); } if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_query(): Error while submitting query\n"); return -2; } rv = get_result(_h, _r); free_query(_h); commit_transaction(_h); return(rv); }
int MAINTmsg(int argc, char *argv[]) { extern MEMOBJ sqlmem; extern int aktuellesprache; extern FILE *FPout; MP_MSG *msgs; char query[RECLAENGE]; int nextid; int previd; int status; int anzahl; int i; if (outputsprache == (int)UNDEF) { aktuellesprache = dialogsprache; } else { aktuellesprache = outputsprache; } /* F�r dynamisches Select aus */ status = begin_transaction(SQLISOLATION_RU, SQLPRIO_NORMAL); if (status != (int)MPOK) return(status); (void)sprintf(query, _SELECT_ALL_MSG, (short)aktuellesprache); status = dynamic_sql_query((int)_MSG, &sqlmem.buffer, query, &anzahl, &sqlmem.datalen, &sqlmem.alloclen); (void)end_transaction(); /* Pr�fe Status und Anzahl */ if (status != (int)MPOK) return(status); if (anzahl < 1) return(status); /* Setzte MSG pointer */ msgs=(MP_MSG *)sqlmem.buffer; /* Titel */ fprintf(FPout, "\n\n"); fprintf(FPout, "Typ ID TextNr Text\n"); fprintf(FPout, "=====================================================\n\n"); /* alle Eintr�ge */ i = 0; fprintf(FPout, "%3d %7d %6d %s\n", msgs[i].msg_typ, msgs[i].msg_id, msgs[i].textnr, msgs[i].text); for(i=1; i<anzahl; i++) { previd = msgs[(i-1)].msg_id; nextid = msgs[i].msg_id; if (previd != nextid) fprintf(FPout, "------------------------------------------------------\n\n"); fprintf(FPout, "%3d %7d %6d %s\n", msgs[i].msg_typ, msgs[i].msg_id, msgs[i].textnr, msgs[i].text); } return(status); }
/* This function initializes the database the first time it is opened. * This function sets the KMO error string. It returns -1 on failure. */ static int maildb_sqlite_initialize(sqlite3 *db) { begin_transaction(db); if (sqlite3_exec(db, "CREATE TABLE 'pwd' ('email' varchar(320), 'pwd' varchar(256));" "CREATE UNIQUE INDEX 'pwd_index' ON pwd (email);" "CREATE TABLE 'sender' ('mid' integer(20), 'name' varchar(100));" "CREATE UNIQUE INDEX 'sender_index' ON sender (mid);" "CREATE TABLE 'mail_msg_id' ('msg_id' VARCHAR(20), 'entry_id' INTEGER);" "CREATE UNIQUE INDEX 'msg_id_index' ON mail_msg_id (msg_id);" "CREATE TABLE 'mail_eval_res3' " "(" " 'entry_id' INTEGER PRIMARY KEY," " 'hash' BLOB (20)," " 'ksn' BLOB (24)," " 'status' INTEGER," " 'display_pref' TINYINT(1)," " 'sig_msg' VARCHAR," " 'mid' INTEGER(20)," " 'original_packaging' TINYINT(1)," " 'mua' SMALLINT(3)," " 'field_status' INTEGER(2)," " 'att_plugin_nbr' TINYINT(1)," " 'attachment_nbr' TINYINT(1)," " 'attachment_status' BLOB," " 'sym_key' BLOB," " 'encryption_status' TINYINT(1)," " 'decryption_error_msg' VARCHAR," " 'pod_status' TINYINT(1)," " 'pod_msg' VARCHAR," " 'otut_status' TINYINT(1)," " 'otut_string' BLOB," " 'otut_msg' VARCHAR," " 'kpg_addr' VARCHAR," " 'kpg_port' INTEGER(5)" ");" "CREATE INDEX 'hash_index' ON mail_eval_res3 (hash);" "CREATE INDEX 'entry_id_index' ON mail_msg_id (entry_id);" "CREATE TABLE 'maildb_version' ('version' TINYINT(1));" "INSERT INTO maildb_version (version) VALUES (4);" "COMMIT;", NULL, NULL, NULL)) { kmo_seterror("database initialization failed: %s", sqlite3_errmsg(db)); rollback_transaction(db); return -1; } return 0; }
void persistence_sqlite::arm_timer() { timeout_.cancel(); timeout_.expires_from_now(boost::posix_time::milliseconds(500)); timeout_.async_wait(boost::bind(&persistence_sqlite::timeout, this, boost::asio::placeholders::error)); begin_transaction(); }
int main(int argc, char *argv[]) { int ii; int jj; #define TXN_COUNT 10000 #define LOOP_COUNT 10 GlobalTransactionId gxid[TXN_COUNT]; GTM_Conn *conn; char connect_string[100]; sprintf(connect_string, "host=localhost port=6666 node_name=one remote_type=%d", GTM_NODE_COORDINATOR); conn = PQconnectGTM(connect_string); if (conn == NULL) { client_log(("Error in connection\n")); exit(1); } for (jj = 0; jj < LOOP_COUNT; jj++) { for (ii = 0; ii < TXN_COUNT; ii++) { int kk; GTM_Snapshot snapshot; gxid[ii] = begin_transaction(conn, GTM_ISOLATION_RC); if (gxid[ii] != InvalidGlobalTransactionId) client_log(("Started a new transaction (GXID:%u)\n", gxid[ii])); else client_log(("BEGIN transaction failed for ii=%d\n", ii)); snapshot = get_snapshot(conn, gxid[ii], true); if (ii % 2 == 0) { if (!abort_transaction(conn, gxid[ii])) client_log(("ROLLBACK successful (GXID:%u)\n", gxid[ii])); else client_log(("ROLLBACK failed (GXID:%u)\n", gxid[ii])); } else { if (!commit_transaction(conn, gxid[ii])) client_log(("COMMIT successful (GXID:%u)\n", gxid[ii])); else client_log(("COMMIT failed (GXID:%u)\n", gxid[ii])); } } } GTMPQfinish(conn); return 0; }
/* * Execute a raw SQL query */ int db_raw_query(db_con_t* _h, char* _s, db_res_t** _r) { int rv; if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, _s) < 0) { LOG(L_ERR, "db_raw_query(): Error while submitting query\n"); return -2; } rv = get_result(_h, _r); free_query(_h); commit_transaction(_h); return(rv); }
void test01() { GlobalTransactionId gxid; int rc; char host[1024]; printf("\n=== test01:node_register ===\n"); setUp(); node_get_local_addr(conn, host, sizeof(host)); /* * starting */ rc = node_register_internal(conn, GTM_NODE_GTM, host, 6667, "One zero two", "/tmp/pgxc/data/gtm_standby", NODE_DISCONNECTED); _ASSERT(rc == 0); rc = node_unregister(conn, GTM_NODE_GTM, "One zero two"); _ASSERT(rc == 0); rc = node_register_internal(conn, GTM_NODE_GTM, host, 6667, "One zero two", "/tmp/pgxc/data/gtm_standby", NODE_CONNECTED); _ASSERT(rc == 0); sleep(10); gxid = begin_transaction(conn, GTM_ISOLATION_SERIALIZABLE, timestamp); _ASSERT( gxid!=InvalidGlobalTransactionId ); rc = prepare_transaction(conn, gxid); _ASSERT( rc!=-1 ); rc = abort_transaction(conn, gxid); _ASSERT( rc!=-1 ); sleep(10); /* * closing */ rc = node_unregister(conn, GTM_NODE_GTM, "One zero two"); _ASSERT( rc==0 ); tearDown(); }
/* * Delete a row from the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _n: number of key=value pairs */ int db_delete(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n) { int off; off = snprintf(sql_buf, SQL_BUF_LEN, "delete from %s", CON_TABLE(_h)); if (_n) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, " where "); off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n); } if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_delete(): Error while deleting\n"); return -2; } free_query(_h); commit_transaction(_h); return(0); }
/* * Insert a row into specified table * _h: structure representing database connection * _k: key names * _v: values of the keys * _n: number of key=value pairs */ int db_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) { int off; off = snprintf(sql_buf, SQL_BUF_LEN, "insert into %s (", CON_TABLE(_h)); off += print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); off += snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); off += print_values(sql_buf + off, SQL_BUF_LEN - off, _v, _n); *(sql_buf + off++) = ')'; *(sql_buf + off) = '\0'; if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_insert(): Error while inserting\n"); return -2; } free_query(_h); commit_transaction(_h); return(0); }
bool db_cnx::next_seq_val(const char* seqName, int* id) { try { begin_transaction(); sql_stream update(QString("UPDATE sequence set value=value+1 where name='%1'").arg(seqName), *this); sql_stream select(QString("SELECT value from sequence where name='%1'").arg(seqName), *this); commit_transaction(); select >> *id; -- *id; } catch(db_excpt& p) { DBEXCPT(p); return false; } return true; }
/** * Native method void removeComponent(int componentId) of * com.sun.midp.midletsuite.DynamicComponentStorage. * <p> * Removes a dynamic component given its ID. * <p> * If the component is in use it must continue to be available * to the other components that are using it. * * @param componentId ID of the component ot remove * * @throws IllegalArgumentException if the component cannot be found * @throws MIDletSuiteLockedException is thrown, if the component is * locked */ KNIEXPORT KNI_RETURNTYPE_VOID KNIDECL(com_sun_midp_midletsuite_DynamicComponentStorage_removeComponent) { MIDPError status; MidletSuiteData* pData; ComponentIdType componentId = KNI_GetParameterAsInt(1); do { /* check that the component exists */ pData = get_component_data(componentId); if (pData == NULL) { status = NOT_FOUND; break; } status = begin_transaction(TRANSACTION_REMOVE_COMPONENT, componentId, NULL); if (status != ALL_OK) { break; } status = delete_components_files(pData->suiteId, componentId); if (status != ALL_OK) { break; } (void)remove_from_component_list_and_save(pData->suiteId, componentId); } while(0); (void)finish_transaction(); if (status == SUITE_LOCKED) { KNI_ThrowNew(midletsuiteLocked, NULL); } else if (status == BAD_PARAMS || status == NOT_FOUND) { KNI_ThrowNew(midpIllegalArgumentException, "bad component ID"); } else if (status != ALL_OK) { KNI_ThrowNew(midpRuntimeException, (status == OUT_OF_MEMORY) ? "Out of memory!" : "Remove failed"); } KNI_ReturnVoid(); }
void check_out_impute(char **origin, char **destin, int *imputation_number, char **subset, char **filltabin){ char *filltab = (filltabin && *filltabin) ? *filltabin : "filled"; Tea_stopif(!origin || !*origin, return, 0, "NULL origin table, but I need that."); char *id_column= get_key_word(NULL, "id"); const char *dest = destin ? *destin : NULL; int use_rowids = 0; if (!id_column) { use_rowids++; id_column = strdup("rowid"); } sprintf(apop_opts.db_name_column, "%s", id_column); begin_transaction(); if (dest && strcmp(*origin, dest)){ apop_table_exists(dest, 'd'); apop_query("create table %s as select %s * from %s %s %s", dest, use_rowids ? "rowid as id_col, " : " ", *origin, (subset && *subset) ? "where" : " ", (subset && *subset) ? *subset : " " ); } else dest = *origin; create_index(dest, use_rowids ? "id_col" : id_column); Tea_stopif(!apop_table_exists(filltab), return , 0, "No table named '%s'; did you already doMImpute()?", filltab); apop_data *fills = apop_query_to_text("select %s, field, value from %s where (draw=%i or draw = -1)" , id_column, filltab, *imputation_number); Tea_stopif(!fills || fills->error, return, 0, "Expected fill-in table " "%s, but couldn't query it.", filltab); for(int i=0; i< *fills->textsize; i++){ _Bool is_null = !strcmp(fills->text[i][1], apop_opts.nan_string); char tick = is_null ? ' ' : '\''; apop_query("update %s set %s = %c%s%c " "where cast(%s as numeric) = %s", dest, fills->text[i][0], tick, is_null ? "NULL" : fills->text[i][1], tick, id_column, fills->names->row[i]); } commit_transaction(); apop_data_free(fills); free(id_column); }
/* * Update some rows in the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _uk: updated columns * _uv: updated values of the columns * _n: number of key=value pairs * _un: number of columns to update */ int db_update(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) { int off; off = snprintf(sql_buf, SQL_BUF_LEN, "update %s set ", CON_TABLE(_h)); off += print_set(sql_buf + off, SQL_BUF_LEN - off, _uk, _uv, _un); if (_n) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, " where "); off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n); *(sql_buf + off) = '\0'; } if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_update(): Error while updating\n"); return -2; } free_query(_h); commit_transaction(_h); return(0); }
/** * Safely resave a database by renaming all of its tables, recreating * everything, and then dropping the backup tables only if there were * no errors. If there are errors, drop the new tables and restore the * originals. * * @param book: QofBook to be saved in the database. */ template <DbType Type> void GncDbiBackend<Type>::safe_sync (QofBook* book) { auto conn = dynamic_cast<GncDbiSqlConnection*>(m_conn); g_return_if_fail (conn != nullptr); g_return_if_fail (book != nullptr); ENTER ("book=%p, primary=%p", book, m_book); if (!conn->begin_transaction()) { LEAVE("Failed to obtain a transaction."); return; } if (!conn->table_operation (TableOpType::backup)) { conn->rollback_transaction(); LEAVE ("Failed to rename tables"); return; } if (!conn->drop_indexes()) { conn->rollback_transaction(); LEAVE ("Failed to drop indexes"); return; } sync(m_book); if (check_error()) { conn->rollback_transaction(); LEAVE ("Failed to create new database tables"); return; } conn->table_operation (TableOpType::drop_backup); conn->commit_transaction(); LEAVE ("book=%p", m_book); }
void test_txn4_02() { GlobalTransactionId gxid; int rc; SETUP(); gxid = begin_transaction(conn, GTM_ISOLATION_SERIALIZABLE, timestamp); _ASSERT( gxid != InvalidGlobalTransactionId ); rc = prepare_transaction(conn, gxid); _ASSERT( rc>=0 ); rc = abort_transaction(conn, gxid); _ASSERT( rc>=0 ); _ASSERT( grep_count(LOG_STANDBY, "Sending transaction id 4")==1 ); _ASSERT( grep_count(LOG_STANDBY, "Preparing transaction id 4")==1 ); _ASSERT( grep_count(LOG_STANDBY, "Cancelling transaction id 4")==1 ); TEARDOWN(); }
void check_out_impute(char **origin, char **destin, int *imputation_number, char **subset, char **filltabin){ char *filltab = (filltabin && *filltabin) ? *filltabin : "filled"; Apop_stopif(!origin || !*origin, return, 0, "NULL origin table, but I need that."); char *id_column= get_key_word(NULL, "id"); const char *dest = destin ? *destin : NULL; int use_rowids = 0; if (!id_column) { use_rowids++; id_column = strdup("rowid"); } sprintf(apop_opts.db_name_column, "%s", id_column); if (dest){ apop_table_exists(dest, 'd'); apop_query("create table %s as select %s * from %s %s %s", dest, use_rowids ? "rowid as id_col, " : " ", *origin, (subset && *subset) ? "where" : " ", (subset && *subset) ? *subset : " " ); } else dest = *origin; has_sqlite3_index(dest, use_rowids ? "id_col" : id_column, 'y'); Apop_stopif(!apop_table_exists(filltab), return , 0, "No table named '%s'; did you already doMImpute()?", filltab); apop_data *fills = apop_query_to_text("select %s, field, value from %s where draw+0.0=%i" , id_column, filltab, *imputation_number); Apop_stopif(!fills || fills->error, return, 0, "Expected fill-in table " "%s, but couldn't query it.", filltab); begin_transaction(); if (fills) for(int i=0; i< *fills->textsize; i++) apop_query("update %s set %s = '%s' " "where %s = %s", dest, fills->text[i][0], fills->text[i][1], id_column, fills->names->row[i]); commit_transaction(); apop_data_free(fills); free(id_column); }
/** * Change the enabled state of a suite. * * @param suiteId ID of the suite * @param enabled true if the suite is be enabled * * @return an error code (ALL_OK if no errors) */ static MIDPError change_enabled_state(SuiteIdType suiteId, jboolean enabled) { char* pszError; MIDPError status; jbyte* pPermissions; int numberOfPermissions; jbyte pushInterrupt; jint pushOptions; jboolean temp; lockStorageList* node; MidletSuiteData* pData; /* * This is a public API which can be called without the VM running * so we need automatically init anything needed, to make the * caller's code less complex. * * Initialization is performed in steps so that we do use any * extra resources such as the VM for the operation being performed. */ if (midpInit(LIST_LEVEL) != 0) { return OUT_OF_MEMORY; } status = midp_suite_exists(suiteId); if ((status != ALL_OK) && (status != SUITE_CORRUPTED_ERROR)) { return status; } node = find_storage_lock(suiteId); if (node != NULL) { if (node->update == KNI_TRUE) { /* Suite is being updated currently. */ return SUITE_LOCKED; } } status = read_settings(&pszError, suiteId, &temp, &pushInterrupt, &pushOptions, &pPermissions, &numberOfPermissions); if (status != ALL_OK) { storageFreeError(pszError); return status; } status = begin_transaction(TRANSACTION_ENABLE_SUITE, suiteId, NULL); if (status != ALL_OK) { return status; } status = write_settings(&pszError, suiteId, enabled, pushInterrupt, pushOptions, pPermissions, numberOfPermissions, NULL); pcsl_mem_free(pPermissions); if (status != ALL_OK) { storageFreeError(pszError); /* nothing was written, so nothing to rollback, just finish */ (void)finish_transaction(); return status; } /* synchronize the settings in the list of MidletSuiteData structures */ pData = get_suite_data(suiteId); /* * We can assert that pData is not NULL because midp_suite_exists() * was called above to ensure that the suite with the given ID exists. */ if (pData != NULL) { int status; char* pszError; pData->isEnabled = enabled; /* IMPL_NOTE: these settings must be cached and saved on AMS exit. */ status = write_suites_data(&pszError); storageFreeError(pszError); if (status != ALL_OK) { (void)rollback_transaction(); return IO_ERROR; } } (void)finish_transaction(); return ALL_OK; }
void do_bdr_unregister(void) { PGconn *conn = NULL; ExtensionStatus extension_status = REPMGR_UNKNOWN; int target_node_id = UNKNOWN_NODE_ID; t_node_info node_info = T_NODE_INFO_INITIALIZER; RecordStatus record_status = RECORD_NOT_FOUND; bool node_record_deleted = false; PQExpBufferData event_details; char *dbname; /* sanity-check configuration for BDR-compatability */ if (config_file_options.replication_type != REPLICATION_TYPE_BDR) { log_error(_("cannot run BDR UNREGISTER on a non-BDR node")); exit(ERR_BAD_CONFIG); } dbname = pg_malloc0(MAXLEN); if (dbname == NULL) { log_error(_("unable to allocate memory; terminating.")); exit(ERR_OUT_OF_MEMORY); } /* store the database name for future reference */ get_conninfo_value(config_file_options.conninfo, "dbname", dbname); conn = establish_db_connection(config_file_options.conninfo, true); if (!is_bdr_db(conn, NULL)) { log_error(_("database \"%s\" is not BDR-enabled"), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } extension_status = get_repmgr_extension_status(conn, NULL); if (extension_status != REPMGR_INSTALLED) { log_error(_("repmgr is not installed on database \"%s\""), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } pfree(dbname); if (!is_bdr_repmgr(conn)) { log_error(_("repmgr metadatabase contains records for non-BDR nodes")); PQfinish(conn); exit(ERR_BAD_CONFIG); } initPQExpBuffer(&event_details); if (runtime_options.node_id != UNKNOWN_NODE_ID) target_node_id = runtime_options.node_id; else target_node_id = config_file_options.node_id; /* Check node exists and is really a BDR node */ record_status = get_node_record(conn, target_node_id, &node_info); if (record_status != RECORD_FOUND) { log_error(_("no record found for node %i"), target_node_id); PQfinish(conn); exit(ERR_BAD_CONFIG); } begin_transaction(conn); log_debug("unregistering node %i", target_node_id); node_record_deleted = delete_node_record(conn, target_node_id); if (node_record_deleted == false) { appendPQExpBuffer(&event_details, "unable to delete node record for node \"%s\" (ID: %i)", node_info.node_name, target_node_id); rollback_transaction(conn); } else { appendPQExpBuffer(&event_details, "node record deleted for node \"%s\" (ID: %i)", node_info.node_name, target_node_id); commit_transaction(conn); } /* Log the event */ create_event_notification( conn, &config_file_options, config_file_options.node_id, "bdr_unregister", true, event_details.data); PQfinish(conn); log_notice(_("bdr node \"%s\" (ID: %i) successfully unregistered"), node_info.node_name, target_node_id); termPQExpBuffer(&event_details); return; }
/* * do_bdr_register() * * As each BDR node is its own primary, registering a BDR node * will create the repmgr metadata schema if necessary. */ void do_bdr_register(void) { PGconn *conn = NULL; BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER; ExtensionStatus extension_status = REPMGR_UNKNOWN; t_node_info node_info = T_NODE_INFO_INITIALIZER; RecordStatus record_status = RECORD_NOT_FOUND; PQExpBufferData event_details; bool success = true; char *dbname = NULL; /* sanity-check configuration for BDR-compatability */ if (config_file_options.replication_type != REPLICATION_TYPE_BDR) { log_error(_("cannot run BDR REGISTER on a non-BDR node")); exit(ERR_BAD_CONFIG); } dbname = pg_malloc0(MAXLEN); if (dbname == NULL) { log_error(_("unable to allocate memory; terminating.")); exit(ERR_OUT_OF_MEMORY); } /* store the database name for future reference */ get_conninfo_value(config_file_options.conninfo, "dbname", dbname); conn = establish_db_connection(config_file_options.conninfo, true); if (!is_bdr_db(conn, NULL)) { log_error(_("database \"%s\" is not BDR-enabled"), dbname); log_hint(_("when using repmgr with BDR, the repmgr schema must be stored in the BDR database")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } /* Check that there are at most 2 BDR nodes */ get_all_bdr_node_records(conn, &bdr_nodes); if (bdr_nodes.node_count == 0) { log_error(_("database \"%s\" is BDR-enabled but no BDR nodes were found"), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } /* BDR 2 implementation is for 2 nodes only */ if (get_bdr_version_num() < 3 && bdr_nodes.node_count > 2) { log_error(_("repmgr can only support BDR 2.x clusters with 2 nodes")); log_detail(_("this BDR cluster has %i nodes"), bdr_nodes.node_count); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } if (get_bdr_version_num() > 2) { log_error(_("\"repmgr bdr register\" is for BDR 2.x only")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } /* check for a matching BDR node */ { PQExpBufferData bdr_local_node_name; bool node_match = false; initPQExpBuffer(&bdr_local_node_name); node_match = bdr_node_name_matches(conn, config_file_options.node_name, &bdr_local_node_name); if (node_match == false) { if (strlen(bdr_local_node_name.data)) { log_error(_("local node BDR node name is \"%s\", expected: \"%s\""), bdr_local_node_name.data, config_file_options.node_name); log_hint(_("\"node_name\" in repmgr.conf must match \"node_name\" in bdr.bdr_nodes")); } else { log_error(_("local node does not report BDR node name")); log_hint(_("ensure this is an active BDR node")); } PQfinish(conn); pfree(dbname); termPQExpBuffer(&bdr_local_node_name); exit(ERR_BAD_CONFIG); } termPQExpBuffer(&bdr_local_node_name); } /* check whether repmgr extension exists, and there are no non-BDR nodes registered */ extension_status = get_repmgr_extension_status(conn, NULL); if (extension_status == REPMGR_UNKNOWN) { log_error(_("unable to determine status of \"repmgr\" extension in database \"%s\""), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } if (extension_status == REPMGR_UNAVAILABLE) { log_error(_("\"repmgr\" extension is not available")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } if (extension_status == REPMGR_INSTALLED) { if (!is_bdr_repmgr(conn)) { log_error(_("repmgr metadatabase contains records for non-BDR nodes")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } } else { log_debug("creating repmgr extension in database \"%s\"", dbname); begin_transaction(conn); if (!create_repmgr_extension(conn)) { log_error(_("unable to create repmgr extension - see preceding error message(s); aborting")); rollback_transaction(conn); pfree(dbname); PQfinish(conn); exit(ERR_BAD_CONFIG); } commit_transaction(conn); } pfree(dbname); if (bdr_node_has_repmgr_set(conn, config_file_options.node_name) == false) { log_debug("bdr_node_has_repmgr_set() = false"); bdr_node_set_repmgr_set(conn, config_file_options.node_name); } /* * before adding the extension tables to the replication set, if any other * BDR nodes exist, populate repmgr.nodes with a copy of existing entries * * currently we won't copy the contents of any other tables * */ { NodeInfoList local_node_records = T_NODE_INFO_LIST_INITIALIZER; (void) get_all_node_records(conn, &local_node_records); if (local_node_records.node_count == 0) { BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER; BdrNodeInfoListCell *bdr_cell = NULL; get_all_bdr_node_records(conn, &bdr_nodes); if (bdr_nodes.node_count == 0) { log_error(_("unable to retrieve any BDR node records")); log_detail("%s", PQerrorMessage(conn)); PQfinish(conn); exit(ERR_BAD_CONFIG); } for (bdr_cell = bdr_nodes.head; bdr_cell; bdr_cell = bdr_cell->next) { PGconn *bdr_node_conn = NULL; NodeInfoList existing_nodes = T_NODE_INFO_LIST_INITIALIZER; NodeInfoListCell *cell = NULL; ExtensionStatus other_node_extension_status = REPMGR_UNKNOWN; /* skip the local node */ if (strncmp(node_info.node_name, bdr_cell->node_info->node_name, sizeof(node_info.node_name)) == 0) { continue; } log_debug("connecting to BDR node \"%s\" (conninfo: \"%s\")", bdr_cell->node_info->node_name, bdr_cell->node_info->node_local_dsn); bdr_node_conn = establish_db_connection_quiet(bdr_cell->node_info->node_local_dsn); if (PQstatus(bdr_node_conn) != CONNECTION_OK) { continue; } /* check repmgr schema exists, skip if not */ other_node_extension_status = get_repmgr_extension_status(bdr_node_conn, NULL); if (other_node_extension_status != REPMGR_INSTALLED) { continue; } (void) get_all_node_records(bdr_node_conn, &existing_nodes); for (cell = existing_nodes.head; cell; cell = cell->next) { log_debug("creating record for node \"%s\" (ID: %i)", cell->node_info->node_name, cell->node_info->node_id); create_node_record(conn, "bdr register", cell->node_info); } PQfinish(bdr_node_conn); break; } } } /* Add the repmgr extension tables to a replication set */ if (get_bdr_version_num() < 3) { add_extension_tables_to_bdr_replication_set(conn); } else { /* this is the only table we need to replicate */ char *replication_set = get_default_bdr_replication_set(conn); /* * this probably won't happen, but we need to be sure we're using * the replication set metadata correctly... */ if (conn == NULL) { log_error(_("unable to retrieve default BDR replication set")); log_hint(_("see preceding messages")); log_debug("check query in get_default_bdr_replication_set()"); exit(ERR_BAD_CONFIG); } if (is_table_in_bdr_replication_set(conn, "nodes", replication_set) == false) { add_table_to_bdr_replication_set(conn, "nodes", replication_set); } pfree(replication_set); } initPQExpBuffer(&event_details); begin_transaction(conn); /* * we'll check if a record exists (even if the schema was just created), * as there's a faint chance of a race condition */ record_status = get_node_record(conn, config_file_options.node_id, &node_info); /* Update internal node record */ node_info.type = BDR; node_info.node_id = config_file_options.node_id; node_info.upstream_node_id = NO_UPSTREAM_NODE; node_info.active = true; node_info.priority = config_file_options.priority; strncpy(node_info.node_name, config_file_options.node_name, sizeof(node_info.node_name)); strncpy(node_info.location, config_file_options.location, sizeof(node_info.location)); strncpy(node_info.conninfo, config_file_options.conninfo, sizeof(node_info.conninfo)); if (record_status == RECORD_FOUND) { bool node_updated = false; /* * At this point we will have established there are no non-BDR * records, so no need to verify the node type */ if (!runtime_options.force) { log_error(_("this node is already registered")); log_hint(_("use -F/--force to overwrite the existing node record")); rollback_transaction(conn); PQfinish(conn); exit(ERR_BAD_CONFIG); } /* * don't permit changing the node name - this must match the BDR node * name set when the node was registered. */ if (strncmp(node_info.node_name, config_file_options.node_name, sizeof(node_info.node_name)) != 0) { log_error(_("a record for node %i is already registered with node_name \"%s\""), config_file_options.node_id, node_info.node_name); log_hint(_("node_name configured in repmgr.conf is \"%s\""), config_file_options.node_name); rollback_transaction(conn); PQfinish(conn); exit(ERR_BAD_CONFIG); } node_updated = update_node_record(conn, "bdr register", &node_info); if (node_updated == true) { appendPQExpBuffer(&event_details, _("node record updated for node \"%s\" (%i)"), config_file_options.node_name, config_file_options.node_id); log_verbose(LOG_NOTICE, "%s", event_details.data); } else { success = false; } } else { /* create new node record */ bool node_created = create_node_record(conn, "bdr register", &node_info); if (node_created == true) { appendPQExpBuffer(&event_details, _("node record created for node \"%s\" (ID: %i)"), config_file_options.node_name, config_file_options.node_id); log_notice("%s", event_details.data); } else { success = false; } } if (success == false) { rollback_transaction(conn); PQfinish(conn); exit(ERR_DB_QUERY); } commit_transaction(conn); /* Log the event */ create_event_notification( conn, &config_file_options, config_file_options.node_id, "bdr_register", true, event_details.data); termPQExpBuffer(&event_details); PQfinish(conn); log_notice(_("BDR node %i registered (conninfo: %s)"), config_file_options.node_id, config_file_options.conninfo); return; }
void update(int pass) { char *tsmd_name; int tsmd_rowcnt, tsmd_processed, tsmd_rowid, ec; stmt_timeseriesmetadata.sql = SQL_TIMESERIESMETADATA; ec = PREPARE(adb, stmt_timeseriesmetadata); if (ec != SQLITE_OK) { APPERR("prepare", stmt_timeseriesmetadata.sql, ec); exit(1); } do { ec = STEP(stmt_timeseriesmetadata); if (ec == SQLITE_ROW) { /* * 0 1 2 3 * rowid, tsmd_name, tsmd_rowcnt, tsmd_processed */ tsmd_rowid = sqlite3_column_int(stmt_timeseriesmetadata.stmtp, 0); tsmd_name = (char *)sqlite3_column_text(stmt_timeseriesmetadata.stmtp, 1); tsmd_rowcnt = sqlite3_column_int(stmt_timeseriesmetadata.stmtp, 2); tsmd_processed = sqlite3_column_int(stmt_timeseriesmetadata.stmtp, 3); printf("Processing rowid %d name %s rowcnt %d processed %d\n", tsmd_rowid, tsmd_name, tsmd_rowcnt, tsmd_processed); switch(pass) { case 1: update_day_pass1(tsmd_name); break; case 2: begin_transaction(); update_day_pass2(tsmd_name); end_transaction(); break; case 3: update_day_pass3(tsmd_name); break; default: printf("invalude pass number %d\n", pass); exit(1); } } } while (ec == SQLITE_ROW); if (ec != SQLITE_DONE) { APPERR("step", stmt_timeseriesmetadata.sql, ec); exit(1); } ec = FINALIZE(stmt_timeseriesmetadata); if (ec != SQLITE_OK) { APPERR("finalize", stmt_timeseriesmetadata.sql, ec); exit(1); } }
/** Create or open an PostgreSQL database * \param db the databse to associate with the sqlite3 database * \return 0 if successful, -1 otherwise */ int psql_create_database(Database* db) { MString *conninfo; MString *str; PGconn *conn; PGresult *res = NULL; int ret = -1; loginfo ("psql:%s: Accessing database\n", db->name); /* * Make a connection to the database server -- check if the * requested database exists or not by connecting to the 'postgres' * database and querying that. */ conninfo = psql_prepare_conninfo("postgres", pg_host, pg_port, pg_user, pg_pass, pg_conninfo); conn = PQconnectdb(mstring_buf (conninfo)); /* Check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { logerror ("psql: Could not connect to PostgreSQL database (conninfo \"%s\"): %s\n", mstring_buf(conninfo), PQerrorMessage (conn)); goto cleanup_exit; } PQsetNoticeReceiver(conn, psql_receive_notice, "postgres"); str = mstring_create(); mstring_sprintf (str, "SELECT datname from pg_database where datname='%s';", db->name); res = PQexec (conn, mstring_buf (str)); if (PQresultStatus (res) != PGRES_TUPLES_OK) { logerror ("psql: Could not get list of existing databases\n"); goto cleanup_exit; } /* No result rows means database doesn't exist, so create it instead */ if (PQntuples (res) == 0) { loginfo ("psql:%s: Database does not exist, creating it\n", db->name); mstring_set (str, ""); mstring_sprintf (str, "CREATE DATABASE \"%s\";", db->name); res = PQexec (conn, mstring_buf (str)); if (PQresultStatus (res) != PGRES_COMMAND_OK) { logerror ("psql:%s: Could not create database: %s\n", db->name, PQerrorMessage (conn)); goto cleanup_exit; } } PQfinish (conn); mstring_delete(conninfo); /* Now that the database should exist, make a connection to the it for real */ conninfo = psql_prepare_conninfo(db->name, pg_host, pg_port, pg_user, pg_pass, pg_conninfo); conn = PQconnectdb(mstring_buf (conninfo)); /* Check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { logerror ("psql:%s: Could not connect to PostgreSQL database (conninfo \"%s\"): %s\n", db->name, mstring_buf(conninfo), PQerrorMessage (conn)); goto cleanup_exit; } PQsetNoticeReceiver(conn, psql_receive_notice, db->name); PsqlDB* self = (PsqlDB*)xmalloc(sizeof(PsqlDB)); self->conn = conn; self->last_commit = time (NULL); db->create = psql_create_database; db->release = psql_release; db->table_create = psql_table_create; db->table_create_meta = psql_table_create_meta; db->table_free = psql_table_free; db->insert = psql_insert; db->add_sender_id = psql_add_sender_id; db->get_metadata = psql_get_metadata; db->set_metadata = psql_set_metadata; db->get_uri = psql_get_uri; db->get_table_list = psql_get_table_list; db->handle = self; begin_transaction (self); /* Everything was successufl, prepare for cleanup */ ret = 0; cleanup_exit: if (res) PQclear (res); if (ret) PQfinish (conn); /* If return !=0, cleanup connection */ mstring_delete (str); mstring_delete (conninfo); return ret; }
/** * Moves the given midlet suite to another folder. * * @param suiteId ID of the suite * @param newFolderId ID of the folder where the suite must be moved * * @return ALL_OK if no errors or an error code */ MIDPError midp_move_suite_to_folder(SuiteIdType suiteId, FolderIdType newFolderId) { MIDPError status = ALL_OK; char* pszError = NULL; lockStorageList *node = NULL; MidletSuiteData* pSuiteData; FolderIdType oldFolderId; /* * This is a public API which can be called without the VM running * so we need automatically init anything needed, to make the * caller's code less complex. * * Initialization is performed in steps so that we do use any * extra resources such as the VM for the operation being performed. */ if (midpInit(REMOVE_LEVEL) != 0) { return OUT_OF_MEMORY; } /* load _suites.dat */ status = read_suites_data(&pszError); storageFreeError(pszError); if (status != ALL_OK) { return status; } node = find_storage_lock(suiteId); if (node != NULL) { if (node->update != KNI_TRUE) { return SUITE_LOCKED; } } pSuiteData = get_suite_data(suiteId); if (pSuiteData == NULL) { remove_storage_lock(suiteId); return NOT_FOUND; } status = begin_transaction(TRANSACTION_MOVE_TO_FOLDER, suiteId, NULL); if (status != ALL_OK) { remove_storage_lock(suiteId); return status; } oldFolderId = pSuiteData->folderId; pSuiteData->folderId = newFolderId; status = write_suites_data(&pszError); storageFreeError(pszError); if (status != ALL_OK) { pSuiteData->folderId = oldFolderId; (void)rollback_transaction(); } else { (void)finish_transaction(); } remove_storage_lock(suiteId); return status; }