From fb43a4faf5492453c751bbd2d2319c9ca8614a11 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 28 Sep 2013 10:51:12 +0200 Subject: Make schema version access (but not modification) thread-safe Also cache the version in statements so that we don't have to lock the mutex (slow) every time we need to check the version. --- odb/mssql/section-statements.hxx | 13 +++++++++++++ odb/mssql/section-statements.txx | 1 + odb/mssql/statements-base.hxx | 24 +++++++++++++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/odb/mssql/section-statements.hxx b/odb/mssql/section-statements.hxx index d6ac777..c592dd9 100644 --- a/odb/mssql/section-statements.hxx +++ b/odb/mssql/section-statements.hxx @@ -10,12 +10,15 @@ #include // std::size_t #include +#include #include #include #include #include #include +#include +#include #include namespace odb @@ -46,6 +49,15 @@ namespace odb connection_type& connection () {return conn_;} + const schema_version_migration& + version_migration (const char* name = "") const + { + if (svm_ == 0) + svm_ = &conn_.database ().schema_version_migration (name); + + return *svm_; + } + image_type& image () {return image_;} @@ -138,6 +150,7 @@ namespace odb protected: connection_type& conn_; + mutable const schema_version_migration* svm_; // These come from object_statements. // diff --git a/odb/mssql/section-statements.txx b/odb/mssql/section-statements.txx index fcb6874..7036270 100644 --- a/odb/mssql/section-statements.txx +++ b/odb/mssql/section-statements.txx @@ -14,6 +14,7 @@ namespace odb image_type& im, binding& id, binding& idv) : conn_ (conn), + svm_ (0), image_ (im), id_binding_ (id), idv_binding_ (idv), diff --git a/odb/mssql/statements-base.hxx b/odb/mssql/statements-base.hxx index e307b13..2669e89 100644 --- a/odb/mssql/statements-base.hxx +++ b/odb/mssql/statements-base.hxx @@ -7,10 +7,12 @@ #include +#include #include #include -#include // connection +#include +#include #include @@ -29,18 +31,30 @@ namespace odb return conn_; } + // Schema version. database::schema_version_migration() is thread- + // safe which means it is also slow. Cache the result in statements + // so we can avoid the mutex lock. This is thread-safe since if the + // version is updated, then the statements cache will be expired. + // + const schema_version_migration& + version_migration (const char* name = "") const + { + if (svm_ == 0) + svm_ = &conn_.database ().schema_version_migration (name); + + return *svm_; + } + public: virtual ~statements_base (); protected: - statements_base (connection_type& conn) - : conn_ (conn) - { - } + statements_base (connection_type& conn): conn_ (conn), svm_ (0) {} protected: connection_type& conn_; + mutable const schema_version_migration* svm_; }; } } -- cgit v1.1