SSPCPP-656 - NameID insert logic appears wrong for ODBC Session store
[shibboleth/cpp-sp.git] / odbc-store / odbc-store.cpp
index 8f61b06..de0cd31 100644 (file)
@@ -1,26 +1,29 @@
-/*
- *  Copyright 2001-2007 Internet2
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
-/*
- * odbc-store.cpp - Storage service using ODBC
+/**
+ * odbc-store.cpp
  *
- * $Id$
+ * Storage Service using ODBC.
  */
 
-// eventually we might be able to support autoconf via cygwin...
 #if defined (_MSC_VER) || defined(__BORLANDC__)
 # include "config_win32.h"
 #else
 #ifdef WIN32
 # define _CRT_NONSTDC_NO_DEPRECATE 1
 # define _CRT_SECURE_NO_DEPRECATE 1
-# define NOMINMAX
-# define SHIBODBC_EXPORTS __declspec(dllexport)
+#endif
+
+#ifdef WIN32
+# define ODBCSTORE_EXPORTS __declspec(dllexport)
 #else
-# define SHIBODBC_EXPORTS
+# define ODBCSTORE_EXPORTS
 #endif
 
-#include <shib-target/shib-target.h>
-#include <shibsp/exceptions.h>
-#include <log4cpp/Category.hh>
+#include <xmltooling/logging.h>
+#include <xmltooling/unicode.h>
+#include <xmltooling/XMLToolingConfig.h>
 #include <xmltooling/util/NDC.h>
+#include <xmltooling/util/StorageService.h>
 #include <xmltooling/util/Threads.h>
-
-#include <ctime>
-#include <algorithm>
-#include <sstream>
+#include <xmltooling/util/XMLHelper.h>
+#include <xercesc/util/XMLUniDefs.hpp>
 
 #include <sql.h>
 #include <sqlext.h>
 
-#ifdef HAVE_LIBDMALLOCXX
-#include <dmalloc.h>
-#endif
+#include <boost/lexical_cast.hpp>
+#include <boost/algorithm/string.hpp>
 
-using namespace shibsp;
-using namespace shibtarget;
-using namespace opensaml::saml2md;
-using namespace saml;
+using namespace xmltooling::logging;
 using namespace xmltooling;
-using namespace log4cpp;
+using namespace xercesc;
+using namespace boost;
 using namespace std;
 
-#define PLUGIN_VER_MAJOR 3
-#define PLUGIN_VER_MINOR 0
-
-#define COLSIZE_KEY 64
-#define COLSIZE_CONTEXT 256
-#define COLSIZE_STRING_VALUE 256
-
-
-/* tables definitions - not used here */
-
-#define STRING_TABLE "STRING_STORE"
-
-#define STRING_TABLE \
-  "CREATE TABLE STRING_TABLE ( "\
-  "context VARCHAR( COLSIZE_CONTEXT ), " \
-  "key VARCHAR( COLSIZE_KEY ), " \
-  "value VARCHAR( COLSIZE_STRING_VALUE ), " \
-  "expires TIMESTAMP, "
-  "PRIMARY KEY (context, key), "
-  "INDEX (context))"
-
-
-#define TEXT_TABLE "TEXT_STORE"
-
-#define TEXT_TABLE \
-  "CREATE TABLE TEXT_TABLE ( "\
-  "context VARCHAR( COLSIZE_CONTEXT ), " \
-  "key VARCHAR( COLSIZE_KEY ), " \
-  "value LONG TEXT, " \
-  "expires TIMESTAMP, "
-  "PRIMARY KEY (context, key), "
-  "INDEX (context))"
-
+#define PLUGIN_VER_MAJOR 1
+#define PLUGIN_VER_MINOR 1
+
+#define LONGDATA_BUFLEN 16384
+
+#define COLSIZE_CONTEXT 255
+#define COLSIZE_ID 255
+#define COLSIZE_STRING_VALUE 255
+
+#define STRING_TABLE "strings"
+#define TEXT_TABLE "texts"
+
+/* table definitions
+CREATE TABLE version (
+    major int NOT nullptr,
+    minor int NOT nullptr
+    )
+
+CREATE TABLE strings (
+    context varchar(255) not null,
+    id varchar(255) not null,
+    expires datetime not null,
+    version int not null,
+    value varchar(255) not null,
+    PRIMARY KEY (context, id)
+    )
+
+CREATE TABLE texts (
+    context varchar(255) not null,
+    id varchar(255) not null,
+    expires datetime not null,
+    version int not null,
+    value text not null,
+    PRIMARY KEY (context, id)
+    )
+*/
+
+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);
+    static const XMLCh RetryOnError[] =     UNICODE_LITERAL_12(R,e,t,r,y,O,n,E,r,r,o,r);
+    static const XMLCh contextSize[] =      UNICODE_LITERAL_11(c,o,n,t,e,x,t,S,i,z,e);
+    static const XMLCh keySize[] =          UNICODE_LITERAL_7(k,e,y,S,i,z,e);
+    static const XMLCh stringSize[] =       UNICODE_LITERAL_10(s,t,r,i,n,g,S,i,z,e);
+
+    // RAII for ODBC handles
+    struct ODBCConn {
+        ODBCConn(SQLHDBC conn) : handle(conn), autoCommit(true) {}
+        ~ODBCConn() {
+            if (handle != SQL_NULL_HDBC) {
+                SQLRETURN sr = SQL_SUCCESS;
+                if (!autoCommit)
+                    sr = SQLSetConnectAttr(handle, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, 0);
+                SQLDisconnect(handle);
+                SQLFreeHandle(SQL_HANDLE_DBC, handle);
+                if (!SQL_SUCCEEDED(sr))
+                    throw IOException("Failed to commit connection and return to auto-commit mode.");
+            }
+        }
+        operator SQLHDBC() {return handle;}
+        SQLHDBC handle;
+        bool autoCommit;
+    };
 
