+ m_log.debug("SQLPrepare succeeded. SQL: %s", q.c_str());
+
+ SQLLEN b_ind = SQL_NTS;
+ sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast<char*>(context), &b_ind);
+ if (!SQL_SUCCEEDED(sr)) {
+ m_log.error("SQLBindParam failed (context = %s)", context);
+ log_error(stmt, SQL_HANDLE_STMT);
+ throw IOException("ODBC StorageService failed to insert record.");
+ }
+ m_log.debug("SQLBindParam succeeded (context = %s)", context);
+
+ sr = SQLBindParam(stmt, 2, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast<char*>(key), &b_ind);
+ if (!SQL_SUCCEEDED(sr)) {
+ m_log.error("SQLBindParam failed (key = %s)", key);
+ log_error(stmt, SQL_HANDLE_STMT);
+ throw IOException("ODBC StorageService failed to insert record.");
+ }
+ m_log.debug("SQLBindParam succeeded (key = %s)", key);
+
+ if (strcmp(table, TEXT_TABLE)==0)
+ sr = SQLBindParam(stmt, 3, SQL_C_CHAR, SQL_LONGVARCHAR, strlen(value), 0, const_cast<char*>(value), &b_ind);
+ else
+ sr = SQLBindParam(stmt, 3, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast<char*>(value), &b_ind);
+ if (!SQL_SUCCEEDED(sr)) {
+ m_log.error("SQLBindParam failed (value = %s)", value);
+ log_error(stmt, SQL_HANDLE_STMT);
+ throw IOException("ODBC StorageService failed to insert record.");
+ }
+ m_log.debug("SQLBindParam succeeded (value = %s)", value);
+
+ int attempts = 3;
+ pair<bool,bool> logres;
+ do {
+ logres = make_pair(false,false);
+ attempts--;
+ sr = SQLExecute(stmt);
+ if (SQL_SUCCEEDED(sr)) {
+ m_log.debug("SQLExecute of insert succeeded");
+ return true;
+ }
+ m_log.error("insert record failed (t=%s, c=%s, k=%s)", table, context, key);
+ logres = log_error(stmt, SQL_HANDLE_STMT, "23000");
+ if (logres.second) {
+ // Supposedly integrity violation.
+ // Try and delete any expired record still hanging around until the final attempt.
+ if (attempts > 0) {
+ reap(table, context);
+ logres.first = true; // force it to treat as a retryable error
+ continue;
+ }
+ return false;
+ }
+ } while (attempts && logres.first);
+
+ throw IOException("ODBC StorageService failed to insert record.");