changed set|getTimeout() => set|getLoginTimeout()

added set|getConnectionTimeout()
This commit is contained in:
Aleksandar Fabijanic 2009-02-28 01:52:41 +00:00
parent 68a79674c1
commit b0bbfb5554
25 changed files with 227 additions and 88 deletions

View File

@ -63,7 +63,7 @@ public:
/// Returns the name associated with this connector.
virtual Poco::AutoPtr<Poco::Data::SessionImpl> createSession(const std::string& connectionString,
std::size_t timeout = Poco::Data::SessionImpl::CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = Poco::Data::SessionImpl::LOGIN_TIMEOUT_DEFAULT);
/// Creates a MySQL SessionImpl object and initializes it with the given connectionString.
static void registerConnector();

View File

@ -63,7 +63,7 @@ public:
static const std::string MYSQL_SERIALIZABLE;
SessionImpl(const std::string& connectionString,
std::size_t timeout = CONNECT_TIMEOUT_DEFAULT);
std::size_t loginTimeout = LOGIN_TIMEOUT_DEFAULT);
/// Creates the SessionImpl. Opens a connection to the database
///
/// Connection string format:
@ -91,6 +91,12 @@ public:
bool isConnected();
/// Returns true if connected, false otherwise.
void setConnectionTimeout(std::size_t timeout);
/// Sets the session connection timeout value.
std::size_t getConnectionTimeout();
/// Returns the session connection timeout value.
void begin();
/// Starts a transaction
@ -171,6 +177,7 @@ private:
SessionHandle _handle;
bool _connected;
bool _inTransaction;
std::size_t _timeout;
Poco::FastMutex _mutex;
};
@ -207,12 +214,30 @@ inline const std::string& SessionImpl::connectorName()
}
inline bool SessionImpl::isTransaction()
{
return _inTransaction;
}
inline bool SessionImpl::isTransactionIsolation(Poco::UInt32 ti)
{
return getTransactionIsolation() == ti;
}
inline bool SessionImpl::isConnected()
{
return _connected;
}
inline std::size_t SessionImpl::getConnectionTimeout()
{
return _timeout;
}
template <>
inline std::string& SessionImpl::getValue(MYSQL_BIND* pResult, std::string& val)
{

View File

@ -66,8 +66,8 @@ const std::string SessionImpl::MYSQL_REPEATABLE_READ = "REPEATABLE READ";
const std::string SessionImpl::MYSQL_SERIALIZABLE = "SERIALIZABLE";
SessionImpl::SessionImpl(const std::string& connectionString, std::size_t timeout) :
Poco::Data::AbstractSessionImpl<SessionImpl>(toLower(connectionString), timeout),
SessionImpl::SessionImpl(const std::string& connectionString, std::size_t loginTimeout) :
Poco::Data::AbstractSessionImpl<SessionImpl>(toLower(connectionString), loginTimeout),
_handle(0),
_connected(false),
_inTransaction(false)
@ -77,6 +77,7 @@ SessionImpl::SessionImpl(const std::string& connectionString, std::size_t timeou
&SessionImpl::getInsertId);
open();
setConnectionTimeout(CONNECTION_TIMEOUT_DEFAULT);
}
@ -95,7 +96,7 @@ void SessionImpl::open(const std::string& connect)
_handle.init();
unsigned int timeout = static_cast<unsigned int>(getTimeout());
unsigned int timeout = static_cast<unsigned int>(getLoginTimeout());
_handle.options(MYSQL_OPT_CONNECT_TIMEOUT, timeout);
std::map<std::string, std::string> options;
@ -275,17 +276,13 @@ void SessionImpl::close()
_connected = false;
}
}
bool SessionImpl::isConnected()
{
return _connected;
}
bool SessionImpl::isTransaction()
void SessionImpl::setConnectionTimeout(std::size_t timeout)
{
return _inTransaction;
_handle.options(MYSQL_OPT_READ_TIMEOUT, static_cast<unsigned int>(timeout));
_handle.options(MYSQL_OPT_WRITE_TIMEOUT, static_cast<unsigned int>(timeout));
_timeout = timeout;
}

