[cpp-sp COMMIT] /branches/REL_2/odbc-store/odbc-store.cpp
noreply at shibboleth.net
noreply at shibboleth.net
Wed Jul 18 18:54:00 EDT 2012
Author: scantor
Date: Wed Jul 18 18:54:00 2012
New Revision: 3730
URL: http://svn.shibboleth.net/view/cpp-sp?rev=3730&view=rev
Log:
Some cleanup and defensive code
Modified:
branches/REL_2/odbc-store/odbc-store.cpp
Modified: branches/REL_2/odbc-store/odbc-store.cpp
URL: http://svn.shibboleth.net/view/cpp-sp/branches/REL_2/odbc-store/odbc-store.cpp?rev=3730&r1=3729&r2=3730&view=diff
==============================================================================
--- branches/REL_2/odbc-store/odbc-store.cpp (original)
+++ branches/REL_2/odbc-store/odbc-store.cpp Wed Jul 18 18:54:00 2012
@@ -112,13 +112,15 @@
struct ODBCConn {
ODBCConn(SQLHDBC conn) : handle(conn), autoCommit(true) {}
~ODBCConn() {
- 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.");
+ 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;
@@ -269,7 +271,7 @@
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_HANDLE), m_isolation(SQL_TXN_SERIALIZABLE)
+ cleanup_thread(nullptr), shutdown(false), m_henv(SQL_NULL_HENV), m_isolation(SQL_TXN_SERIALIZABLE)
{
#ifdef _DEBUG
xmltooling::NDC ndc("ODBCStorageService");
@@ -286,7 +288,7 @@
else
throw XMLToolingException("Unknown transaction isolationLevel property.");
- if (m_henv == SQL_NULL_HANDLE) {
+ if (m_henv == SQL_NULL_HENV) {
// Enable connection pooling.
SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)SQL_CP_ONE_PER_HENV, 0);
@@ -374,33 +376,37 @@
#endif
// Get a handle.
- SQLHDBC handle;
- SQLRETURN sr=SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &handle);
- if (!SQL_SUCCEEDED(sr)) {
+ 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 IOException("ODBC StorageService failed to allocate a connection handle.");
}
- sr=SQLDriverConnect(handle,nullptr,(SQLCHAR*)m_connstring.c_str(),m_connstring.length(),nullptr,0,nullptr,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)) {
m_log.error("failed to connect to database");
log_error(handle, SQL_HANDLE_DBC);
+ 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))
+ if (!SQL_SUCCEEDED(sr)) {
+ SQLDisconnect(handle);
+ SQLFreeHandle(SQL_HANDLE_DBC, handle);
throw IOException("ODBC StorageService failed to set transaction isolation level.");
+ }
return handle;
}
SQLHSTMT ODBCStorageService::getHSTMT(SQLHDBC conn)
{
- SQLHSTMT hstmt;
+ SQLHSTMT hstmt = SQL_NULL_HSTMT;
SQLRETURN sr = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt);
- if (!SQL_SUCCEEDED(sr)) {
+ 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.");
More information about the commits
mailing list