diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-11-02 14:29:23 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-11-02 14:29:23 +0200 |
commit | 1dab2da7c969e328281765d59d2fe90d618fadc6 (patch) | |
tree | 5271af031172c7751ab09e1adc5fb731c6c846d7 /optimistic | |
parent | 9b10c11da5fdc5e64187d390b33ab62f6d2c49c0 (diff) |
Add example for optimistic concurrency support
Diffstat (limited to 'optimistic')
-rw-r--r-- | optimistic/README | 56 | ||||
-rw-r--r-- | optimistic/database.hxx | 81 | ||||
-rw-r--r-- | optimistic/driver.cxx | 159 | ||||
-rw-r--r-- | optimistic/makefile | 114 | ||||
-rw-r--r-- | optimistic/person.hxx | 68 |
5 files changed, 478 insertions, 0 deletions
diff --git a/optimistic/README b/optimistic/README new file mode 100644 index 0000000..4c38e66 --- /dev/null +++ b/optimistic/README @@ -0,0 +1,56 @@ +This example shows how to use optimistic concurrency in ODB. + +The example consists of the following files: + +person.hxx + Header file defining the 'person' persistent class. Besides the standard + persistent class pragmas, this definition also uses the 'optimistic' + pragma to indicate to the ODB compiler that the class must support + optimistic concurrency. It also uses the 'version' pragma to specify + which data member will contain the object version. + +person-odb.hxx +person-odb.ixx +person-odb.cxx +person.sql + The first three files contain the database support code and the last file + contains the database schema for the person.hxx header. + + These files are generated by the ODB compiler from person.hxx using the + following command line: + + odb -d <database> --generate-schema person.hxx + + Where <database> stands for the database system we are using, for example, + 'mysql'. + +database.hxx + Contains the create_database() function which instantiates the concrete + database class corresponding to the database system we are using. + +driver.cxx + Driver for the example. It includes the person.hxx and person-odb.hxx + headers to gain access to the persistent classes and their database support + code. It also includes database.hxx for the create_database() function + declaration. + + In main() the driver first calls create_database() to obtain the database + instance and persists a sample 'person' object. It then emulates the + parallel execution of two processes that try to concurrently update or + delete this object. For each step the driver prints the versions of the + object as seen by each process. + +To run the example we may first need to create the database schema (for some +database systems, such as SQLite, the schema is embedded into the generated +code which makes this step unnecessary). Using MySQL as an example, this +can be achieved with the following command: + +mysql --user=odb_test --database=odb_test < person.sql + +Here we use 'odb_test' as the database login and also 'odb_test' as the +database name. + +Once the database schema is ready, we can run the example (using MySQL as +the database): + +./driver --user odb_test --database odb_test diff --git a/optimistic/database.hxx b/optimistic/database.hxx new file mode 100644 index 0000000..dc9b389 --- /dev/null +++ b/optimistic/database.hxx @@ -0,0 +1,81 @@ +// file : optimistic/database.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +// +// Create concrete database instance based on the DATABASE_* macros. +// + +#ifndef DATABASE_HXX +#define DATABASE_HXX + +#include <string> +#include <memory> // std::auto_ptr +#include <cstdlib> // std::exit +#include <iostream> + +#include <odb/database.hxx> + +#if defined(DATABASE_MYSQL) +# include <odb/mysql/database.hxx> +#elif defined(DATABASE_SQLITE) +# include <odb/connection.hxx> +# include <odb/transaction.hxx> +# include <odb/schema-catalog.hxx> +# include <odb/sqlite/database.hxx> +#elif defined(DATABASE_PGSQL) +# include <odb/pgsql/database.hxx> +#endif + +inline std::auto_ptr<odb::database> +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb::core; + + if (argc > 1 && argv[1] == string ("--help")) + { + cerr << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + odb::mysql::database::print_usage (cerr); +#elif defined(DATABASE_SQLITE) + odb::sqlite::database::print_usage (cerr); +#elif defined(DATABASE_PGSQL) + odb::pgsql::database::print_usage (cerr); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + auto_ptr<database> db (new odb::mysql::database (argc, argv)); +#elif defined(DATABASE_SQLITE) + auto_ptr<database> db ( + new odb::sqlite::database ( + argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + + // Create the database schema. Due to bugs in SQLite foreign key + // support for DDL statements, we need to temporarily disable + // foreign keys. + // + { + connection_ptr c (db->connection ()); + + c->execute ("PRAGMA foreign_keys=OFF"); + + transaction t (c->begin ()); + schema_catalog::create_schema (*db); + t.commit (); + + c->execute ("PRAGMA foreign_keys=ON"); + } +#elif defined(DATABASE_PGSQL) + auto_ptr<database> db (new odb::pgsql::database (argc, argv)); +#endif + + return db; +} + +#endif // DATABASE_HXX diff --git a/optimistic/driver.cxx b/optimistic/driver.cxx new file mode 100644 index 0000000..78cb14d --- /dev/null +++ b/optimistic/driver.cxx @@ -0,0 +1,159 @@ +// file : optimistic/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include <memory> // std::auto_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include "database.hxx" // create_database + +#include "person.hxx" +#include "person-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + // Persist the object. + // + // At this point the initial version (1) is assigned. + // + unsigned long id; + { + person p ("John", "Doe", 21); + + transaction t (db->begin ()); + id = db->persist (p); + t.commit (); + + cout << "version after persist: " << p.version () << endl; + } + + // Process 1: load the object. + // + person p1; + { + transaction t (db->begin ()); + db->load (id, p1); + t.commit (); + + cout << "process 1 version after load: " << p1.version () << endl; + } + + // Process 2: load the object. + // + person p2; + { + transaction t (db->begin ()); + db->load (id, p2); + t.commit (); + + cout << "process 2 version after load: " << p2.version () << endl; + } + + // Process 1: update the object. + // + // At this point the version is incremented and becomes 2. + // + { + p1.age (20); // Correct the wrong age. + + transaction t (db->begin ()); + db->update (p1); + t.commit (); + + cout << "process 1 version after update: " << p1.version () << endl; + } + + // Process 2: update the object. + // + // Since the object version in this process is 1 while in the database + // it is 2, this operation will fail. + // + { + p2.age (p2.age () + 1); // Increment the age. + + transaction t (db->begin ()); + + try + { + db->update (p2); + } + catch (const object_changed&) + { + cout << "process 2 version is out of date: " << p2.version () << endl; + + // Reload the object and retry the operation. Note that the second + // update call cannot throw object_changed since we reloaded the + // object and are trying to update it in a single transaction. + // + db->reload (p2); + + cout << "process 2 version after reload: " << p2.version () << endl; + + p2.age (p2.age () + 1); + db->update (p2); + } + + t.commit (); + + cout << "process 2 version after update: " << p2.version () << endl; + cout << "final age value: " << p2.age () << endl; + } + + // Process 1: delete the object if the person is younger than 21. + // + // Since the object version in this process is 2 while in the database + // it is 3, this operation will fail. Note that this will only hold + // true if we are deleting the object by passing an object instance + // to the erase() function. If instead we pass object id, then the + // object will be deleted regardless of the version. + // + if (p1.age () < 21) + { + transaction t (db->begin ()); + + try + { + db->erase (p1); + // db->erase (id); // Never throws object_changed. + } + catch (const object_changed&) + { + cout << "process 1 version is out of date: " << p1.version () << endl; + + // Reload the object and retry the operation. Similar to update, the + // second erase call cannot throw object_changed since we reloaded + // the object and are trying to erase it in a single transaction. + // + db->reload (p1); + + cout << "process 1 version after reload: " << p2.version () << endl; + + if (p1.age () < 21) + { + db->erase (p1); + cout << "object deleted" << endl; + } + else + cout << "object not deleted" << endl; + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/optimistic/makefile b/optimistic/makefile new file mode 100644 index 0000000..a073531 --- /dev/null +++ b/optimistic/makefile @@ -0,0 +1,114 @@ +# file : optimistic/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := person.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +$(call import,\ + $(scf_root)/import/libodb/stub.make,\ + l: odb.l,cpp-options: odb.l.cpp-options) + +ifdef db_id +$(call import,\ + $(scf_root)/import/libodb-$(db_id)/stub.make,\ + l: odb_db.l,cpp-options: odb_db.l.cpp-options) +endif + +ifeq ($(odb_db.l.cpp-options),) +odb_db.l.cpp-options := $(out_base)/.unbuildable +endif + +# Build. +# +$(driver): $(cxx_obj) $(odb_db.l) $(odb.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) -D$(db_macro) +$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ +--table-prefix optimistic_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(odb.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export name := $(name) +$(dist): export odb_header_stem := $(basename $(odb_hdr)) +$(dist): export extra_dist := README $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) README database.hxx) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): header := $(odb_hdr) +$(test): $(driver) + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(odb_rules)) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + diff --git a/optimistic/person.hxx b/optimistic/person.hxx new file mode 100644 index 0000000..60f3082 --- /dev/null +++ b/optimistic/person.hxx @@ -0,0 +1,68 @@ +// file : optimistic/person.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#ifndef PERSON_HXX +#define PERSON_HXX + +#include <string> + +#include <odb/core.hxx> + +#pragma db object optimistic +class person +{ +public: + person () {} + person (const std::string& first, + const std::string& last, + unsigned short age) + : first_ (first), last_ (last), age_ (age) + { + } + + const std::string& + first () const + { + return first_; + } + + const std::string& + last () const + { + return last_; + } + + unsigned short + age () const + { + return age_; + } + + void + age (unsigned short age) + { + age_ = age; + } + + unsigned long + version () const + { + return version_; + } + +private: + friend class odb::access; + + #pragma db id auto + unsigned long id_; + + #pragma db version + unsigned long version_; + + std::string first_; + std::string last_; + unsigned short age_; +}; + +#endif // PERSON_HXX |