Skip to content

Commit

Permalink
fix(Data::SessionPool): Improve Data::SessionPool thread safety #4206
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-f committed Oct 20, 2023
1 parent 9e212d8 commit 379008a
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 49 deletions.
20 changes: 15 additions & 5 deletions Data/ODBC/include/Poco/Data/ODBC/ConnectionHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ class ODBC_API ConnectionHandle
int getTimeout() const;
/// Returns the connection timeout in seconds.

operator const SQLHDBC& () const;
/// Const conversion operator into reference to native type.

const SQLHDBC& handle() const;
/// Returns const reference to handle;

const SQLHDBC* pHandle() const;
/// Returns const pointer to handle;

operator const SQLHDBC& () const;
/// Const conversion operator into reference to native type.

operator bool();
/// Returns true if handles are not null.
/// True value is not a guarantee that the connection is valid.

private:
operator SQLHDBC& ();
/// Conversion operator into reference to native type.
Expand All @@ -82,8 +86,8 @@ class ODBC_API ConnectionHandle
ConnectionHandle(const ConnectionHandle&);
const ConnectionHandle& operator=(const ConnectionHandle&);

const EnvironmentHandle* _pEnvironment;
SQLHDBC _hdbc;
const EnvironmentHandle* _pEnvironment = nullptr;
SQLHDBC _hdbc = SQL_NULL_HDBC;
std::string _connectString;

friend class Poco::Data::ODBC::SessionImpl;
Expand All @@ -102,6 +106,12 @@ inline ConnectionHandle::operator const SQLHDBC& () const
}


inline ConnectionHandle::operator bool()
{
return _pEnvironment != nullptr && _hdbc != SQL_NULL_HDBC;
}


