From 639cdeed5247d5ddc3ac8889688767c6f0da559c Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 15 Jul 2010 22:51:10 +0000 Subject: Cache that a DSN is unavailable for 60sec --- libodbcpp/connection.cpp | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'libodbcpp/connection.cpp') diff --git a/libodbcpp/connection.cpp b/libodbcpp/connection.cpp index 03bf813..8eb6b40 100644 --- a/libodbcpp/connection.cpp +++ b/libodbcpp/connection.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "connection.h" #include "error.h" @@ -12,33 +13,33 @@ ODBC::Connection::Connection(const DSN& d) : { SQLRETURN dberr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate handle"); + throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate handle"); } - dberr = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC2, 0); + dberr = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_ENV, env, "Set ODBC version"); + throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set ODBC version"); } dberr = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle"); + throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle"); } dberr = SQLSetConnectAttr(conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_ENV, env, "Set connection attributes"); + throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set connection attributes"); } dberr = SQLConnect(conn, (SQLCHAR*)d.dsn.c_str(), SQL_NTS, (SQLCHAR*)d.username.c_str(), SQL_NTS, (SQLCHAR*)d.password.c_str(), SQL_NTS); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_DBC, conn, "Connect"); + throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Connect"); } dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit"); + throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit"); } } @@ -50,32 +51,32 @@ ODBC::Connection::Connection(const std::string & s) : { SQLRETURN dberr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate handle"); + throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate handle"); } dberr = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_ENV, env, "Set ODBC version"); + throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set ODBC version"); } dberr = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle"); + throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle"); } dberr = SQLSetConnectAttr(conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_ENV, env, "Set connection attributes"); + throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set connection attributes"); } dberr = SQLDriverConnect(conn, NULL, (SQLCHAR*)s.c_str(), s.length(), NULL, 0, NULL, SQL_DRIVER_NOPROMPT); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_DBC, conn, "Connect"); + throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Connect"); } dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON); if ((dberr != SQL_SUCCESS)) { - throw Error(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit"); + throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit"); } } @@ -195,3 +196,15 @@ ODBC::Connection::getAttrInt(SQLINTEGER attr) const return result; } +ODBC::ConnectionError::ConnectionError(RETCODE err, SQLSMALLINT handletype, SQLHANDLE handle, char const * stage) : + ODBC::Error(err, handletype, handle, "%s", stage), + FailureTime(time(NULL)) +{ +} + +ODBC::ConnectionError::ConnectionError(const ConnectionError & e) : + ODBC::Error(strdup(e.what())), + FailureTime(e.FailureTime) +{ +} + -- cgit v1.2.3