//向数据库存储数据 BOOL MysqlAddData(UCHAR * Sql) { HSTMT hstmt; RETCODE retcode; //申请语句句柄 retcode = ::SQLAllocStmt(hDbc,&hstmt); if(retcode != SQL_SUCCESS) //申请失败 返回FALSE { //MessageBoxA(NULL,"Data insert fail 1!","tip",MB_OK); return FALSE; } //执行Sql语句 retcode = ::SQLExecDirect(hstmt,Sql,SQL_NTS); if(retcode != SQL_SUCCESS) //执行失败 释放语句句柄 返回FALSE { ::SQLFreeHandle(SQL_HANDLE_STMT,hstmt); //MessageBoxA(NULL,"Data insert fail 2!","tip",MB_OK); return FALSE; } //提交事务 retcode = SQLEndTran(SQL_HANDLE_DBC , hDbc, SQL_COMMIT); ::SQLFreeHandle(SQL_HANDLE_STMT,hstmt); if(retcode == SQL_SUCCESS) { //MessageBoxA(NULL,"Data insert success!","tip",MB_OK); return TRUE; } else { SQLEndTran(SQL_HANDLE_DBC , hDbc, SQL_ROLLBACK); //MessageBoxA(NULL,"Data insert fail 3!","tip",MB_OK); return FALSE; } }
SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_SQLEndTran(switch_odbc_handle_t *handle, switch_bool_t commit) { #ifdef SWITCH_HAVE_ODBC if (commit) { return SQLEndTran(SQL_HANDLE_DBC, handle->con, SQL_COMMIT); } else { return SQLEndTran(SQL_HANDLE_DBC, handle->con, SQL_ROLLBACK); } #else return (switch_odbc_status_t) SWITCH_FALSE; #endif }
void ODBCSession::Rollback() { if(tmode == IMPLICIT) { SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_ROLLBACK); tlevel = 0; return; } tlevel--; ASSERT(tlevel >= 0); if(tlevel == 0) { SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_ROLLBACK); SQLSetConnectAttr(hdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_NTS); } }
SQLRETURN unixodbc_backend_debug::do_end_transaction(SQLSMALLINT handle_type, SQLHANDLE connection_or_environment_handle, SQLSMALLINT completion_type) const { std::cout << " *DEBUG* end_transaction"; auto const return_code = SQLEndTran(handle_type, connection_or_environment_handle, completion_type); std::cout << " (return code " << return_code << ")" << std::endl; return return_code; }
static void insert_test_man(void) { SQLLEN sql_nts = SQL_NTS; SQLINTEGER commit_off = SQL_AUTOCOMMIT_OFF; SQLINTEGER commit_on = SQL_AUTOCOMMIT_ON; SQLINTEGER id = 0; char string[64]; CHKSetConnectAttr(SQL_ATTR_AUTOCOMMIT, int2ptr(commit_off), SQL_IS_INTEGER, "SI"); odbc_reset_statement(); CHKBindParameter(1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, sizeof(id), 0, &id, 0, &sql_nts, "SI"); CHKBindParameter(2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(string), 0, string, 0, &sql_nts, "SI"); CHKPrepare(T("insert into test values (?, ?)"), SQL_NTS, "SI"); for (id = 0; id < 20; id++) { sprintf(string, "This is a test (%d)", (int) id); CHKExecute("SI"); } SQLEndTran(SQL_HANDLE_DBC, odbc_conn, SQL_COMMIT); SQLSetConnectAttr(odbc_conn, SQL_ATTR_AUTOCOMMIT, int2ptr(commit_on), SQL_IS_INTEGER); odbc_reset_statement(); }
bool OdbcConnection::TryEndTran(SQLSMALLINT completionType) { SQLRETURN ret = SQLEndTran(SQL_HANDLE_DBC, connection, completionType); if (ret == SQL_STILL_EXECUTING) { return false; } if (!SQL_SUCCEEDED(ret)) { statement.Throw(); } // put the connection back into auto commit mode ret = SQLSetConnectAttr( connection, SQL_ATTR_AUTOCOMMIT, reinterpret_cast<SQLPOINTER>( SQL_AUTOCOMMIT_ON ), SQL_IS_UINTEGER ); // TODO: This will not work because calling into TryEndTran again from the callback will fail // when the completion has already finished. if (ret == SQL_STILL_EXECUTING) { return false; } if (!SQL_SUCCEEDED(ret)) { statement.Throw(); } return true; }
void SessionImpl::rollback() { if (!isAutoCommit()) checkError(SQLEndTran(SQL_HANDLE_DBC, _db, SQL_ROLLBACK)); _inTransaction = false; }
void SessionImpl::commit() { if (!isAutoCommit()) checkError(SQLEndTran(SQL_HANDLE_DBC, _db, SQL_COMMIT)); _inTransaction = false; }
static int odbc_handle_rollback(pdo_dbh_t *dbh) { pdo_odbc_db_handle *H = (pdo_odbc_db_handle *)dbh->driver_data; RETCODE rc; rc = SQLEndTran(SQL_HANDLE_DBC, H->dbc, SQL_ROLLBACK); if (rc != SQL_SUCCESS) { pdo_odbc_drv_error("SQLEndTran: Rollback"); if (rc != SQL_SUCCESS_WITH_INFO) { return 0; } } if (dbh->auto_commit && H->dbc) { /* turn auto-commit back on again */ rc = SQLSetConnectAttr(H->dbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_IS_INTEGER); if (rc != SQL_SUCCESS) { pdo_odbc_drv_error("SQLSetConnectAttr AUTOCOMMIT = ON"); return 0; } } return 1; }
/* ** Rolls back a transaction. */ static int conn_commit (lua_State *L) { conn_data *conn = (conn_data *) getconnection (L); SQLRETURN ret = SQLEndTran(hDBC, conn->hdbc, SQL_COMMIT); if (error(ret)) return fail(L, hSTMT, conn->hdbc); else return pass(L); }
/* ** Rollback the current transaction. */ static int conn_rollback (lua_State *L) { conn_data *conn = (conn_data *) getconnection (L); SQLRETURN ret = SQLEndTran(hDBC, conn->hdbc, SQL_ROLLBACK); if (error(ret)) return fail(L, hSTMT, conn->hdbc); else return pass(L); }
static void EndTransaction(SQLSMALLINT type) { SQLRETURN ret; ret = SQLEndTran(SQL_HANDLE_DBC, Connection, type); if (ret != SQL_SUCCESS) ODBC_REPORT_ERROR("Can't commit transaction"); }
static int do_rollback(Con& con) { int ret = SQLEndTran(SQL_HANDLE_DBC, con.hdbc, SQL_ROLLBACK); if (ret != SQL_SUCCESS) { ndbout << "Rollback failed" << endl; print_err(SQL_HANDLE_DBC, con.hdbc); return -1; } return 0; }
static int do_commit(Con& con) { int ret = SQLEndTran(SQL_HANDLE_DBC, con.hdbc, SQL_COMMIT); if (ret != SQL_SUCCESS) { ndbout << "Commit failed" << endl; print_err(SQL_HANDLE_DBC, con.hdbc); return -1; } return 0; }
int commit_transaction(struct db_context_t *dbc) { int i; i = SQLEndTran(SQL_HANDLE_DBC, dbc->hdbc, SQL_COMMIT); if (i != SQL_SUCCESS && i != SQL_SUCCESS_WITH_INFO) { LOG_ODBC_ERROR(SQL_HANDLE_STMT, dbc->hstmt); return ERROR; } return OK; }
void ODBCSession::Begin() { if(tmode == IMPLICIT) { SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); tlevel = 0; return; } if(tlevel == 0) SQLSetConnectAttr(hdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, SQL_NTS); tlevel++; }
int rollback_transaction(struct db_context_t *dbc) { int i; i = SQLEndTran(SQL_HANDLE_DBC, dbc->hdbc, SQL_ROLLBACK); if (i != SQL_SUCCESS && i != SQL_SUCCESS_WITH_INFO) { LOG_ODBC_ERROR(SQL_HANDLE_STMT, dbc->hstmt); return ERROR; } return STATUS_ROLLBACK; }
bool CDb2Connection::CommitTrans() { m_lasterror = SQLEndTran(SQL_HANDLE_DBC,m_hDbc,SQL_COMMIT); if(!SQL_SUCCEEDED(m_lasterror)) return false; m_lasterror = SQLSetConnectAttr(m_hDbc,SQL_ATTR_AUTOCOMMIT,(SQLPOINTER)SQL_AUTOCOMMIT_ON,0); if(!SQL_SUCCEEDED(m_lasterror)) return false; return true; }
bool CMssqlConnection::RollbackTrans() { m_lasterror = SQLEndTran(SQL_HANDLE_DBC,m_hDbc,SQL_ROLLBACK); if(!SQL_SUCCEEDED(m_lasterror)) return false; m_lasterror = SQLSetConnectAttr(m_hDbc,SQL_ATTR_AUTOCOMMIT,(SQLPOINTER)SQL_AUTOCOMMIT_ON,0); if(!SQL_SUCCEEDED(m_lasterror)) return false; return true; }
void connection::commit_transaction() { if(not _transaction_active) { throw sqlpp::exception("ODBC error: Cannot commit a finished or failed transaction"); } if(_handle->debug) { std::cerr << "ODBC debug: Committing Transaction\n"; } if(!SQL_SUCCEEDED(SQLEndTran(SQL_HANDLE_DBC, _handle->dbc, SQL_COMMIT))) { throw sqlpp::exception("ODBC error: Could not SQLEndTran COMMIT("+detail::odbc_error(_handle->dbc, SQL_HANDLE_DBC)+')'); } set_autocommit(_handle->dbc); _transaction_active = false; }
void connection::rollback_transaction(bool report) { if(not _transaction_active) { throw sqlpp::exception("ODBC error: Cannot rollback a finished or failed transaction"); } if(report || _handle->debug) { std::cerr << "ODBC warning: Rolling back unfinished transaction" << std::endl; } if(!SQL_SUCCEEDED(SQLEndTran(SQL_HANDLE_DBC, _handle->dbc, SQL_ROLLBACK))) { throw sqlpp::exception("ODBC error: Could not SQLEndTran ROLLBACK("+detail::odbc_error(_handle->dbc, SQL_HANDLE_DBC)+')'); } set_autocommit(_handle->dbc); _transaction_active = false; }
int main(int argc, char **argv) { int rc; HSTMT hstmt = SQL_NULL_HSTMT; test_connect(); rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); if (!SQL_SUCCEEDED(rc)) { print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); exit(1); } /* * Execute an erroneous query, and call SQLGetDiagRec twice on the * statement. Should get the same result both times; SQLGetDiagRec is * not supposed to change the state of the statement. */ rc = SQLExecDirect(hstmt, (SQLCHAR *) "broken query ", SQL_NTS); print_diag("SQLExecDirect", SQL_HANDLE_STMT, hstmt); print_diag("get same message again", SQL_HANDLE_STMT, hstmt); rc = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_ROLLBACK); CHECK_STMT_RESULT(rc, "SQLEndTran failed", hstmt); rc = SQLFreeStmt(hstmt, SQL_DROP); CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); /* kill this connection */ printf ("killing connection...\n"); rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); if (!SQL_SUCCEEDED(rc)) { print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); exit(1); } rc = SQLExecDirect(hstmt, (SQLCHAR *) "select pg_terminate_backend(pg_backend_pid()) ", SQL_NTS); print_diag(NULL, SQL_HANDLE_STMT, hstmt); /* * Test SQLGetDiagRec on the connection, after the backend connection is * dead. Twice, again to check that the first call doesn't clear the * error. */ print_diag("SQLGetDiagRec on connection says:", SQL_HANDLE_DBC, conn); print_diag("SQLGetDiagRec called again:", SQL_HANDLE_DBC, conn); return 0; }
void bgl_odbc_sql_end_tran(obj_t handletype, SQLHANDLE handle, obj_t completiontype) { SQLRETURN v; SQLSMALLINT ht; SQLSMALLINT ct; if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(handletype)), "environment")) { ht = SQL_HANDLE_ENV; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(handletype)), "connection")) { ht = SQL_HANDLE_DBC; } else { odbc_error("bgl_odbc_sql_end_tran", "Invalid handle type", handletype); } if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(completiontype)), "commit")) { ct = SQL_COMMIT; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(handletype)), "rollback")) { ct = SQL_ROLLBACK; } else { odbc_error("bgl_odbc_sql_end_tran", "Invalid completion type", completiontype); } v = SQLEndTran(ht, handle, ct); if(!SQL_SUCCEEDED(v)) { report_odbc_error("bgl_odbc_sql_end_tran", ht, handle); } }
static int odbc_handle_closer(pdo_dbh_t *dbh) { pdo_odbc_db_handle *H = (pdo_odbc_db_handle*)dbh->driver_data; if (H->dbc != SQL_NULL_HANDLE) { SQLEndTran(SQL_HANDLE_DBC, H->dbc, SQL_ROLLBACK); SQLDisconnect(H->dbc); SQLFreeHandle(SQL_HANDLE_DBC, H->dbc); H->dbc = NULL; } SQLFreeHandle(SQL_HANDLE_ENV, H->env); H->env = NULL; pefree(H, dbh->is_persistent); dbh->driver_data = NULL; return 0; }
//////////////////////////////////////////////////////////////////////////////// //with NON_TRANSACTIONAL, it makes durable all of the modifications to active //incarnations whose PIDs are contained in the pids parameter, regardless of the //transactional context of the calling thread // //with TRANSACTIONAL, it behaves as follows: //$$ if the invoking thread is accociated with a transcation context, it makes // durable all state modifications made in the current transactional scope for // incarnations whose PIDs are contained in the pids parameter, flushing them // to the underying datastore. //$$ if the invoking thread is not associated with a transactional context, the // standard exception TRANSACTION_REQUIRED is raised. //if the session pool implementation is unable to reconcile the changes and make //them durable, then the PERSISTENT_STORE standard exception is raised //////////////////////////////////////////////////////////////////////////////// void SessionPoolImpl::flush_by_pids(const PidList& pids) { DEBUG_OUT("CatalogBaseImpl::flush_by_pids() is called"); if( !CanTransact() ) NORMAL_OUT( "CatalogBaseImpl::flush_by_pids() - Database do not support transaction, flush is errorprone!" ); if( access_mode==READ_ONLY ) { NORMAL_ERR( "CatalogBaseImpl::flush_by_pids() - Session pool is read-only!" ); return; } std::string strFlush = ""; std::vector<Pid> vPidList; //there is no function for deleting Pid from PidList, so the PidList should //be first converted in a list of Pid for(CORBA::ULong i=0; i<pids.length(); i++) vPidList.push_back(pids[i]); for( homeBaseIter_=lHomeBases_.begin(); homeBaseIter_!=lHomeBases_.end(); homeBaseIter_++ ) strFlush += (*homeBaseIter_)->getFlushByPid(vPidList); if(ExecuteSQL(strFlush.c_str())) { SQLRETURN ret; ret = SQLEndTran(SQL_HANDLE_DBC, hDbc_, SQL_COMMIT); if(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) { // the flush is successfull, we set the modified-value of each // storage object back to FALSE for( homeBaseIter_=lHomeBases_.begin(); homeBaseIter_!=lHomeBases_.end(); homeBaseIter_++ ) (*homeBaseIter_)->setBatchUnModified(); } else { NORMAL_ERR( "CatalogBaseImpl::flush_by_pids() - Transaction is not successful!" ); } } else { NORMAL_ERR( "CatalogBaseImpl::flush_by_pids() - flush_by_pids is not executed!" ); } }
/****************************************************************************** * * * Function: zbx_db_rollback * * * * Purpose: rollback transaction * * * * Author: Eugene Grigorjev * * * * Comments: do nothing if DB does not support transactions * * * ******************************************************************************/ int zbx_db_rollback() { int rc = ZBX_DB_OK, last_txn_error; if (0 == txn_level) { zabbix_log(LOG_LEVEL_CRIT, "ERROR: rollback without transaction." " Please report it to Job Arranger Team."); assert(0); } last_txn_error = txn_error; /* allow rollback of failed transaction */ txn_error = 0; #if defined(HAVE_IBM_DB2) if (SUCCEED != zbx_ibm_db2_success(SQLEndTran(SQL_HANDLE_DBC, ibm_db2.hdbc, SQL_ROLLBACK))) rc = ZBX_DB_DOWN; if (SUCCEED != zbx_ibm_db2_success(SQLSetConnectAttr(ibm_db2.hdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_NTS))) rc = ZBX_DB_DOWN; if (ZBX_DB_OK != rc) { zbx_ibm_db2_log_errors(SQL_HANDLE_DBC, ibm_db2.hdbc); rc = (SQL_CD_TRUE == IBM_DB2server_status() ? ZBX_DB_FAIL : ZBX_DB_DOWN); } #elif defined(HAVE_MYSQL) || defined(HAVE_POSTGRESQL) rc = zbx_db_execute("%s", "rollback;"); #elif defined(HAVE_ORACLE) OCITransRollback(oracle.svchp, oracle.errhp, OCI_DEFAULT); #elif defined(HAVE_SQLITE3) rc = zbx_db_execute("%s", "rollback;"); php_sem_release(&sqlite_access); #endif if (ZBX_DB_DOWN != rc) /* ZBX_DB_FAIL or ZBX_DB_OK or number of changes */ txn_level--; else txn_error = last_txn_error; /* in case of DB down we will repeat this operation */ return rc; }
/****************************************************************************** * * * Function: zbx_db_commit * * * * Purpose: commit transaction * * * * Author: Eugene Grigorjev * * * * Comments: do nothing if DB does not support transactions * * * ******************************************************************************/ int zbx_db_commit() { int rc = ZBX_DB_OK; if (0 == txn_level) { zabbix_log(LOG_LEVEL_CRIT, "ERROR: commit without transaction." " Please report it to Job Arranger Team."); assert(0); } if (1 == txn_error) { zabbix_log(LOG_LEVEL_DEBUG, "commit called on failed transaction, doing a rollback instead"); return zbx_db_rollback(); } #if defined(HAVE_IBM_DB2) if (SUCCEED != zbx_ibm_db2_success(SQLEndTran(SQL_HANDLE_DBC, ibm_db2.hdbc, SQL_COMMIT))) rc = ZBX_DB_DOWN; if (SUCCEED != zbx_ibm_db2_success(SQLSetConnectAttr(ibm_db2.hdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_NTS))) rc = ZBX_DB_DOWN; if (ZBX_DB_OK != rc) { zbx_ibm_db2_log_errors(SQL_HANDLE_DBC, ibm_db2.hdbc); rc = (SQL_CD_TRUE == IBM_DB2server_status() ? ZBX_DB_FAIL : ZBX_DB_DOWN); } #elif defined(HAVE_MYSQL) || defined(HAVE_POSTGRESQL) rc = zbx_db_execute("%s", "commit;"); #elif defined(HAVE_ORACLE) OCITransCommit(oracle.svchp, oracle.errhp, OCI_DEFAULT); #elif defined(HAVE_SQLITE3) rc = zbx_db_execute("%s", "commit;"); php_sem_release(&sqlite_access); #endif if (ZBX_DB_DOWN != rc) /* ZBX_DB_FAIL or ZBX_DB_OK or number of changes */ txn_level--; return rc; }
void DBConnection::executeUpdate(const char* sql) { _idle = false; SQLHSTMT hstmt = NULL; result = SQLAllocHandle(SQL_HANDLE_STMT,this->dbc,&hstmt); std::string sqlstr = sql; std::string& sqlstr2 = sqlstr; std::wstring wsql = s2ws(sqlstr2); result = SQLExecDirect(hstmt,(SQLWCHAR*)wsql.c_str(),SQL_NTS); if (result != SQL_SUCCESS) { showDBSError(hstmt); } result = SQLEndTran(SQL_HANDLE_DBC,dbc,SQL_COMMIT); if (result != SQL_SUCCESS) { showDBCError(dbc); } SQLFreeHandle(SQL_HANDLE_STMT,hstmt); }
//////////////////////////////////////////////////////////////////////////////// //write to disk any cached modifications of storage object incarnations managed //by this catalog. //////////////////////////////////////////////////////////////////////////////// void CatalogBaseImpl::flush() { DEBUG_OUT("CatalogBaseImpl::flush() is called"); if( !CanTransact() ) NORMAL_OUT( "CatalogBaseImpl::flush() - Database do not support transaction, flush is errorprone!" ); if( access_mode==READ_ONLY ) { NORMAL_ERR( "CatalogBaseImpl::flush() - Session is read-only!" ); return; } std::string strFlush = ""; for( homeBaseIter_=lHomeBases_.begin(); homeBaseIter_!=lHomeBases_.end(); homeBaseIter_++ ) strFlush += (*homeBaseIter_)->getFlush(); if(ExecuteSQL(strFlush.c_str())) { SQLRETURN ret; ret = SQLEndTran(SQL_HANDLE_DBC, hDbc_, SQL_COMMIT); if(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) { // the flush is successfull, we set the modified-value of each // storage object back to FALSE for( homeBaseIter_=lHomeBases_.begin(); homeBaseIter_!=lHomeBases_.end(); homeBaseIter_++ ) (*homeBaseIter_)->setBatchUnModified(); } else { NORMAL_ERR( "CatalogBaseImpl::flush() - Database transaction is not successful!" ); } } else { NORMAL_ERR( "CatalogBaseImpl::flush() - flush is not executed!" ); } }
void ring_vm_odbc_rollback ( void *pPointer ) { ring_odbc *pODBC ; SQLRETURN ret ; if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_MISS1PARA); return ; } if ( RING_API_ISPOINTER(1) ) { pODBC = (ring_odbc *) RING_API_GETCPOINTER(1,RING_VM_POINTER_ODBC) ; if ( pODBC == NULL ) { return ; } ret = SQLEndTran(SQL_HANDLE_DBC,pODBC->dbc,SQL_ROLLBACK); if ( SQL_SUCCEEDED(ret) ) { RING_API_RETNUMBER(1); } else { RING_API_RETNUMBER(0); } } else { RING_API_ERROR(RING_API_BADPARATYPE); } }