View File

@ -43,6 +43,7 @@
#include "Poco/Data/MySQL/Connector.h"
#include "Poco/Data/MySQL/MySQLException.h"
#include "Poco/Data/Nullable.h"
#include "Poco/Data/DataException.h"
#include <iostream>
using namespace Poco::Data;
@ -764,7 +765,7 @@ CppUnit::Test* MySQLTest::suite()
{
_pSession = new Session(MySQL::Connector::KEY, _dbConnString);
}
catch (ConnectionException& ex)
catch (ConnectionFailedException& ex)
{
std::cout << ex.displayText() << std::endl;
return 0;

View File

@ -80,8 +80,8 @@ private:
const ConnectionHandle& operator=(const ConnectionHandle&);
const EnvironmentHandle* _pEnvironment;
SQLHDBC _hdbc;
bool _ownsEnvironment;
SQLHDBC _hdbc;
bool _ownsEnvironment;
};

View File

@ -66,7 +66,7 @@ public:
/// Returns the name associated with this connector.
Poco::AutoPtr<Poco::Data::SessionImpl> createSession(const std::string& connectionString,
std::size_t timeout = Poco::Data::SessionImpl::CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = Poco::Data::SessionImpl::LOGIN_TIMEOUT_DEFAULT);
/// Creates a ODBC SessionImpl object and initializes it with the given connectionString.
static void registerConnector();

View File

@ -79,7 +79,7 @@ private:
const EnvironmentHandle& operator=(const EnvironmentHandle&);
SQLHENV _henv;
bool _isOwner;
bool _isOwner;
};

View File

@ -74,7 +74,7 @@ public:
};
SessionImpl(const std::string& connect,
std::size_t timeout,
std::size_t loginTimeout,
std::size_t maxFieldSize = ODBC_MAX_FIELD_SIZE,
bool autoBind = true,
bool autoExtract = true);
@ -104,6 +104,12 @@ public:
bool isConnected();
/// Returns true if session is connected
void setConnectionTimeout(std::size_t timeout);
/// Sets the session connection timeout value.
std::size_t getConnectionTimeout();
/// Returns the session connection timeout value.
void begin();
/// Starts a transaction

View File

