namespace {
static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
+ static const XMLCh isolationLevel[] = UNICODE_LITERAL_14(i,s,o,l,a,t,i,o,n,L,e,v,e,l);
static const XMLCh ConnectionString[] = UNICODE_LITERAL_16(C,o,n,n,e,c,t,i,o,n,S,t,r,i,n,g);
// RAII for ODBC handles
struct ODBCConn {
- ODBCConn(SQLHDBC conn) : handle(conn) {}
+ ODBCConn(SQLHDBC conn) : handle(conn), autoCommit(true) {}
~ODBCConn() {
- SQLRETURN sr = SQLEndTran(SQL_HANDLE_DBC, handle, SQL_COMMIT);
+ SQLRETURN sr = SQL_SUCCESS;
+ if (!autoCommit)
+ sr = SQLSetConnectAttr(handle, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, NULL);
SQLDisconnect(handle);
SQLFreeHandle(SQL_HANDLE_DBC,handle);
if (!SQL_SUCCEEDED(sr))
- throw IOException("Failed to commit connection.");
+ throw IOException("Failed to commit connection and return to auto-commit mode.");
}
operator SQLHDBC() {return handle;}
SQLHDBC handle;
- };
-
- struct ODBCStatement {
- ODBCStatement(SQLHSTMT statement) : handle(statement) {}
- ~ODBCStatement() {SQLFreeHandle(SQL_HANDLE_STMT,handle);}
- operator SQLHSTMT() {return handle;}
- SQLHSTMT handle;
+ bool autoCommit;
};
class ODBCStorageService : public StorageService
SQLHENV m_henv;
string m_connstring;
+ long m_isolation;
};
StorageService* ODBCStorageServiceFactory(const DOMElement* const & e)
};
ODBCStorageService::ODBCStorageService(const DOMElement* e) : m_log(Category::getInstance("XMLTooling.StorageService")),
- m_cleanupInterval(900), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_henv(SQL_NULL_HANDLE)
+ m_cleanupInterval(900), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_henv(SQL_NULL_HANDLE), m_isolation(SQL_TXN_SERIALIZABLE)
{
#ifdef _DEBUG
xmltooling::NDC ndc("ODBCStorageService");
if (!m_cleanupInterval)
m_cleanupInterval = 900;
+ auto_ptr_char iso(e ? e->getAttributeNS(NULL,isolationLevel) : NULL);
+ if (iso.get() && *iso.get()) {
+ if (!strcmp(iso.get(),"SERIALIZABLE"))
+ m_isolation = SQL_TXN_SERIALIZABLE;
+ else if (!strcmp(iso.get(),"REPEATABLE_READ"))
+ m_isolation = SQL_TXN_REPEATABLE_READ;
+ else if (!strcmp(iso.get(),"READ_COMMITTED"))
+ m_isolation = SQL_TXN_READ_COMMITTED;
+ else if (!strcmp(iso.get(),"READ_UNCOMMITTED"))
+ m_isolation = SQL_TXN_READ_UNCOMMITTED;
+ else
+ throw XMLToolingException("Unknown transaction isolationLevel property.");
+ }
+
if (m_henv == SQL_NULL_HANDLE) {
// Enable connection pooling.
SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)SQL_CP_ONE_PER_HENV, 0);
throw IOException("ODBC StorageService failed to connect to database.");
}
- sr = SQLSetConnectAttr(handle, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, NULL);
- if (!SQL_SUCCEEDED(sr))
- throw IOException("ODBC StorageService failed to disable auto-commit mode.");
- sr = SQLSetConnectAttr(handle, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)SQL_TXN_SERIALIZABLE, NULL);
+ sr = SQLSetConnectAttr(handle, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)m_isolation, NULL);
if (!SQL_SUCCEEDED(sr))
- throw IOException("ODBC StorageService failed to enable transaction isolation.");
+ throw IOException("ODBC StorageService failed to set transaction isolation level.");
return handle;
}
pair<int,int> ODBCStorageService::getVersion(SQLHDBC conn)
{
// Grab the version number from the database.
- ODBCStatement stmt(getHSTMT(conn));
+ SQLHSTMT stmt = getHSTMT(conn);
SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)"SELECT major,minor FROM version", SQL_NTS);
if (!SQL_SUCCEEDED(sr)) {
// Get statement handle.
ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
+ SQLHSTMT stmt = getHSTMT(conn);
// Prepare and exectute insert statement.
//char *scontext = makeSafeSQL(context);
log_error(stmt, SQL_HANDLE_STMT);
throw IOException("ODBC StorageService failed to insert record.");
}
- m_log.debug("SQLPrepare() succeded. SQL: %s", q.c_str());
+ m_log.debug("SQLPrepare succeded. SQL: %s", q.c_str());
SQLINTEGER b_ind = SQL_NTS;
- sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_VARCHAR, 0, 0, const_cast<char*>(context), &b_ind);
+ 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);
}
m_log.debug("SQLBindParam succeded (context = %s)", context);
- sr = SQLBindParam(stmt, 2, SQL_C_CHAR, SQL_VARCHAR, 0, 0, const_cast<char*>(key), &b_ind);
+ 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);
}
m_log.debug("SQLBindParam succeded (key = %s)", key);
- sr = SQLBindParam(stmt, 3, SQL_C_CHAR, (strcmp(table, TEXT_TABLE)==0 ? SQL_LONGVARCHAR : SQL_VARCHAR), 0, 0, const_cast<char*>(value), &b_ind);
+ 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);
return false; // supposedly integrity violation?
throw IOException("ODBC StorageService failed to insert record.");
}
+
+ m_log.debug("SQLExecute of insert succeeded");
return true;
}
// Get statement handle.
ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
+ SQLHSTMT stmt = getHSTMT(conn);
// Prepare and exectute select statement.
char timebuf[32];
if (!value && !expiration)
throw IOException("ODBC StorageService given invalid update instructions.");
- // Get statement handle.
+ // Get statement handle. Disable auto-commit mode to wrap select + update.
ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
+ SQLRETURN sr = SQLSetConnectAttr(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, NULL);
+ if (!SQL_SUCCEEDED(sr))
+ throw IOException("ODBC StorageService failed to disable auto-commit mode.");
+ conn.autoCommit = false;
+ SQLHSTMT stmt = getHSTMT(conn);
// First, fetch the current version for later, which also ensures the record still exists.
char timebuf[32];
char *scontext = makeSafeSQL(context);
char *skey = makeSafeSQL(key);
string q("SELECT version FROM ");
- q = q + table + " WHERE context='" + scontext + "' AND id='" + key + "' AND expires > " + timebuf;
+ q = q + table + " WHERE context='" + scontext + "' AND id='" + skey + "' AND expires > " + timebuf;
m_log.debug("SQL: %s", q.c_str());
- SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+ sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
if (!SQL_SUCCEEDED(sr)) {
freeSafeSQL(scontext, context);
freeSafeSQL(skey, key);
return -1;
}
+ SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+ stmt = getHSTMT(conn);
+
// Prepare and exectute update statement.
q = string("UPDATE ") + table + " SET ";
q = q + "expires = " + timebuf;
}
- q = q + " WHERE context='" + scontext + "' AND id='" + key + "'";
+ q = q + " WHERE context='" + scontext + "' AND id='" + skey + "'";
freeSafeSQL(scontext, context);
freeSafeSQL(skey, key);
log_error(stmt, SQL_HANDLE_STMT);
throw IOException("ODBC StorageService failed to update record.");
}
- m_log.debug("SQLPrepare() succeded. SQL: %s", q.c_str());
+ m_log.debug("SQLPrepare succeded. SQL: %s", q.c_str());
SQLINTEGER b_ind = SQL_NTS;
if (value) {
- sr = SQLBindParam(stmt, 1, SQL_C_CHAR, (strcmp(table, TEXT_TABLE)==0 ? SQL_LONGVARCHAR : SQL_VARCHAR), 0, 0, const_cast<char*>(value), &b_ind);
+ if (strcmp(table, TEXT_TABLE)==0)
+ sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_LONGVARCHAR, strlen(value), 0, const_cast<char*>(value), &b_ind);
+ else
+ sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_VARCHAR, 255, 0, const_cast<char*>(value), &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 update record.");
}
+ m_log.debug("SQLExecute of update succeeded");
return ver + 1;
}
// Get statement handle.
ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
+ SQLHSTMT stmt = getHSTMT(conn);
// Prepare and execute delete statement.
char *scontext = makeSafeSQL(context);
// Get statement handle.
ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
+ SQLHSTMT stmt = getHSTMT(conn);
char timebuf[32];
timestampFromTime(expiration, timebuf);
// Get statement handle.
ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
+ SQLHSTMT stmt = getHSTMT(conn);
// Prepare and execute delete statement.
char nowbuf[32];
// Get statement handle.
ODBCConn conn(getHDBC());
- ODBCStatement stmt(getHSTMT(conn));
+ SQLHSTMT stmt = getHSTMT(conn);
// Prepare and execute delete statement.
char *scontext = makeSafeSQL(context);