+    class ODBCStorageService : public StorageService
+    {
+    public:
+        ODBCStorageService(const DOMElement* e);
+        virtual ~ODBCStorageService();
 
+        const Capabilities& getCapabilities() const {
+            return m_caps;
+        }
 
-static const XMLCh ConnectionString[] =
-{ chLatin_C, chLatin_o, chLatin_n, chLatin_n, chLatin_e, chLatin_c, chLatin_t, chLatin_i, chLatin_o, chLatin_n,
-  chLatin_S, chLatin_t, chLatin_r, chLatin_i, chLatin_n, chLatin_g, chNull
-};
-static const XMLCh cleanupInterval[] =
-{ chLatin_c, chLatin_l, chLatin_e, chLatin_a, chLatin_n, chLatin_u, chLatin_p,
-  chLatin_I, chLatin_n, chLatin_t, chLatin_e, chLatin_r, chLatin_v, chLatin_a, chLatin_l, chNull
-};
-static const XMLCh cacheTimeout[] =
-{ chLatin_c, chLatin_a, chLatin_c, chLatin_h, chLatin_e, chLatin_T, chLatin_i, chLatin_m, chLatin_e, chLatin_o, chLatin_u, chLatin_t, chNull };
-static const XMLCh odbcTimeout[] =
-{ chLatin_o, chLatin_d, chLatin_b, chLatin_c, chLatin_T, chLatin_i, chLatin_m, chLatin_e, chLatin_o, chLatin_u, chLatin_t, chNull };
-static const XMLCh storeAttributes[] =
-{ chLatin_s, chLatin_t, chLatin_o, chLatin_r, chLatin_e, chLatin_A, chLatin_t, chLatin_t, chLatin_r, chLatin_i, chLatin_b, chLatin_u, chLatin_t, chLatin_e, chLatin_s, chNull };
+        bool createString(const char* context, const char* key, const char* value, time_t expiration) {
+            return createRow(STRING_TABLE, context, key, value, expiration);
+        }
+        int readString(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0) {
+            return readRow(STRING_TABLE, context, key, pvalue, pexpiration, version);
+        }
+        int updateString(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0) {
+            return updateRow(STRING_TABLE, context, key, value, expiration, version);
+        }
+        bool deleteString(const char* context, const char* key) {
+            return deleteRow(STRING_TABLE, context, key);
+        }
 
-static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
+        bool createText(const char* context, const char* key, const char* value, time_t expiration) {
+            return createRow(TEXT_TABLE, context, key, value, expiration);
+        }
+        int readText(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0) {
+            return readRow(TEXT_TABLE, context, key, pvalue, pexpiration, version);
+        }
+        int updateText(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0) {
+            return updateRow(TEXT_TABLE, context, key, value, expiration, version);
+        }
+        bool deleteText(const char* context, const char* key) {
+            return deleteRow(TEXT_TABLE, context, key);
+        }
 
+        void reap(const char* context) {
+            reap(STRING_TABLE, context);
+            reap(TEXT_TABLE, context);
+        }
 
-// ODBC tools
+        void updateContext(const char* context, time_t expiration) {
+            updateContext(STRING_TABLE, context, expiration);
+            updateContext(TEXT_TABLE, context, expiration);
+        }
 
-struct ODBCConn {
-    ODBCConn(SQLHDBC conn) : handle(conn) {}
-    ~ODBCConn() {SQLFreeHandle(SQL_HANDLE_DBC,handle);}
-    operator SQLHDBC() {return handle;}
-    SQLHDBC handle;
-};
+        void deleteContext(const char* context) {
+            deleteContext(STRING_TABLE, context);
+            deleteContext(TEXT_TABLE, context);
+        }
+         
+
+    private:
+        bool createRow(const char *table, const char* context, const char* key, const char* value, time_t expiration);
+        int readRow(const char *table, const char* context, const char* key, string* pvalue, time_t* pexpiration, int version);
+        int updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration, int version);
+        bool deleteRow(const char *table, const char* context, const char* key);
+
+        void reap(const char* table, const char* context);
+        void updateContext(const char* table, const char* context, time_t expiration);
+        void deleteContext(const char* table, const char* context);
+
+        SQLHDBC getHDBC();
+        SQLHSTMT getHSTMT(SQLHDBC);
+        pair<SQLINTEGER,SQLINTEGER> getVersion(SQLHDBC);
+        pair<bool,bool> log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor=nullptr);
+
+        static void* cleanup_fn(void*); 
+        void cleanup();
+
+        Category& m_log;
+        Capabilities m_caps;
+        int m_cleanupInterval;
+        scoped_ptr<CondWait> shutdown_wait;
+        Thread* cleanup_thread;
+        bool shutdown;
+
+        SQLHENV m_henv;
+        string m_connstring;
+        long m_isolation;
+        bool m_wideVersion;
+        vector<SQLINTEGER> m_retries;
+    };
 
-class ODBCBase : public virtual saml::IPlugIn
-{
-public:
-    ODBCBase(const DOMElement* e);
-    virtual ~ODBCBase();
+    StorageService* ODBCStorageServiceFactory(const DOMElement* const & e)
+    {
+        return new ODBCStorageService(e);
+    }
 
-    SQLHDBC getHDBC();
+    // convert SQL timestamp to time_t 
+    time_t timeFromTimestamp(SQL_TIMESTAMP_STRUCT expires)
+    {
+        time_t ret;
+        struct tm t;
+        t.tm_sec=expires.second;
+        t.tm_min=expires.minute;
+        t.tm_hour=expires.hour;
+        t.tm_mday=expires.day;
+        t.tm_mon=expires.month-1;
+        t.tm_year=expires.year-1900;
+        t.tm_isdst=0;
+#if defined(HAVE_TIMEGM)
+        ret = timegm(&t);
+#else
+        ret = mktime(&t) - timezone;
+#endif
+        return (ret);
+    }
 
-    Category* log;
+    // conver time_t to SQL string
+    void timestampFromTime(time_t t, char* ret)
+    {
+#ifdef HAVE_GMTIME_R
+        struct tm res;
+        struct tm* ptime=gmtime_r(&t,&res);
+#else
+        struct tm* ptime=gmtime(&t);
+#endif
+        strftime(ret,32,"{ts '%Y-%m-%d %H:%M:%S'}",ptime);
+    }
 
-protected:
-    const DOMElement* m_root; // can only use this during initialization
-    string m_connstring;
+    class SQLString {
+        const char* m_src;
+        string m_copy;
+    public:
+        SQLString(const char* src) : m_src(src) {
+            if (strchr(src, '\'')) {
+                m_copy = src;
+                replace_all(m_copy, "'", "''");
+            }
+        }
 
-    static SQLHENV m_henv;          // single handle for both plugins
-    bool m_bInitializedODBC;        // tracks which class handled the process
-    static const char* p_connstring;
+        operator const char*() const {
+            return tostr();
+        }
 
-    pair<int,int> getVersion(SQLHDBC);
-    void log_error(SQLHANDLE handle, SQLSMALLINT htype);
+        const char* tostr() const {
+            return m_copy.empty() ? m_src : m_copy.c_str();
+        }
+    };
 };
 