@ -50,11 +50,11 @@ namespace ODBC {
SessionImpl::SessionImpl(const std::string& connect,
std::size_t timeout,
std::size_t loginTimeout,
std::size_t maxFieldSize,
bool autoBind,
bool autoExtract):
Poco::Data::AbstractSessionImpl<SessionImpl>(connect, timeout),
Poco::Data::AbstractSessionImpl<SessionImpl>(connect, loginTimeout),
_connector(toLower(Connector::KEY)),
_maxFieldSize(maxFieldSize),
_autoBind(autoBind),
@ -116,11 +116,11 @@ void SessionImpl::open(const std::string& connect)
poco_assert_dbg (!connectionString().empty());
SQLUINTEGER tout = static_cast<SQLUINTEGER>(getTimeout());
SQLUINTEGER tout = static_cast<SQLUINTEGER>(getLoginTimeout());
if (Utility::isError(SQLSetConnectAttr(_db, SQL_ATTR_LOGIN_TIMEOUT, (SQLPOINTER) tout, 0)))
{
if (Utility::isError(SQLGetConnectAttr(_db, SQL_ATTR_LOGIN_TIMEOUT, &tout, 0, 0)) ||
getTimeout() != tout)
getLoginTimeout() != tout)
{
ConnectionError e(_db);
throw ConnectionFailedException(e.toString());
@ -172,6 +172,45 @@ void SessionImpl::open(const std::string& connect)
}
bool SessionImpl::isConnected()
{
SQLUINTEGER value = 0;
if (Utility::isError(Poco::Data::ODBC::SQLGetConnectAttr(_db,
SQL_ATTR_CONNECTION_DEAD,
&value,
0,
0))) return false;
return (SQL_CD_FALSE == value);
}
void SessionImpl::setConnectionTimeout(std::size_t timeout)
{
SQLUINTEGER value = static_cast<SQLUINTEGER>(timeout);
checkError(Poco::Data::ODBC::SQLSetConnectAttr(_db,
SQL_ATTR_CONNECTION_TIMEOUT,
&value,
SQL_IS_UINTEGER), "Failed to set connection timeout.");
}
std::size_t SessionImpl::getConnectionTimeout()
{
SQLUINTEGER value = 0;
checkError(Poco::Data::ODBC::SQLGetConnectAttr(_db,
SQL_ATTR_CONNECTION_TIMEOUT,
&value,
0,
0), "Failed to get connection timeout.");
return value;
}
bool SessionImpl::canTransact()
{
if (ODBC_TXN_CAPABILITY_UNKNOWN == _canTransact)
@ -278,7 +317,7 @@ void SessionImpl::autoCommit(const std::string&, bool val)
SQL_ATTR_AUTOCOMMIT,
val ? (SQLPOINTER) SQL_AUTOCOMMIT_ON :
(SQLPOINTER) SQL_AUTOCOMMIT_OFF,
0), "Failed to set automatic commit.");
SQL_IS_UINTEGER), "Failed to set automatic commit.");
}
@ -296,21 +335,6 @@ bool SessionImpl::isAutoCommit(const std::string&)
}
bool SessionImpl::isConnected()
{
Poco::UInt32 value = 0;
if (Utility::isError(Poco::Data::ODBC::SQLGetConnectAttr(_db,
SQL_ATTR_CONNECTION_DEAD,
&value,
0,
0)))
return false;
return (SQL_CD_FALSE == value);
}
bool SessionImpl::isTransaction()
{
if (!canTransact()) return false;

View File

@ -65,7 +65,9 @@ CppUnit::Test* ODBCTestSuite::suite()
addTest(pSuite, ODBCSQLiteTest::suite());
addTest(pSuite, ODBCSQLServerTest::suite());
addTest(pSuite, ODBCDB2Test::suite());
#if defined(POCO_OS_FAMILY_WINDOWS)
// MS Access driver does not support connection status detection
// disabled for the time being
#if 0 //defined(POCO_OS_FAMILY_WINDOWS)
addTest(pSuite, ODBCAccessTest::suite());
#endif

View File

@ -66,7 +66,7 @@ public:
/// Returns the name associated with this connector.
Poco::AutoPtr<Poco::Data::SessionImpl> createSession(const std::string& connectionString,
std::size_t timeout = Poco::Data::SessionImpl::CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = Poco::Data::SessionImpl::LOGIN_TIMEOUT_DEFAULT);
/// Creates a SQLite SessionImpl object and initializes it with the given connectionString.
static void registerConnector();

View File

@ -60,7 +60,7 @@ class SQLite_API SessionImpl: public Poco::Data::AbstractSessionImpl<SessionImpl
{
public:
SessionImpl(const std::string& fileName,
std::size_t timeout = CONNECT_TIMEOUT_DEFAULT);
std::size_t loginTimeout = LOGIN_TIMEOUT_DEFAULT);
/// Creates the SessionImpl. Opens a connection to the database.
~SessionImpl();
@ -69,15 +69,6 @@ public:
Poco::Data::StatementImpl* createStatementImpl();
/// Returns an SQLite StatementImpl.
void begin();
/// Starts a transaction.
void commit();
/// Commits and ends a transaction.
void rollback();
/// Aborts a transaction.
void open(const std::string& connect = "");
/// Opens a connection to the Database.
@ -87,6 +78,21 @@ public:
bool isConnected();
/// Returns true if connected, false otherwise.
void setConnectionTimeout(std::size_t timeout);
/// Sets the session connection timeout value.
std::size_t getConnectionTimeout();
/// Returns the session connection timeout value.
void begin();
/// Starts a transaction.
void commit();
/// Commits and ends a transaction.
void rollback();
/// Aborts a transaction.
bool canTransact();
/// Returns true if session has transaction capabilities.
@ -116,6 +122,7 @@ private:
sqlite3* _pDB;
bool _connected;
bool _isTransaction;
int _timeout;
static const std::string DEFERRED_BEGIN_TRANSACTION;
static const std::string COMMIT_TRANSACTION;
@ -144,6 +151,12 @@ inline const std::string& SessionImpl::connectorName()
}
inline std::size_t SessionImpl::getConnectionTimeout()
{
return static_cast<std::size_t>(_timeout);
}
} } } // namespace Poco::Data::SQLite

