aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-08-28 16:53:54 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-08-28 16:53:54 +0200
commit19109c06566ecc1b2a42820145c784d12ad26b27 (patch)
treeda45e2f220f026562dc44adcb0579167ded964f7
parent1c2289c99f86a9f8cccfa87aef981e7c39dd4e9e (diff)
Add create() hook to connection factories
This will allow the user to either establish the connection themselves (using the handle c-tor) and/or configure the connection post-creation.
-rw-r--r--odb/mysql/connection-factory.cxx36
-rw-r--r--odb/mysql/connection-factory.hxx24
2 files changed, 40 insertions, 20 deletions
diff --git a/odb/mysql/connection-factory.cxx b/odb/mysql/connection-factory.cxx
index 2da8235..f6f8ab5 100644
--- a/odb/mysql/connection-factory.cxx
+++ b/odb/mysql/connection-factory.cxx
@@ -149,6 +149,12 @@ namespace odb
// connection_pool_factory
//
+ connection_pool_factory::pooled_connection_ptr connection_pool_factory::
+ create ()
+ {
+ return pooled_connection_ptr (new (shared) pooled_connection (*db_));
+ }
+
connection_pool_factory::
~connection_pool_factory ()
{
@@ -174,7 +180,7 @@ namespace odb
//
while (true)
{
- shared_ptr<pooled_connection> c;
+ pooled_connection_ptr c;
lock l (mutex_);
@@ -201,8 +207,8 @@ namespace odb
// For new connections we don't need to ping so we
// can return immediately.
//
- shared_ptr<pooled_connection> c (
- new (shared) pooled_connection (*db_, this));
+ c = create ();
+ c->pool_ = this;
in_use_++;
return c;
}
@@ -220,7 +226,7 @@ namespace odb
return c;
}
- return shared_ptr<pooled_connection> (); // Never reached.
+ return pooled_connection_ptr (); // Never reached.
}
void connection_pool_factory::
@@ -235,11 +241,7 @@ namespace odb
connections_.reserve (min_);
for(size_t i (0); i < min_; ++i)
- {
- connections_.push_back (
- shared_ptr<pooled_connection> (
- new (shared) pooled_connection (*db_, 0)));
- }
+ connections_.push_back (create ());
}
}
@@ -261,8 +263,7 @@ namespace odb
in_use_--;
if (keep)
- connections_.push_back (
- shared_ptr<pooled_connection> (inc_ref (c)));
+ connections_.push_back (pooled_connection_ptr (inc_ref (c)));
if (waiters_ != 0)
cond_.signal ();
@@ -275,8 +276,17 @@ namespace odb
//
connection_pool_factory::pooled_connection::
- pooled_connection (database_type& db, connection_pool_factory* pool)
- : connection (db), pool_ (pool)
+ pooled_connection (database_type& db)
+ : connection (db), pool_ (0)
+ {
+ callback_.arg = this;
+ callback_.zero_counter = &zero_counter;
+ shared_base::callback_ = &callback_;
+ }
+
+ connection_pool_factory::pooled_connection::
+ pooled_connection (database_type& db, MYSQL* handle)
+ : connection (db, handle), pool_ (0)
{
callback_.arg = this;
callback_.zero_counter = &zero_counter;
diff --git a/odb/mysql/connection-factory.hxx b/odb/mysql/connection-factory.hxx
index 399fdae..927c701 100644
--- a/odb/mysql/connection-factory.hxx
+++ b/odb/mysql/connection-factory.hxx
@@ -110,13 +110,12 @@ namespace odb
connection_pool_factory (const connection_pool_factory&);
connection_pool_factory& operator= (const connection_pool_factory&);
- private:
+ protected:
class pooled_connection: public connection
{
public:
- // NULL pool value indicates that the connection is not in use.
- //
- pooled_connection (database_type&, connection_pool_factory*);
+ pooled_connection (database_type&);
+ pooled_connection (database_type&, MYSQL*);
private:
static bool
@@ -126,19 +125,30 @@ namespace odb
friend class connection_pool_factory;
shared_base::refcount_callback callback_;
+
+ // NULL pool value indicates that the connection is not in use.
+ //
connection_pool_factory* pool_;
};
friend class pooled_connection;
- typedef std::vector<details::shared_ptr<pooled_connection> > connections;
- private:
+ typedef details::shared_ptr<pooled_connection> pooled_connection_ptr;
+ typedef std::vector<pooled_connection_ptr> connections;
+
+ // This function is called whenever the pool needs to create a new
+ // connection.
+ //
+ virtual pooled_connection_ptr
+ create ();
+
+ protected:
// Return true if the connection should be deleted, false otherwise.
//
bool
release (pooled_connection*);
- private:
+ protected:
const std::size_t max_;
const std::size_t min_;
const bool ping_;