Mercurial > hg > orthanc-postgresql
changeset 10:401684ea8d73
Use of advisory locks
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 27 Feb 2015 16:56:00 +0100 |
parents | c074a7321bad |
children | 1efb30985931 |
files | Core/GlobalProperties.cpp Core/PostgreSQLResult.cpp Core/PostgreSQLResult.h NEWS |
diffstat | 4 files changed, 39 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/Core/GlobalProperties.cpp Mon Feb 16 09:46:54 2015 +0100 +++ b/Core/GlobalProperties.cpp Fri Feb 27 16:56:00 2015 +0100 @@ -25,6 +25,8 @@ #include "PostgreSQLResult.h" #include "PostgreSQLTransaction.h" +#define USE_ADVISORY_LOCK 1 + namespace OrthancPlugins { GlobalProperties::GlobalProperties(PostgreSQLConnection& connection, @@ -53,6 +55,20 @@ { PostgreSQLTransaction transaction(connection_); +#if USE_ADVISORY_LOCK == 1 + PostgreSQLStatement s(connection_, "select pg_try_advisory_lock($1);"); + s.DeclareInputInteger(0); + s.BindInteger(0, lockKey_); + + PostgreSQLResult result(s); + if (result.IsDone() || + !result.GetBoolean(0)) + { + throw PostgreSQLException("The database is locked by another instance of Orthanc."); + } +#else + PostgreSQLTransaction transaction(connection_); + // Check the lock if (!allowUnlock) { @@ -67,6 +83,7 @@ // Lock the database SetGlobalProperty(lockKey_, "1"); +#endif transaction.Commit(); } @@ -130,10 +147,14 @@ { if (useLock_) { +#if USE_ADVISORY_LOCK == 1 + // Nothing to do, the lock is released after the connection is closed +#else // Remove the lock PostgreSQLTransaction transaction(connection_); SetGlobalProperty(lockKey_, "0"); transaction.Commit(); +#endif } } }
--- a/Core/PostgreSQLResult.cpp Mon Feb 16 09:46:54 2015 +0100 +++ b/Core/PostgreSQLResult.cpp Fri Feb 27 16:56:00 2015 +0100 @@ -98,6 +98,14 @@ return PQgetisnull(reinterpret_cast<PGresult*>(result_), position_, column) != 0; } + bool PostgreSQLResult::GetBoolean(unsigned int column) const + { + CheckColumn(column, BOOLOID); + assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 1); + const uint8_t *v = reinterpret_cast<const uint8_t*>(PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column)); + return (v[0] != 0); + } + int PostgreSQLResult::GetInteger(unsigned int column) const { CheckColumn(column, INT4OID);
--- a/Core/PostgreSQLResult.h Mon Feb 16 09:46:54 2015 +0100 +++ b/Core/PostgreSQLResult.h Fri Feb 27 16:56:00 2015 +0100 @@ -54,6 +54,8 @@ bool IsNull(unsigned int column) const; + bool GetBoolean(unsigned int column) const; + int GetInteger(unsigned int column) const; int64_t GetInteger64(unsigned int column) const;