-SQLHENV ODBCBase::m_henv = SQL_NULL_HANDLE;
-const char* ODBCBase::p_connstring = NULL;
-
-ODBCBase::ODBCBase(const DOMElement* e) : m_root(e), m_bInitializedODBC(false)
+ODBCStorageService::ODBCStorageService(const DOMElement* e) : m_log(Category::getInstance("XMLTooling.StorageService")),
+    m_caps(XMLHelper::getAttrInt(e, 255, contextSize), XMLHelper::getAttrInt(e, 255, keySize), XMLHelper::getAttrInt(e, 255, stringSize)),
+    m_cleanupInterval(XMLHelper::getAttrInt(e, 900, cleanupInterval)),
+    cleanup_thread(nullptr), shutdown(false), m_henv(SQL_NULL_HENV), m_isolation(SQL_TXN_SERIALIZABLE), m_wideVersion(false)
 {
 #ifdef _DEBUG
-    xmltooling::NDC ndc("ODBCBase");
+    xmltooling::NDC ndc("ODBCStorageService");
 #endif
-    log = &(Category::getInstance("shibtarget.ODBC"));
-
-    if (m_henv == SQL_NULL_HANDLE) {
+    string iso(XMLHelper::getAttrString(e, "SERIALIZABLE", isolationLevel));
+    if (iso == "SERIALIZABLE")
+        m_isolation = SQL_TXN_SERIALIZABLE;
+    else if (iso == "REPEATABLE_READ")
+        m_isolation = SQL_TXN_REPEATABLE_READ;
+    else if (iso == "READ_COMMITTED")
+        m_isolation = SQL_TXN_READ_COMMITTED;
+    else if (iso == "READ_UNCOMMITTED")
+        m_isolation = SQL_TXN_READ_UNCOMMITTED;
+    else
+        throw XMLToolingException("Unknown transaction isolationLevel property.");
+
+    if (m_henv == SQL_NULL_HENV) {
         // Enable connection pooling.
         SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)SQL_CP_ONE_PER_HENV, 0);
 
         // Allocate the environment.
         if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_henv)))
-            throw ConfigurationException("ODBC failed to initialize.");
+            throw XMLToolingException("ODBC failed to initialize.");
 
         // Specify ODBC 3.x
         SQLSetEnvAttr(m_henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
 
-        log->info("ODBC initialized");
-        m_bInitializedODBC = true;
+        m_log.info("ODBC initialized");
     }
 
     // Grab connection string from the configuration.
-    e=XMLHelper::getFirstChildElement(e,ConnectionString);
-    if (!e || !e->hasChildNodes()) {
-        if (!p_connstring) {
-            this->~ODBCBase();
-            throw ConfigurationException("ODBC cache requires ConnectionString element in configuration.");
-        }
-        m_connstring=p_connstring;
-    }
-    else {
-        xmltooling::auto_ptr_char arg(e->getFirstChild()->getNodeValue());
-        m_connstring=arg.get();
-        p_connstring=m_connstring.c_str();
+    e = e ? XMLHelper::getFirstChildElement(e, ConnectionString) : nullptr;
+    auto_ptr_char arg(e ? e->getTextContent() : nullptr);
+    if (!arg.get() || !*arg.get()) {
+        SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
+        throw XMLToolingException("ODBC StorageService requires ConnectionString element in configuration.");
     }
+    m_connstring = arg.get();
 
     // Connect and check version.
-    SQLHDBC conn=getHDBC();
-    pair<int,int> v=getVersion(conn);
-    SQLFreeHandle(SQL_HANDLE_DBC,conn);
+    ODBCConn conn(getHDBC());
+    pair<SQLINTEGER,SQLINTEGER> v = getVersion(conn);
 
     // Make sure we've got the right version.
     if (v.first != PLUGIN_VER_MAJOR) {
-        this->~ODBCBase();
-        log->crit("unknown database version: %d.%d", v.first, v.second);
-        throw SAMLException("Unknown cache database version.");
+        SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
+        m_log.crit("unknown database version: %d.%d", v.first, v.second);
+        throw XMLToolingException("Unknown database version for ODBC StorageService.");
+    }
+    
+    if (v.first > 1 || v.second > 0) {
+        m_log.info("using 32-bit int type for version fields in tables");
+        m_wideVersion = true;
+    }
+
+    // Load any retry errors to check.
+    e = XMLHelper::getNextSiblingElement(e, RetryOnError);
+    while (e) {
+        if (e->hasChildNodes()) {
+            try {
+                int code = XMLString::parseInt(e->getTextContent());
+                m_retries.push_back(code);
+                m_log.info("will retry operations when native ODBC error (%d) is returned", code);
+            }
+            catch (XMLException&) {
+                m_log.error("skipping non-numeric ODBC retry code");
+            }
+        }
+        e = XMLHelper::getNextSiblingElement(e, RetryOnError);
     }
+
+    // Initialize the cleanup thread
+    shutdown_wait.reset(CondWait::create());
+    cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
 }
 
-ODBCBase::~ODBCBase()
+ODBCStorageService::~ODBCStorageService()
 {
-    //delete m_mysql;
-    if (m_bInitializedODBC)
-        SQLFreeHandle(SQL_HANDLE_ENV,m_henv);
-    m_bInitializedODBC=false;
-    m_henv = SQL_NULL_HANDLE;
-    p_connstring=NULL;
+    shutdown = true;
+    shutdown_wait->signal();
+    cleanup_thread->join(nullptr);
+    if (m_henv != SQL_NULL_HANDLE)
+        SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
 }
 
