summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-11-19 21:32:03 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-11-19 21:32:03 +0000
commit27512e1fe271ec8efdb120a25be1a0333c727751 (patch)
tree6409e84ef5fbe8cc844858ff20d056dc4cacf16f
parentExpose the mocked database instance name (diff)
downloadlibdbpp-27512e1fe271ec8efdb120a25be1a0333c727751.tar.bz2
libdbpp-27512e1fe271ec8efdb120a25be1a0333c727751.tar.xz
libdbpp-27512e1fe271ec8efdb120a25be1a0333c727751.zip
Add a database connection pool.libdbpp-0.10
-rw-r--r--libdbpp/Jamfile.jam2
-rw-r--r--libdbpp/connectionPool.cpp27
-rw-r--r--libdbpp/connectionPool.h32
-rw-r--r--libdbpp/unittests/Jamfile.jam13
-rw-r--r--libdbpp/unittests/testConnectionPool.cpp41
5 files changed, 115 insertions, 0 deletions
diff --git a/libdbpp/Jamfile.jam b/libdbpp/Jamfile.jam
index 1e09e94..ff52020 100644
--- a/libdbpp/Jamfile.jam
+++ b/libdbpp/Jamfile.jam
@@ -8,6 +8,7 @@ alias glibmm : : : :
lib boost_date_time : : <name>boost_date_time ;
lib boost_filesystem ;
lib boost_system ;
+lib boost_thread ;
lib adhocutil : : : : <include>/usr/include/adhocutil ;
lib boost_utf : : <name>boost_unit_test_framework ;
@@ -16,6 +17,7 @@ lib dbppcore :
<library>glibmm
<library>adhocutil
<library>boost_system
+ <library>boost_thread
<library>boost_filesystem
<cflags>-fvisibility=hidden
<variant>release:<cflags>-flto
diff --git a/libdbpp/connectionPool.cpp b/libdbpp/connectionPool.cpp
new file mode 100644
index 0000000..24a78fa
--- /dev/null
+++ b/libdbpp/connectionPool.cpp
@@ -0,0 +1,27 @@
+#include "connectionPool.h"
+#include <resourcePool.impl.h>
+
+template class AdHoc::ResourcePool<DB::Connection>;
+template class AdHoc::ResourceHandle<DB::Connection>;
+
+namespace DB {
+ ConnectionPool::ConnectionPool(unsigned int m, unsigned int k, const std::string & t, const std::string & cs) :
+ ResourcePool<Connection>(m, k),
+ factory(ConnectionFactory::get(t)),
+ connectionString(cs)
+ {
+ }
+
+ Connection *
+ ConnectionPool::createResource() const
+ {
+ return factory->create(connectionString);
+ }
+
+ void
+ ConnectionPool::testResource(const Connection * c) const
+ {
+ c->ping();
+ }
+}
+
diff --git a/libdbpp/connectionPool.h b/libdbpp/connectionPool.h
new file mode 100644
index 0000000..c2ce950
--- /dev/null
+++ b/libdbpp/connectionPool.h
@@ -0,0 +1,32 @@
+#ifndef DB_CONNECTIONPOOL_H
+#define DB_CONNECTIONPOOL_H
+
+#include <resourcePool.h>
+#include <visibility.h>
+#include "connection.h"
+
+namespace DB {
+ /// Specialisation of AdHoc::ResourcePool for database connections.
+ class DLL_PUBLIC ConnectionPool : public AdHoc::ResourcePool<Connection> {
+ public:
+ /// Create a new connection pool.
+ /// @param max Maximum number of concurrent database connections.
+ /// @param keep Number of connections to keep open after use.
+ /// @param type Database connection factory name.
+ /// @param connectionString Connection string to pass to the connection factory.
+ ConnectionPool(unsigned int max, unsigned int keep, const std::string & type, const std::string & connectionString);
+
+ protected:
+ /// Create a new connection.
+ Connection * createResource() const override;
+ /// Ping a connection.
+ void testResource(const Connection *) const override;
+
+ private:
+ const ConnectionFactory * factory;
+ const std::string connectionString;
+ };
+}
+
+#endif
+
diff --git a/libdbpp/unittests/Jamfile.jam b/libdbpp/unittests/Jamfile.jam
index b7b2170..aa6c195 100644
--- a/libdbpp/unittests/Jamfile.jam
+++ b/libdbpp/unittests/Jamfile.jam
@@ -24,6 +24,19 @@ run
;
run
+ testConnectionPool.cpp
+ : : :
+ <define>ROOT=\"$(me)\"
+ <define>BOOST_TEST_DYN_LINK
+ <library>..//dbppcore
+ <library>..//adhocutil
+ <library>../../libpqpp//dbpp-postgresql
+ <library>boost_utf
+ :
+ testConnectionPool
+ ;
+
+run
testUtils.cpp
: : :
<define>ROOT=\"$(me)\"
diff --git a/libdbpp/unittests/testConnectionPool.cpp b/libdbpp/unittests/testConnectionPool.cpp
new file mode 100644
index 0000000..ebe22d1
--- /dev/null
+++ b/libdbpp/unittests/testConnectionPool.cpp
@@ -0,0 +1,41 @@
+#define BOOST_TEST_MODULE DbConnectionPool
+#include <boost/test/unit_test.hpp>
+
+#include <connectionPool.h>
+#include <mock.h>
+#include <buffer.h>
+
+class MockPool : public PQ::Mock, public DB::ConnectionPool {
+ public:
+ MockPool() :
+ PQ::Mock("user=postgres dbname=postgres", "pqmock", { }),
+ DB::ConnectionPool(4, 2, "postgresql", stringbf("user=postgres dbname=%s", databaseName()))
+ {
+ }
+};
+
+BOOST_AUTO_TEST_CASE( basic )
+{
+ MockPool pool;
+ DB::Connection * cr;
+ {
+ auto c = pool.get();
+ c->beginTx();
+ c->commitTx();
+ cr = c.get();
+ }
+ {
+ auto c = pool.get();
+ BOOST_REQUIRE_EQUAL(cr, c.get());
+ }
+ {
+ auto c1 = pool.get();
+ auto c2 = pool.get();
+ auto c3 = pool.get();
+ auto c4 = pool.get();
+ BOOST_REQUIRE_EQUAL(4, pool.inUseCount());
+ }
+ BOOST_REQUIRE_EQUAL(0, pool.inUseCount());
+ BOOST_REQUIRE_EQUAL(2, pool.availableCount());
+}
+