View File

@ -58,14 +58,15 @@ const std::string SessionImpl::COMMIT_TRANSACTION("COMMIT");
const std::string SessionImpl::ABORT_TRANSACTION("ROLLBACK");
SessionImpl::SessionImpl(const std::string& fileName, std::size_t timeout):
Poco::Data::AbstractSessionImpl<SessionImpl>(fileName, timeout),
SessionImpl::SessionImpl(const std::string& fileName, std::size_t loginTimeout):
Poco::Data::AbstractSessionImpl<SessionImpl>(fileName, loginTimeout),
_connector(toLower(Connector::KEY)),
_pDB(0),
_connected(false),
_isTransaction(false)
{
open();
setConnectionTimeout(CONNECTION_TIMEOUT_DEFAULT);
}
@ -179,7 +180,7 @@ void SessionImpl::open(const std::string& connect)
{
ActiveConnector connector(connectionString(), &_pDB);
ActiveResult<int> result = connector.connect();
if (!result.tryWait(getTimeout() * 1000))
if (!result.tryWait(getLoginTimeout() * 1000))
throw ConnectionFailedException("Timed out.");
int rc = result.data();
@ -215,4 +216,13 @@ bool SessionImpl::isConnected()
}
void SessionImpl::setConnectionTimeout(std::size_t timeout)
{
int tout = 1000 * timeout;
int rc = sqlite3_busy_timeout(_pDB, tout);
if (rc != 0) Utility::throwException(rc);
_timeout = tout;
}
} } } // namespace Poco::Data::SQLite

View File

@ -73,7 +73,7 @@ public:
/// The getter method for a property.
AbstractSessionImpl(const std::string& connectionString,
std::size_t timeout = CONNECT_TIMEOUT_DEFAULT): SessionImpl(connectionString, timeout),
std::size_t timeout = LOGIN_TIMEOUT_DEFAULT): SessionImpl(connectionString, timeout),
_storage(std::string("deque")),
_bulk(false),
_emptyStringIsNull(false),

View File

@ -67,7 +67,7 @@ public:
/// Returns the name associated with this connector.
virtual Poco::AutoPtr<SessionImpl> createSession(const std::string& connectionString,
std::size_t timeout = SessionImpl::CONNECT_TIMEOUT_DEFAULT) = 0;
std::size_t timeout = SessionImpl::LOGIN_TIMEOUT_DEFAULT) = 0;
/// Create a SessionImpl object and initialize it with the given connectionString.
};

View File

@ -73,6 +73,8 @@ public:
void open(const std::string& connect = "");
void close();
bool isConnected();
void setConnectionTimeout(std::size_t timeout);
std::size_t getConnectionTimeout();
bool canTransact();
bool isTransaction();
void setTransactionIsolation(Poco::UInt32);

View File