-void ODBCBase::log_error(SQLHANDLE handle, SQLSMALLINT htype)
+pair<bool,bool> ODBCStorageService::log_error(SQLHANDLE handle, SQLSMALLINT htype, const char* checkfor)
 {
     SQLSMALLINT         i = 0;
     SQLINTEGER  native;
@@ -218,426 +367,415 @@ void ODBCBase::log_error(SQLHANDLE handle, SQLSMALLINT htype)
     SQLSMALLINT         len;
     SQLRETURN   ret;
 
+    pair<bool,bool> res = make_pair(false,false);
     do {
         ret = SQLGetDiagRec(htype, handle, ++i, state, &native, text, sizeof(text), &len);
-        if (SQL_SUCCEEDED(ret))
-            log->error("ODBC Error: %s:%ld:%ld:%s", state, i, native, text);
+        if (SQL_SUCCEEDED(ret)) {
+            m_log.error("ODBC Error: %s:%ld:%ld:%s", state, i, native, text);
+            for (vector<SQLINTEGER>::const_iterator n = m_retries.begin(); !res.first && n != m_retries.end(); ++n)
+                res.first = (*n == native);
+            if (checkfor && !strcmp(checkfor, (const char*)state))
+                res.second = true;
+        }
     } while(SQL_SUCCEEDED(ret));
+    return res;
 }
 
-SQLHDBC ODBCBase::getHDBC()
+SQLHDBC ODBCStorageService::getHDBC()
 {
 #ifdef _DEBUG
-    xmltooling::NDC ndc("getMYSQL");
+    xmltooling::NDC ndc("getHDBC");
 #endif
 
     // Get a handle.
-    SQLHDBC handle;
-    SQLRETURN sr=SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &handle);
-    if (!SQL_SUCCEEDED(sr)) {
-        log->error("failed to allocate connection handle");
+    SQLHDBC handle = SQL_NULL_HDBC;
+    SQLRETURN sr = SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &handle);
+    if (!SQL_SUCCEEDED(sr) || handle == SQL_NULL_HDBC) {
+        m_log.error("failed to allocate connection handle");
         log_error(m_henv, SQL_HANDLE_ENV);
-        throw SAMLException("ODBCBase::getHDBC failed to allocate connection handle");
+        throw IOException("ODBC StorageService failed to allocate a connection handle.");
     }
 
-    sr=SQLDriverConnect(handle,NULL,(SQLCHAR*)m_connstring.c_str(),m_connstring.length(),NULL,0,NULL,SQL_DRIVER_NOPROMPT);
+    sr = SQLDriverConnect(handle,nullptr,(SQLCHAR*)m_connstring.c_str(),m_connstring.length(),nullptr,0,nullptr,SQL_DRIVER_NOPROMPT);
     if (!SQL_SUCCEEDED(sr)) {
-        log->error("failed to connect to database");
+        m_log.error("failed to connect to database");
         log_error(handle, SQL_HANDLE_DBC);
-        throw SAMLException("ODBCBase::getHDBC failed to connect to database");
+        SQLFreeHandle(SQL_HANDLE_DBC, handle);
+        throw IOException("ODBC StorageService failed to connect to database.");
+    }
+
+    sr = SQLSetConnectAttr(handle, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)m_isolation, 0);
+    if (!SQL_SUCCEEDED(sr)) {
+        SQLDisconnect(handle);
+        SQLFreeHandle(SQL_HANDLE_DBC, handle);
+        throw IOException("ODBC StorageService failed to set transaction isolation level.");
     }
 
     return handle;
 }
 
-pair<int,int> ODBCBase::getVersion(SQLHDBC conn)
+SQLHSTMT ODBCStorageService::getHSTMT(SQLHDBC conn)
+{
+    SQLHSTMT hstmt = SQL_NULL_HSTMT;
+    SQLRETURN sr = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt);
+    if (!SQL_SUCCEEDED(sr) || hstmt == SQL_NULL_HSTMT) {
+        m_log.error("failed to allocate statement handle");
+        log_error(conn, SQL_HANDLE_DBC);
+        throw IOException("ODBC StorageService failed to allocate a statement handle.");
+    }
+    return hstmt;
+}
+
+pair<SQLINTEGER,SQLINTEGER> ODBCStorageService::getVersion(SQLHDBC conn)
 {
     // Grab the version number from the database.
-    SQLHSTMT hstmt;
-    SQLAllocHandle(SQL_HANDLE_STMT,conn,&hstmt);
+    SQLHSTMT stmt = getHSTMT(conn);
     
-    SQLRETURN sr=SQLExecDirect(hstmt, (SQLCHAR*)"SELECT major,minor FROM version", SQL_NTS);
+    SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)"SELECT major,minor FROM version", SQL_NTS);
     if (!SQL_SUCCEEDED(sr)) {
-        log->error("failed to read version from database");
-        log_error(hstmt, SQL_HANDLE_STMT);
-        throw SAMLException("ODBCBase::getVersion failed to read version from database");
+        m_log.error("failed to read version from database");
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService failed to read version from database.");
     }
 
     SQLINTEGER major;
     SQLINTEGER minor;
-    SQLBindCol(hstmt,1,SQL_C_SLONG,&major,0,NULL);
-    SQLBindCol(hstmt,2,SQL_C_SLONG,&minor,0,NULL);
+    SQLBindCol(stmt, 1, SQL_C_SLONG, &major, 0, nullptr);
+    SQLBindCol(stmt, 2, SQL_C_SLONG, &minor, 0, nullptr);
 
-    if ((sr=SQLFetch(hstmt)) != SQL_NO_DATA) {
-        SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
-        return pair<int,int>(major,minor);
-    }
+    if ((sr = SQLFetch(stmt)) != SQL_NO_DATA)
+        return make_pair(major,minor);
 
-    SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
-    log->error("no rows returned in version query");
-    throw SAMLException("ODBCBase::getVersion failed to read version from database");
+    m_log.error("no rows returned in version query");
+    throw IOException("ODBC StorageService failed to read version from database.");
 }
 
