aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-08-10 13:57:24 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-08-10 13:57:24 +0200
commitb49d3e5e0e58e69b4fde856e37f73d101b5b2c0a (patch)
tree92c587949496e21028071ab7ca2202bc5b67f6bb
parent0a589394a09ce5b3f16d902d657710a2886cc2fc (diff)
Delay closing statement until there are no active statements
-rw-r--r--odb/mysql/connection.cxx39
-rw-r--r--odb/mysql/connection.hxx19
-rw-r--r--odb/mysql/result.txx1
-rw-r--r--odb/mysql/statement.cxx9
4 files changed, 59 insertions, 9 deletions
diff --git a/odb/mysql/connection.cxx b/odb/mysql/connection.cxx
index 7eb1b34..c6ca5a3 100644
--- a/odb/mysql/connection.cxx
+++ b/odb/mysql/connection.cxx
@@ -9,6 +9,8 @@
#include <odb/mysql/connection.hxx>
#include <odb/mysql/exceptions.hxx>
+using namespace std;
+
namespace odb
{
namespace mysql
@@ -18,7 +20,7 @@ namespace odb
: handle_ (&mysql_), active_ (0), statement_cache_ (*this)
{
if (mysql_init (handle_) == 0)
- throw std::bad_alloc ();
+ throw bad_alloc ();
// Force the CLIENT_FOUND_ROWS flag so that UPDATE returns the
// number of found rows, not the number of changed rows. This
@@ -43,7 +45,42 @@ namespace odb
connection::
~connection ()
{
+ if (stmt_handles_.size () > 0)
+ free_stmt_handles ();
+
mysql_close (handle_);
}
+
+ MYSQL_STMT* connection::
+ alloc_stmt_handle ()
+ {
+ MYSQL_STMT* stmt (mysql_stmt_init (handle_));
+
+ if (stmt == 0)
+ throw bad_alloc ();
+
+ return stmt;
+ }
+
+ void connection::
+ free_stmt_handle (MYSQL_STMT* stmt)
+ {
+ if (active_ == 0)
+ mysql_stmt_close (stmt);
+ else
+ stmt_handles_.push_back (stmt);
+ }
+
+ void connection::
+ free_stmt_handles ()
+ {
+ for (stmt_handles::iterator i (stmt_handles_.begin ()),
+ e (stmt_handles_.end ()); i != e; ++i)
+ {
+ mysql_stmt_close (*i);
+ }
+
+ stmt_handles_.clear ();
+ }
}
}
diff --git a/odb/mysql/connection.hxx b/odb/mysql/connection.hxx
index 513abc5..8509790 100644
--- a/odb/mysql/connection.hxx
+++ b/odb/mysql/connection.hxx
@@ -8,6 +8,8 @@
#include <mysql/mysql.h>
+#include <vector>
+
#include <odb/forward.hxx>
#include <odb/shared-ptr.hxx>
@@ -52,17 +54,34 @@ namespace odb
active (statement* s)
{
active_ = s;
+
+ if (s == 0 && stmt_handles_.size () > 0)
+ free_stmt_handles ();
}
+ public:
+ MYSQL_STMT*
+ alloc_stmt_handle ();
+
+ void
+ free_stmt_handle (MYSQL_STMT*);
+
private:
connection (const connection&);
connection& operator= (const connection&);
private:
+ void
+ free_stmt_handles ();
+
+ private:
MYSQL mysql_;
MYSQL* handle_;
statement* active_;
statement_cache_type statement_cache_;
+
+ typedef std::vector<MYSQL_STMT*> stmt_handles;
+ stmt_handles stmt_handles_;
};
}
}
diff --git a/odb/mysql/result.txx b/odb/mysql/result.txx
index b27b22e..b85e2e0 100644
--- a/odb/mysql/result.txx
+++ b/odb/mysql/result.txx
@@ -11,7 +11,6 @@ namespace odb
result_impl<T>::
~result_impl ()
{
- statement_->free_result ();
}
template <typename T>
diff --git a/odb/mysql/statement.cxx b/odb/mysql/statement.cxx
index 981c756..009e3f1 100644
--- a/odb/mysql/statement.cxx
+++ b/odb/mysql/statement.cxx
@@ -5,8 +5,6 @@
#include <mysql/mysqld_error.h> // ER_DUP_ENTRY
-#include <new> // std::bad_alloc
-
#include <odb/mysql/statement.hxx>
#include <odb/mysql/connection.hxx>
#include <odb/mysql/exceptions.hxx>
@@ -22,18 +20,15 @@ namespace odb
statement::
statement (connection& conn)
- : conn_ (conn)
+ : conn_ (conn), stmt_ (conn_.alloc_stmt_handle ())
{
- stmt_ = mysql_stmt_init (conn_.handle ());
- if (stmt_ == 0)
- throw bad_alloc ();
}
statement::
~statement ()
{
- mysql_stmt_close (stmt_);
+ conn_.free_stmt_handle (stmt_);
}
void statement::