From b49d3e5e0e58e69b4fde856e37f73d101b5b2c0a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 10 Aug 2010 13:57:24 +0200 Subject: Delay closing statement until there are no active statements --- odb/mysql/connection.cxx | 39 ++++++++++++++++++++++++++++++++++++++- odb/mysql/connection.hxx | 19 +++++++++++++++++++ odb/mysql/result.txx | 1 - odb/mysql/statement.cxx | 9 ++------- 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 #include +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 +#include + #include #include @@ -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 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:: ~result_impl () { - statement_->free_result (); } template 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 // ER_DUP_ENTRY -#include // std::bad_alloc - #include #include #include @@ -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:: -- cgit v1.1