-
-// ------------------------------------------------------------
-
-// ODBC Storage Service class
-
-class ODBCStorageService : public ODBCBase, public StorageService
+bool ODBCStorageService::createRow(const char* table, const char* context, const char* key, const char* value, time_t expiration)
 {
-    string stringTable = STRING_TABLE;
-    string textTable = TEXT_TABLE;
-
-public:
-    ODBCStorageService(const DOMElement* e);
-    virtual ~ODBCStorageService();
-
-    void createString(const char* context, const char* key, const char* value, time_t expiration) {
-        return createRow(string_table, context, key, value, expiration);
-    }
-    bool readString(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL) {
-        return readRow(string_table, context, key, value, expiration, COLSIZE_STRING_VALUE);
-    }
-    bool updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=0) {
-        return updateRow(string_table, context, key, value, expiration);
-    }
-    bool deleteString(const char* context, const char* key) {
-        return deleteRow(string_table, context, key, value, expiration);
-    }
-
-    void createText(const char* context, const char* key, const char* value, time_t expiration) {
-        return createRow(text_table, context, key, value, expiration);
-    }
-    bool readText(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL) {
-        return readRow(text_table, context, key, value, expiration, 0);
-    }
-    bool updateText(const char* context, const char* key, const char* value=NULL, time_t expiration=0) {
-        return updateRow(text_table, context, key, value, expiration);
-    }
-    bool deleteText(const char* context, const char* key) {
-        return deleteRow(text_table, context, key, value, expiration);
-    }
-
-    void reap(const char* context) {
-        reap(string_table, context);
-        reap(text_table, context);
-    }
-    void deleteContext(const char* context) {
-        deleteCtx(string_table, context);
-        deleteCtx(text_table, context);
-    }
-     
-
-private:
-
-    void createRow(const char *table, const char* context, const char* key, const char* value, time_t expiration);
-    bool readRow(const char *table, const char* context, const char* key, string* pvalue, time_t* pexpiration, int maxsize);
-    bool updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration);
-    bool deleteRow(const char *table, const char* context, const char* key);
-
-    void reapRows(const char* table, const char* context);
-    void deleteCtx(const char* table, const char* context);
+#ifdef _DEBUG
+    xmltooling::NDC ndc("createRow");
+#endif
 
-    xmltooling::CondWait* shutdown_wait;
-    bool shutdown;
-    xmltooling::Thread* cleanup_thread;
+    char timebuf[32];
+    timestampFromTime(expiration, timebuf);
 
-    static void* cleanup_fcn(void*); 
-    void cleanup();
+    // Get statement handle.
+    ODBCConn conn(getHDBC());
+    SQLHSTMT stmt = getHSTMT(conn);
 
-    CondWait* shutdown_wait;
-    Thread* cleanup_thread;
-    bool shutdown;
-    int m_cleanupInterval;
-    Category& log;
+    string q  = string("INSERT INTO ") + table + " VALUES (?,?," + timebuf + ",1,?)";
 
-    StorageService* ODBCStorageServiceFactory(const DOMElement* const & e)
-    {
-        return new ODBCStorageService(e);
+    SQLRETURN sr = SQLPrepare(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    if (!SQL_SUCCEEDED(sr)) {
+        m_log.error("SQLPrepare failed (t=%s, c=%s, k=%s)", table, context, key);
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService failed to insert record.");
     }
+    m_log.debug("SQLPrepare succeeded. SQL: %s", q.c_str());
 
-    // convert SQL timestamp to time_t 
-    time_t timeFromTimestamp(SQL_TIMESTAMP_STRUCT expires)
-    {
-        time_t ret;
-        struct tm t;
-        t.tm_sec=expires.second;
-        t.tm_min=expires.minute;
-        t.tm_hour=expires.hour;
-        t.tm_mday=expires.day;
-        t.tm_mon=expires.month-1;
-        t.tm_year=expires.year-1900;
-        t.tm_isdst=0;
-#if defined(HAVE_TIMEGM)
-        ret = timegm(&t);
-#else
-        ret = mktime(&t) - timezone;
-#endif
-        return (ret);
+    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);
 
-    // conver time_t to SQL string
-    void timestampFromTime(time_t t, char &ret)
-    {
-#ifdef HAVE_GMTIME_R
-        struct tm res;
-        struct tm* ptime=gmtime_r(&created,&res);
-#else
-        struct tm* ptime=gmtime(&created);
-#endif
-        strftime(ret,32,"{ts '%Y-%m-%d %H:%M:%S'}",ptime);
+    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);
 
-};
-
-// class constructor
-
-ODBCStorageService::ODBCStorageService(const DOMElement* e):
-   ODBCBase(e),
-   shutdown(false),
-   m_cleanupInterval(0)
-
-{
-#ifdef _DEBUG
-    xmltooling::NDC ndc("ODBCStorageService");
-#endif
-    log = &(Category::getInstance("shibtarget.StorageService.ODBC"));
-
-    const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL;
-    if (tag && *tag) {
-        m_cleanupInterval = XMLString::parseInt(tag);
+    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.");
     }
-    if (!m_cleanupInterval) m_cleanupInterval=300;
-
-    contextLock = Mutex::create();
-    shutdown_wait = CondWait::create();
-
-    // Initialize the cleanup thread
-    cleanup_thread = Thread::create(&cleanup_fcn, (void*)this);
-}
-
-ODBCStorageService::~ODBCStorageService()
-{
-    shutdown = true;
-    shutdown_wait->signal();
-    cleanup_thread->join(NULL);
+    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);
 
-    delete shutdown_wait;
+    throw IOException("ODBC StorageService failed to insert record.");
 }
 
