aboutsummaryrefslogtreecommitdiff
path: root/libbrep/database-lock.hxx
blob: ab5441dc47c12246f3fc099252240ac4bed393de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// file      : libbrep/database-lock.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef LIBBREP_DATABASE_LOCK_HXX
#define LIBBREP_DATABASE_LOCK_HXX

#include <odb/pgsql/forward.hxx>    // database, transaction
#include <odb/pgsql/connection.hxx>

#include <libbrep/types.hxx>
#include <libbrep/utility.hxx>

namespace brep
{
  struct database_locked: std::exception
  {
    virtual char const*
    what () const throw () {return "database locked";}
  };

  // Try to "lock" the PostgreSQL database in the constructor and release the
  // lock in the destructor. Throw database_locked if the database is already
  // locked by someone else. May also throw odb::pgsql::database_exception.
  //
  // This mechanism is used by the brep loader and schema migration tool to
  // make sure they don't step on each others toes.
  //
  // Note: movable but not copyable.
  //
  class database_lock
  {
  public:
    explicit
    database_lock (odb::pgsql::database&);

  private:
    odb::pgsql::connection_ptr connection_;
    unique_ptr<odb::pgsql::transaction> transaction_;
  };
}

#endif // LIBBREP_DATABASE_LOCK_HXX