@ -173,7 +173,7 @@ class Data_API Session
/// For complete list of supported data types with their respective specifications, see the documentation for format in Foundation.
{
public:
static const std::size_t CONNECT_TIMEOUT_DEFAULT = SessionImpl::CONNECT_TIMEOUT_DEFAULT;
static const std::size_t LOGIN_TIMEOUT_DEFAULT = SessionImpl::LOGIN_TIMEOUT_DEFAULT;
static const Poco::UInt32 TRANSACTION_READ_UNCOMMITTED = 0x00000001L;
static const Poco::UInt32 TRANSACTION_READ_COMMITTED = 0x00000002L;
static const Poco::UInt32 TRANSACTION_REPEATABLE_READ = 0x00000004L;
@ -184,12 +184,12 @@ public:
Session(const std::string& connector,
const std::string& connectionString,
std::size_t timeout = CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = LOGIN_TIMEOUT_DEFAULT);
/// Creates a new session, using the given connector (which must have
/// been registered), and connectionString.
Session(const std::string& connection,
std::size_t timeout = CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = LOGIN_TIMEOUT_DEFAULT);
/// Creates a new session, using the given connection (must be in
/// "connection:///connectionString" format).
@ -232,11 +232,17 @@ public:
void reconnect();
/// Closes the session and opens it.
void setTimeout(std::size_t timeout);
/// Sets the session timeout value.
void setLoginTimeout(std::size_t timeout);
/// Sets the session login timeout value.
std::size_t getTimeout() const;
/// Returns the session timeout value.
std::size_t getLoginTimeout() const;
/// Returns the session login timeout value.
void setConnectionTimeout(std::size_t timeout);
/// Sets the session connection timeout value.
std::size_t getConnectionTimeout();
/// Returns the session connection timeout value.
void begin();
/// Starts a transaction.
@ -355,15 +361,27 @@ inline void Session::reconnect()
}
inline void Session::setTimeout(std::size_t timeout)
inline void Session::setLoginTimeout(std::size_t timeout)
{
_pImpl->setTimeout(timeout);
_pImpl->setLoginTimeout(timeout);
}
inline std::size_t Session::getTimeout() const
inline std::size_t Session::getLoginTimeout() const
{
return _pImpl->getTimeout();
return _pImpl->getLoginTimeout();
}
inline void Session::setConnectionTimeout(std::size_t timeout)
{
_pImpl->setConnectionTimeout(timeout);
}
inline std::size_t Session::getConnectionTimeout()
{
return _pImpl->getConnectionTimeout();
}

View File

@ -85,12 +85,12 @@ public:
Session create(const std::string& key,
const std::string& connectionString,
std::size_t timeout = Session::CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = Session::LOGIN_TIMEOUT_DEFAULT);
/// Creates a Session for the given key with the connectionString. Throws an Poco:Data::UnknownDataBaseException
/// if no Connector is registered for that key.
Session create(const std::string& uri,
std::size_t timeout = Session::CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = Session::LOGIN_TIMEOUT_DEFAULT);
/// Creates a Session for the given URI (must be in key:///connectionString format).
/// Throws an Poco:Data::UnknownDataBaseException if no Connector is registered for the key.

View File

@ -59,14 +59,20 @@ class Data_API SessionImpl: public Poco::RefCountedObject
/// SessionImpl objects are noncopyable.
{
public:
static const std::size_t CONNECT_TIMEOUT_INFINITE = 0;
static const std::size_t LOGIN_TIMEOUT_INFINITE = 0;
/// Infinite connection/login timeout.
static const std::size_t CONNECT_TIMEOUT_DEFAULT = 30;
static const std::size_t LOGIN_TIMEOUT_DEFAULT = 60;
/// Default connection/login timeout in seconds.
static const std::size_t CONNECTION_TIMEOUT_INFINITE = 0;
/// Infinite connection/login timeout.
static const std::size_t CONNECTION_TIMEOUT_DEFAULT = CONNECTION_TIMEOUT_INFINITE;
/// Default connection/login timeout in seconds.
SessionImpl(const std::string& connectionString,
std::size_t timeout = CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = LOGIN_TIMEOUT_DEFAULT);
/// Creates the SessionImpl.
virtual ~SessionImpl();
@ -89,11 +95,17 @@ public:
virtual bool isConnected() = 0;
/// Returns true if session is connected, false otherwise.
void setTimeout(std::size_t timeout);
/// Sets the session timeout value.
void setLoginTimeout(std::size_t timeout);
/// Sets the session login timeout value.
std::size_t getTimeout() const;
/// Returns the session timeout value.
std::size_t getLoginTimeout() const;
/// Returns the session login timeout value.
virtual void setConnectionTimeout(std::size_t timeout) = 0;
/// Sets the session connection timeout value.
virtual std::size_t getConnectionTimeout() = 0;
/// Returns the session connection timeout value.
void reconnect();
/// Closes the connection and opens it again.
@ -187,7 +199,7 @@ private:
SessionImpl& operator = (const SessionImpl&);
std::string _connectionString;
std::size_t _timeout;
std::size_t _loginTimeout;
};
@ -200,15 +212,15 @@ inline const std::string& SessionImpl::connectionString()
}
inline void SessionImpl::setTimeout(std::size_t timeout)
inline void SessionImpl::setLoginTimeout(std::size_t timeout)
{
_timeout = timeout;
_loginTimeout = timeout;
}
inline std::size_t SessionImpl::getTimeout() const
inline std::size_t SessionImpl::getLoginTimeout() const
{
return _timeout;
return _loginTimeout;
}