-// create 
-
-HRESULT ODBCStorageService::createRow(const char *table, const char* context, const char* key, const char* value, time_t expiration)
+int ODBCStorageService::readRow(const char *table, const char* context, const char* key, string* pvalue, time_t* pexpiration, int version)
 {
 #ifdef _DEBUG
-    xmltooling::NDC ndc("createRow");
+    xmltooling::NDC ndc("readRow");
 #endif
 
-    char timebuf[32];
-    timestampFromTime(expiration, timebuf);
-
     // Get statement handle.
-    SQLHSTMT hstmt;
     ODBCConn conn(getHDBC());
-    SQLAllocHandle(SQL_HANDLE_STMT,conn,&hstmt);
+    SQLHSTMT stmt = getHSTMT(conn);
 
-    // Prepare and exectute insert statement.
-    string q  = string("INSERT ") + table + " VALUES ('" + context + "','" + key + "','" + value + "'," + timebuf + "')";
-    log->debug("SQL: %s", q.str());
-
-    HRESULT hr=NOERROR;
-    SQLRETURN sr=SQLExecDirect(hstmt, (SQLCHAR*)q.str().c_str(), SQL_NTS);
+    // Prepare and exectute select statement.
+    char timebuf[32];
+    timestampFromTime(time(nullptr), timebuf);
+    SQLString scontext(context);
+    SQLString skey(key);
+    string q("SELECT version");
+    if (pexpiration)
+        q += ",expires";
+    if (pvalue) {
+        pvalue->erase();
+        q = q + ",CASE version WHEN " + lexical_cast<string>(version) + " THEN null ELSE value END";
+    }
+    q = q + " FROM " + table + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "' AND expires > " + timebuf;
+    if (m_log.isDebugEnabled())
+        m_log.debug("SQL: %s", q.c_str());
+
+    SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
     if (!SQL_SUCCEEDED(sr)) {
-        log->error("insert record failed (t=%s, c=%s, k=%s", table, context, key);
-        log_error(hstmt, SQL_HANDLE_STMT);
-        hr=E_FAIL;
+        m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService search failed.");
+    }
+
+    SQLSMALLINT ver;
+    SQLINTEGER widever;
+    SQL_TIMESTAMP_STRUCT expiration;
+
+    if (m_wideVersion)
+        SQLBindCol(stmt, 1, SQL_C_SLONG, &widever, 0, nullptr);
+    else
+        SQLBindCol(stmt, 1, SQL_C_SSHORT, &ver, 0, nullptr);
+    if (pexpiration)
+        SQLBindCol(stmt, 2, SQL_C_TYPE_TIMESTAMP, &expiration, 0, nullptr);
+
+    if ((sr = SQLFetch(stmt)) == SQL_NO_DATA) {
+        if (m_log.isDebugEnabled())
+            m_log.debug("search returned no data (t=%s, c=%s, k=%s)", table, context, key);
+        return 0;
+    }
+
+    if (pexpiration)
+        *pexpiration = timeFromTimestamp(expiration);
+
+    if (version == (m_wideVersion ? widever : ver)) {
+        if (m_log.isDebugEnabled())
+            m_log.debug("versioned search detected no change (t=%s, c=%s, k=%s)", table, context, key);
+        return version; // nothing's changed, so just echo back the version
+    }
+
+    if (pvalue) {
+        SQLLEN len;
+        SQLCHAR buf[LONGDATA_BUFLEN];
+        while ((sr = SQLGetData(stmt, (pexpiration ? 3 : 2), SQL_C_CHAR, buf, sizeof(buf), &len)) != SQL_NO_DATA) {
+            if (!SQL_SUCCEEDED(sr)) {
+                m_log.error("error while reading text field from result set");
+                log_error(stmt, SQL_HANDLE_STMT);
+                throw IOException("ODBC StorageService search failed to read data from result set.");
+            }
+            pvalue->append((char*)buf);
+        }
     }
-
-    SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
-    return hr;
+    
+    return (m_wideVersion ? widever : ver);
 }
 