inline const SQLHDBC& ConnectionHandle::handle() const
{
return _hdbc;
Expand Down
2 changes: 2 additions & 0 deletions Data/ODBC/src/ConnectionHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ int ConnectionHandle::getTimeout() const

bool ConnectionHandle::isConnected() const
{
if (!*this) return false;

SQLULEN value = 0;

if (Utility::isError(Poco::Data::ODBC::SQLGetConnectAttr(_hdbc,
Expand Down
4 changes: 3 additions & 1 deletion Data/ODBC/src/SessionImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ void SessionImpl::autoCommit(const std::string&, bool val)

bool SessionImpl::isAutoCommit(const std::string&) const
{
if (!_db) return false;

SQLULEN value = 0;

checkError(Poco::Data::ODBC::SQLGetConnectAttr(_db,
Expand All @@ -386,7 +388,7 @@ bool SessionImpl::isAutoCommit(const std::string&) const

bool SessionImpl::isTransaction() const
{
if (!canTransact()) return false;
if (!_db || !canTransact()) return false;

SQLULEN value = 0;
checkError(Poco::Data::ODBC::SQLGetConnectAttr(_db,
Expand Down
3 changes: 3 additions & 0 deletions Data/ODBC/testsuite/src/ODBCDB2Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,9 @@ CppUnit::Test* ODBCDB2Test::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ODBCDB2Test");

CppUnit_addTest(pSuite, ODBCDB2Test, testBareboneODBC);
CppUnit_addTest(pSuite, ODBCDB2Test, testConnection);
CppUnit_addTest(pSuite, ODBCDB2Test, testSession);
CppUnit_addTest(pSuite, ODBCDB2Test, testSessionPool);
CppUnit_addTest(pSuite, ODBCDB2Test, testZeroRows);
CppUnit_addTest(pSuite, ODBCDB2Test, testSimpleAccess);
CppUnit_addTest(pSuite, ODBCDB2Test, testComplexType);
Expand Down
3 changes: 3 additions & 0 deletions Data/ODBC/testsuite/src/ODBCMySQLTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ CppUnit::Test* ODBCMySQLTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ODBCMySQLTest");

CppUnit_addTest(pSuite, ODBCMySQLTest, testBareboneODBC);
CppUnit_addTest(pSuite, ODBCMySQLTest, testConnection);
CppUnit_addTest(pSuite, ODBCMySQLTest, testSession);
CppUnit_addTest(pSuite, ODBCMySQLTest, testSessionPool);
CppUnit_addTest(pSuite, ODBCMySQLTest, testZeroRows);
CppUnit_addTest(pSuite, ODBCMySQLTest, testSimpleAccess);
CppUnit_addTest(pSuite, ODBCMySQLTest, testComplexType);
Expand Down
3 changes: 3 additions & 0 deletions Data/ODBC/testsuite/src/ODBCOracleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,9 @@ CppUnit::Test* ODBCOracleTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ODBCOracleTest");

CppUnit_addTest(pSuite, ODBCOracleTest, testBareboneODBC);
CppUnit_addTest(pSuite, ODBCOracleTest, testConnection);
CppUnit_addTest(pSuite, ODBCOracleTest, testSession);
CppUnit_addTest(pSuite, ODBCOracleTest, testSessionPool);
CppUnit_addTest(pSuite, ODBCOracleTest, testZeroRows);
CppUnit_addTest(pSuite, ODBCOracleTest, testSimpleAccess);
CppUnit_addTest(pSuite, ODBCOracleTest, testComplexType);
Expand Down
3 changes: 3 additions & 0 deletions Data/ODBC/testsuite/src/ODBCPostgreSQLTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ CppUnit::Test* ODBCPostgreSQLTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ODBCPostgreSQLTest");

CppUnit_addTest(pSuite, ODBCPostgreSQLTest, testBareboneODBC);
CppUnit_addTest(pSuite, ODBCPostgreSQLTest, testConnection);
CppUnit_addTest(pSuite, ODBCPostgreSQLTest, testSession);
CppUnit_addTest(pSuite, ODBCPostgreSQLTest, testSessionPool);
CppUnit_addTest(pSuite, ODBCPostgreSQLTest, testZeroRows);
CppUnit_addTest(pSuite, ODBCPostgreSQLTest, testSimpleAccess);
CppUnit_addTest(pSuite, ODBCPostgreSQLTest, testComplexType);
Expand Down
13 changes: 1 addition & 12 deletions Data/ODBC/testsuite/src/ODBCSQLServerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,6 @@ void ODBCSQLServerTest::testTempTable()
}


void ODBCSQLServerTest::testConnection()
{
executor().connection(_connectString);
}


void ODBCSQLServerTest::testSession()
{
executor().session(_connectString, 5);
}


void ODBCSQLServerTest::testBLOB()
{
const std::size_t maxFldSize = 250000;
Expand Down Expand Up @@ -902,6 +890,7 @@ CppUnit::Test* ODBCSQLServerTest::suite()
CppUnit_addTest(pSuite, ODBCSQLServerTest, testBareboneODBC);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testConnection);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testSession);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testSessionPool);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testZeroRows);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testSimpleAccess);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testComplexType);
Expand Down
3 changes: 0 additions & 3 deletions Data/ODBC/testsuite/src/ODBCSQLServerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class ODBCSQLServerTest: public ODBCTest

void testBareboneODBC();

void testConnection();
void testSession();

void testTempTable();

void testBLOB();
Expand Down
3 changes: 3 additions & 0 deletions Data/ODBC/testsuite/src/ODBCSQLiteTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ CppUnit::Test* ODBCSQLiteTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ODBCSQLiteTest");

CppUnit_addTest(pSuite, ODBCSQLiteTest, testBareboneODBC);
CppUnit_addTest(pSuite, ODBCSQLiteTest, testConnection);
CppUnit_addTest(pSuite, ODBCSQLiteTest, testSession);
CppUnit_addTest(pSuite, ODBCSQLiteTest, testSessionPool);
CppUnit_addTest(pSuite, ODBCSQLiteTest, testZeroRows);
CppUnit_addTest(pSuite, ODBCSQLiteTest, testSimpleAccess);
CppUnit_addTest(pSuite, ODBCSQLiteTest, testComplexType);
Expand Down
18 changes: 18 additions & 0 deletions Data/ODBC/testsuite/src/ODBCTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ ODBCTest::~ODBCTest()
}


void ODBCTest::testConnection()
{
_pExecutor->connection(_rConnectString);
}


void ODBCTest::testSession()
{
_pExecutor->session(_rConnectString, 5);
}


void ODBCTest::testSessionPool()
{
_pExecutor->sessionPool(_rConnectString, 1, 4, 3, 5);
}


void ODBCTest::testZeroRows()
{
if (!_pSession) fail ("Test not available.");
Expand Down
6 changes: 5 additions & 1 deletion Data/ODBC/testsuite/src/ODBCTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "SQLExecutor.h"


#define POCO_ODBC_TEST_DATABASE_SERVER "localhost"
#define POCO_ODBC_TEST_DATABASE_SERVER "10.211.55.5"//"localhost"


class ODBCTest: public CppUnit::TestCase
Expand All @@ -47,6 +47,10 @@ class ODBCTest: public CppUnit::TestCase

virtual void testBareboneODBC() = 0;

virtual void testConnection();
virtual void testSession();
virtual void testSessionPool();

virtual void testZeroRows();
virtual void testSimpleAccess();
virtual void testComplexType();
Expand Down
Loading

0 comments on commit 379008a

Please sign in to comment.