View File

@ -45,7 +45,7 @@ namespace Data {
PooledSessionImpl::PooledSessionImpl(PooledSessionHolder* pHolder):
SessionImpl(pHolder->session()->connectionString(),
pHolder->session()->getTimeout()),
pHolder->session()->getLoginTimeout()),
_pHolder(pHolder, true)
{
}
@ -81,6 +81,18 @@ bool PooledSessionImpl::isConnected()
}
void PooledSessionImpl::setConnectionTimeout(std::size_t timeout)
{
return access()->setConnectionTimeout(timeout);
}
std::size_t PooledSessionImpl::getConnectionTimeout()
{
return access()->getConnectionTimeout();
}
bool PooledSessionImpl::canTransact()
{
return access()->canTransact();

View File

@ -44,7 +44,7 @@ namespace Data {
SessionImpl::SessionImpl(const std::string& connectionString, std::size_t timeout):
_connectionString(connectionString),
_timeout(timeout)
_loginTimeout(timeout)
{
}

View File

@ -61,7 +61,7 @@ public:
/// Returns the name associated with this connector.
Poco::AutoPtr<Poco::Data::SessionImpl> createSession(const std::string& connectionString,
std::size_t timeout = SessionImpl::CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = SessionImpl::LOGIN_TIMEOUT_DEFAULT);
/// Creates a test SessionImpl object and initializes it with the given connectionString.
static void addToFactory();

View File

@ -112,9 +112,9 @@ void DataTest::testSession()
assert ("cs" == sess.impl()->connectionString());
assert ("test:///cs" == sess.uri());
assert (sess.getTimeout() == Session::CONNECT_TIMEOUT_DEFAULT);
sess.setTimeout(123);
assert (sess.getTimeout() == 123);
assert (sess.getLoginTimeout() == Session::LOGIN_TIMEOUT_DEFAULT);
sess.setLoginTimeout(123);
assert (sess.getLoginTimeout() == 123);
Session sess2(SessionFactory::instance().create("TeSt:///Cs"));
assert ("test" == sess2.impl()->connectorName());

View File

@ -78,6 +78,17 @@ bool SessionImpl::isConnected()
}
void SessionImpl::setConnectionTimeout(std::size_t timeout)
{
}
std::size_t SessionImpl::getConnectionTimeout()
{
return 0;
}
Poco::Data::StatementImpl* SessionImpl::createStatementImpl()
{
return new TestStatementImpl(*this);

View File

@ -51,7 +51,7 @@ class SessionImpl: public Poco::Data::AbstractSessionImpl<SessionImpl>
{
public:
SessionImpl(const std::string& init,
std::size_t timeout = CONNECT_TIMEOUT_DEFAULT);
std::size_t timeout = LOGIN_TIMEOUT_DEFAULT);
/// Creates the SessionImpl. Opens a connection to the database.
~SessionImpl();
@ -70,6 +70,12 @@ public:
/// Returns true if session is connected to the database,
/// false otherwise.
void setConnectionTimeout(std::size_t timeout);
/// Sets the session connection timeout value.
std::size_t getConnectionTimeout();
/// Returns the session connection timeout value.
void begin();
/// Starts a transaction.