-// read
-
-HRESULT ODBCStorageService::readRow(const char *table, const char* context, const char* key, string& pvalue, time_t& pexpiration, int maxsize)
+int ODBCStorageService::updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration, int version)
 {
 #ifdef _DEBUG
-    xmltooling::NDC ndc("readRow");
+    xmltooling::NDC ndc("updateRow");
 #endif
 
-    SQLCHAR *tvalue = NULL;
-    SQL_TIMESTAMP_STRUCT expires;
-    time_t exp;
+    if (!value && !expiration)
+        throw IOException("ODBC StorageService given invalid update instructions.");
 
-    // Get statement handle.
-    SQLHSTMT hstmt;
+    // Get statement handle. Disable auto-commit mode to wrap select + update.
     ODBCConn conn(getHDBC());
-    SQLAllocHandle(SQL_HANDLE_STMT,conn,&hstmt);
+    SQLRETURN sr = SQLSetConnectAttr(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, 0);
+    if (!SQL_SUCCEEDED(sr))
+        throw IOException("ODBC StorageService failed to disable auto-commit mode.");
+    conn.autoCommit = false;
+    SQLHSTMT stmt = getHSTMT(conn);
 
-    // Prepare and exectute select statement.
-    string q = string("SELECT expires,value FROM ") + table +
-               " WHERE context='" + context + "' AND key='" + key + "'";
-    log->debug("SQL: %s", q.str());
-
-    SQLRETURN sr=SQLExecDirect(hstmt, (SQLCHAR*)q.c_str(), SQL_NTS);
-    if (!SQL_SUCCEEDED(sr)) {
-        log->error("error searching for (t=%s, c=%s, k=%s)", table, context, key);
-        log_error(hstmt, SQL_HANDLE_STMT);
-        SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
-        return E_FAIL;
-    }
+    // First, fetch the current version for later, which also ensures the record still exists.
+    char timebuf[32];
+    timestampFromTime(time(nullptr), timebuf);
+    SQLString scontext(context);
+    SQLString skey(key);
+    string q("SELECT version FROM ");
+    q = q + table + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "' AND expires > " + timebuf;
 
-    // retrieve data 
-    SQLBindCol(hstmt,1,SQL_C_TYPE_TIMESTAMP,&expires,0,NULL);
-    // SQLBindCol(hstmt,1,SQL_C_CHAR,value,sizeof(value),NULL);
+    m_log.debug("SQL: %s", q.c_str());
 
-    if ((sr=SQLFetch(hstmt)) == SQL_NO_DATA) {
-        SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
-        return S_FALSE;
+    sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    if (!SQL_SUCCEEDED(sr)) {
+        m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService search failed.");
     }
 
-    // expire time from bound col
-    exp = timeFromTimestamp(expires);
-    if (time(NULL)>ezp) {
-        log->debug(".. expired");
-        return false;
+    SQLSMALLINT ver;
+    SQLINTEGER widever;
+    if (m_wideVersion)
+        SQLBindCol(stmt, 1, SQL_C_SLONG, &widever, 0, nullptr);
+    else
+        SQLBindCol(stmt, 1, SQL_C_SSHORT, &ver, 0, nullptr);
+    if ((sr = SQLFetch(stmt)) == SQL_NO_DATA) {
+        return 0;
     }
-    if (pexpiration) pexpiration = exp;
-
-    // value by getdata
 
-    // see how much text there is
-    if (maxsize==0) {
-         SQLINTEGER nch;
-         SQLCHAR tv[12];
-         sr = SQLGetData(hstmt, 2, SQL_C_CHAR, tvp, BUFSIZE_TEXT_BLOCK, &nch);
-         if (sr==SQL_SUCCESS || sr==SQL_SUCCESS_WITH_INFO) {
-             maxsize = nch;
-         }
+    // Check version?
+    if (version > 0 && version != (m_wideVersion ? widever : ver)) {
+        return -1;
     }
-
-    tvalue = (SQLCHAR*) malloc(maxsize+1);
-    sr = SQLGetData(hstmt, 2, SQL_C_CHAR, tvalue, maxsize, &nch);
-        if (sr!=SQL_SUCCESS) {
-            log->error("error retriving value for (t=%s, c=%s, k=%s)", table, context, key);
-            log_error(hstmt, SQL_HANDLE_STMT);
-            SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
-            return E_FAIL;
-        }
+    else if ((m_wideVersion && widever == INT_MAX) || (!m_wideVersion && ver == 32767)) {
+        m_log.error("record version overflow (t=%s, c=%s, k=%s)", table, context, key);
+        throw IOException("Version overflow, record in ODBC StorageService could not be updated.");
     }
-    pvalue = string(tvalue);
-    free(tvalue);
-
-    log->debug(".. value found");
-
-    return sr;
-}
-
-
-// update 
-
-HRESULT ODBCStorageService::updateRow(const char *table, const char* context, const char* key, const char* value, time_t expiration)
-{
-#ifdef _DEBUG
-    xmltooling::NDC ndc("updateRow");
-#endif
 
-    char timebuf[32];
-    timestampFromTime(expiration, timebuf);
-
-    // Get statement handle.
-    SQLHSTMT hstmt;
-    ODBCConn conn(getHDBC());
-    SQLAllocHandle(SQL_HANDLE_STMT,conn,&hstmt);
+    SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+    stmt = getHSTMT(conn);
 
     // Prepare and exectute update statement.
+    q = string("UPDATE ") + table + " SET ";
+
+    if (value)
+        q = q + "value=?, version=version+1";
 
-    string expstr = "";
-    if (expiration) expstr = string(",expires = '") + timebuf + "' ";
+    if (expiration) {
+        timestampFromTime(expiration, timebuf);
+        if (value)
+            q += ',';
+        q = q + "expires = " + timebuf;
+    }
 
-    string q  = string("UPDATE ") + table + " SET value='" + value + "'" + expstr + 
-               " WHERE context='" + context + "' AND key='" + key + "' AND expires > NOW()";
-    log->debug("SQL: %s", q.str());
+    q = q + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "'";
 
-    HRESULT hr=NOERROR;
-    SQLRETURN sr=SQLExecDirect(hstmt, (SQLCHAR*)q.str().c_str(), SQL_NTS);
+    sr = SQLPrepare(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
     if (!SQL_SUCCEEDED(sr)) {
-        log->error("update record failed (t=%s, c=%s, k=%s", table, context, key);
-        log_error(hstmt, SQL_HANDLE_STMT);
-        hr=E_FAIL;
+        m_log.error("update of record failed (t=%s, c=%s, k=%s", table, context, key);
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService failed to update record.");
+    }
+    m_log.debug("SQLPrepare succeeded. SQL: %s", q.c_str());
+
+    SQLLEN b_ind = SQL_NTS;
+    if (value) {
+        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 (value = %s)", value);
+            log_error(stmt, SQL_HANDLE_STMT);
+            throw IOException("ODBC StorageService failed to update record.");
+        }
+        m_log.debug("SQLBindParam succeeded (value = %s)", value);
     }
 
-    SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
-    return hr;
-}
+    int attempts = 3;
+    pair<bool,bool> logres;
+    do {
+        logres = make_pair(false,false);
+        attempts--;
+        sr = SQLExecute(stmt);
+        if (sr == SQL_NO_DATA)
+            return 0;   // went missing?
+        else if (SQL_SUCCEEDED(sr)) {
+            m_log.debug("SQLExecute of update succeeded");
+            return (m_wideVersion ? widever : ver) + 1;
+        }
 
+        m_log.error("update of record failed (t=%s, c=%s, k=%s)", table, context, key);
+        logres = log_error(stmt, SQL_HANDLE_STMT);
+    } while (attempts && logres.first);
 
-// delete
+    throw IOException("ODBC StorageService failed to update record.");
+}
 
-HRESULT ODBCStorageService::deleteRow(const char *table, const char *context, const char* key)
+bool ODBCStorageService::deleteRow(const char *table, const char *context, const char* key)
 {
 #ifdef _DEBUG
     xmltooling::NDC ndc("deleteRow");
 #endif
 
     // Get statement handle.
-    SQLHSTMT hstmt;
     ODBCConn conn(getHDBC());
-    SQLAllocHandle(SQL_HANDLE_STMT,conn,&hstmt);
+    SQLHSTMT stmt = getHSTMT(conn);
 
     // Prepare and execute delete statement.
-    string q = string("DELETE FROM ") + table + " WHERE context='" + context + "' AND key='" + key + "'";
-    log->debug("SQL: %s", q.str());
-
-    SQLRETURN sr=SQLExecDirect(hstmt, (SQLCHAR*)q.c_str(), SQL_NTS);
-    HRESULT hr=NOERROR;
-    if (sr==SQL_NO_DATA)
-        hr=S_FALSE;
+    SQLString scontext(context);
+    SQLString skey(key);
+    string q = string("DELETE FROM ") + table + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "'";
+    m_log.debug("SQL: %s", q.c_str());
+
+    SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+     if (sr == SQL_NO_DATA)
+        return false;
     else if (!SQL_SUCCEEDED(sr)) {
-        log->error("error deleting record (t=%s, c=%s, k=%s)", table, context, key);
-        log_error(hstmt, SQL_HANDLE_STMT);
-        hr=E_FAIL;
+        m_log.error("error deleting record (t=%s, c=%s, k=%s)", table, context, key);
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService failed to delete record.");
     }
 
-    SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
-    return hr;
+    return true;
 }
 
 
-// cleanup - delete expired entries
-
 void ODBCStorageService::cleanup()
 {
 #ifdef _DEBUG
     xmltooling::NDC ndc("cleanup");
 #endif
 
-    Mutex* mutex = xmltooling::Mutex::create();
-
-    int rerun_timer = 0;
-    int timeout_life = 0;
+    scoped_ptr<Mutex> mutex(Mutex::create());
 
     mutex->lock();
 
-    log->info("cleanup thread started... running every %d secs", m_cleanupInterval);
+    m_log.info("cleanup thread started... running every %d secs", m_cleanupInterval);
 
     while (!shutdown) {
-        shutdown_wait->timedwait(mutex, m_cleanupInterval);
-
-        if (shutdown) break;
-
-        reap(NULL);
+        shutdown_wait->timedwait(mutex.get(), m_cleanupInterval);
+        if (shutdown)
+            break;
+        try {
+            reap(nullptr);
+        }
+        catch (std::exception& ex) {
+            m_log.error("cleanup thread swallowed exception: %s", ex.what());
+        }
     }
 
-    log->info("cleanup thread exiting...");
+    m_log.info("cleanup thread exiting...");
 
     mutex->unlock();
-    delete mutex;
-    xmltooling::Thread::exit(NULL);
+    Thread::exit(nullptr);
 }
 
-void* ODBCStorageService::cleanup_fcn(void* cache_p)
+void* ODBCStorageService::cleanup_fn(void* cache_p)
 {
   ODBCStorageService* cache = (ODBCStorageService*)cache_p;
 
@@ -648,75 +786,100 @@ void* ODBCStorageService::cleanup_fcn(void* cache_p)
 
   // Now run the cleanup process.
   cache->cleanup();
-  return NULL;
+  return nullptr;
 }
 
+void ODBCStorageService::updateContext(const char *table, const char* context, time_t expiration)
+{
+#ifdef _DEBUG
+    xmltooling::NDC ndc("updateContext");
+#endif
+
+    // Get statement handle.
+    ODBCConn conn(getHDBC());
+    SQLHSTMT stmt = getHSTMT(conn);
+
+    char timebuf[32];
+    timestampFromTime(expiration, timebuf);
+
+    char nowbuf[32];
+    timestampFromTime(time(nullptr), nowbuf);
+
+    SQLString scontext(context);
+    string q = string("UPDATE ") + table + " SET expires = " + timebuf + " WHERE context='" + scontext.tostr() + "' AND expires > " + nowbuf;
+
+    m_log.debug("SQL: %s", q.c_str());
 
-// remove expired entries for a context
+    SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    if ((sr != SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
+        m_log.error("error updating records (t=%s, c=%s)", table, context ? context : "all");
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService failed to update context expiration.");
+    }
+}
 
-void ODBCStorageService::reapRows(const char *table, const char* context)
+void ODBCStorageService::reap(const char *table, const char* context)
 {
 #ifdef _DEBUG
-    xmltooling::NDC ndc("reapRows");
+    xmltooling::NDC ndc("reap");
 #endif
 
     // Get statement handle.
-    SQLHSTMT hstmt;
     ODBCConn conn(getHDBC());
-    SQLAllocHandle(SQL_HANDLE_STMT,conn,&hstmt);
+    SQLHSTMT stmt = getHSTMT(conn);
 
     // Prepare and execute delete statement.
+    char nowbuf[32];
+    timestampFromTime(time(nullptr), nowbuf);
     string q;
     if (context) {
-        q = string("DELETE FROM ") + table + " WHERE context='" + context + "' AND expires<NOW()";
-    } else {
-        q = string("DELETE FROM ") + table + " WHERE expires<NOW()";
+        SQLString scontext(context);
+        q = string("DELETE FROM ") + table + " WHERE context='" + scontext.tostr() + "' AND expires <= " + nowbuf;
     }
-    log->debug("SQL: %s", q.str());
-
-    SQLRETURN sr=SQLExecDirect(hstmt, (SQLCHAR*)q.c_str(), SQL_NTS);
-    HRESULT hr=NOERROR;
-    if (sr==SQL_NO_DATA)
-        hr=S_FALSE;
-    else if (!SQL_SUCCEEDED(sr)) {
-        log->error("error expiring records (t=%s, c=%s)", table, context?context:"null");
-        log_error(hstmt, SQL_HANDLE_STMT);
-        hr=E_FAIL;
+    else {
+        q = string("DELETE FROM ") + table + " WHERE expires <= " + nowbuf;
     }
+    m_log.debug("SQL: %s", q.c_str());
 
-    SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
+    SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    if ((sr != SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
+        m_log.error("error expiring records (t=%s, c=%s)", table, context ? context : "all");
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService failed to purge expired records.");
+    }
 }
 
-
-
-// remove all entries for a context
-
-void ODBCStorageService::deleteCtx(const char *table, const char* context)
+void ODBCStorageService::deleteContext(const char *table, const char* context)
 {
 #ifdef _DEBUG
-    xmltooling::NDC ndc("deleteCtx");
+    xmltooling::NDC ndc("deleteContext");
 #endif
 
     // Get statement handle.
-    SQLHSTMT hstmt;
     ODBCConn conn(getHDBC());
-    SQLAllocHandle(SQL_HANDLE_STMT,conn,&hstmt);
+    SQLHSTMT stmt = getHSTMT(conn);
 
     // Prepare and execute delete statement.
-    string q = string("DELETE FROM ") + table + " WHERE context='" + context + "'";
-    log->debug("SQL: %s", q.str());
-
-    SQLRETURN sr=SQLExecDirect(hstmt, (SQLCHAR*)q.c_str(), SQL_NTS);
-    HRESULT hr=NOERROR;
-    if (sr==SQL_NO_DATA)
-        hr=S_FALSE;
-    else if (!SQL_SUCCEEDED(sr)) {
-        log->error("error deleting context (t=%s, c=%s)", table, context);
-        log_error(hstmt, SQL_HANDLE_STMT);
-        hr=E_FAIL;
+    SQLString scontext(context);
+    string q = string("DELETE FROM ") + table + " WHERE context='" + scontext.tostr() + "'";
+    m_log.debug("SQL: %s", q.c_str());
+
+    SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    if ((sr != SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
+        m_log.error("error deleting context (t=%s, c=%s)", table, context);
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService failed to delete context.");
     }
+}
 
-    SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
+extern "C" int ODBCSTORE_EXPORTS xmltooling_extension_init(void*)
+{
+    // Register this SS type
+    XMLToolingConfig::getConfig().StorageServiceManager.registerFactory("ODBC", ODBCStorageServiceFactory);
+    return 0;
+}
+
+extern "C" void ODBCSTORE_EXPORTS xmltooling_extension_term()
+{
+    XMLToolingConfig::getConfig().StorageServiceManager.deregisterFactory("ODBC");
 }