diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2013-01-18 10:23:39 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2013-01-18 10:23:39 +0200 |
commit | d304a4b2b19dca115954f209d6a37a6958e73ab9 (patch) | |
tree | 0822a40a7d44e7921076b6ad458d29e3ad91c525 /common/session/custom/driver.cxx | |
parent | 856a438b33184959b64864f1afdf5a6a2fd6b0d2 (diff) |
Add support for post-commit/rollback callbacks
New test: common/transaction/callback.
Diffstat (limited to 'common/session/custom/driver.cxx')
-rw-r--r-- | common/session/custom/driver.cxx | 201 |
1 files changed, 119 insertions, 82 deletions
diff --git a/common/session/custom/driver.cxx b/common/session/custom/driver.cxx index 0a58ea5..1b00ada 100644 --- a/common/session/custom/driver.cxx +++ b/common/session/custom/driver.cxx @@ -30,16 +30,14 @@ using odb::transaction; struct counting_tracer: odb::tracer { virtual void - execute (odb::connection&, const char*) - { - count++; - } - + execute (odb::connection&, const char*) {count++;} size_t count; }; static counting_tracer tracer; +struct failed {}; + int main (int argc, char* argv[]) { @@ -81,99 +79,138 @@ main (int argc, char* argv[]) t.commit (); } - session s; - shared_ptr<employer> st, cs; - shared_ptr<employee> ste, cse; - { - transaction t (db->begin ()); + session s; + shared_ptr<employer> st, cs; + shared_ptr<employee> ste, cse; - st = db->load<employer> ("Simple Tech Ltd"); - ste = db->load<employee> (st->employees ()[0].object_id ()); + { + transaction t (db->begin ()); - // Test object cache. - // - shared_ptr<employee> e (st->employees ()[0].load ()); - assert (ste->employer () == st); - assert (ste == e); + st = db->load<employer> ("Simple Tech Ltd"); + ste = db->load<employee> (st->employees ()[0].object_id ()); - t.commit (); - } + // Test object cache. + // + shared_ptr<employee> e (st->employees ()[0].load ()); + assert (ste->employer () == st); + assert (ste == e); - { - transaction t (db->begin ()); + t.commit (); + } - cs = db->load<employer> ("Complex Systems Inc"); - cse = db->load<employee> (cs->employees ()[0].object_id ()); - cs->employees ()[0].load (); + { + transaction t (db->begin ()); - t.commit (); - } + cs = db->load<employer> ("Complex Systems Inc"); + cse = db->load<employee> (cs->employees ()[0].object_id ()); + cs->employees ()[0].load (); - cs->symbol ("CSI"); - - // Swap employees. - // - ste->employer (cs); - cse->employer (st); - st->employees ()[0] = cse; - cs->employees ()[0] = ste; + t.commit (); + } - { - transaction t (db->begin ()); - tracer.count = 0; - t.tracer (tracer); - s.flush (*db); // Flush all the changes. - assert (tracer.count == 3); - t.commit (); - s.mark (); // Mark all the changed objects as unchanged. - } + cs->symbol ("CSI"); - { - transaction t (db->begin ()); - tracer.count = 0; - t.tracer (tracer); - s.flush (*db); - assert (tracer.count == 0); - t.commit (); - } - - cs->symbol ("COMP"); - st->symbol ("SMPL"); - - { - transaction t (db->begin ()); - tracer.count = 0; - t.tracer (tracer); - s.flush (*db); - assert (tracer.count == 2); - t.commit (); - s.mark (); - } - - { - transaction t (db->begin ()); - tracer.count = 0; - t.tracer (tracer); - s.flush (*db); - assert (tracer.count == 0); - t.commit (); + // Swap employees. + // + ste->employer (cs); + cse->employer (st); + st->employees ()[0] = cse; + cs->employees ()[0] = ste; + + { + transaction t (db->begin ()); + tracer.count = 0; + t.tracer (tracer); + s.flush (*db); + assert (tracer.count == 3); + t.commit (); + } + + { + transaction t (db->begin ()); + tracer.count = 0; + t.tracer (tracer); + s.flush (*db); + assert (tracer.count == 0); + t.commit (); + } + + cs->symbol ("COMP"); + st->symbol ("SMPL"); + + { + transaction t (db->begin ()); + tracer.count = 0; + t.tracer (tracer); + s.flush (*db); + assert (tracer.count == 2); + t.commit (); + } + + { + transaction t (db->begin ()); + tracer.count = 0; + t.tracer (tracer); + s.flush (*db); + assert (tracer.count == 0); + t.commit (); + } + + // Explicit update. + // + cs->symbol ("CS"); + st->symbol ("ST"); + + { + transaction t (db->begin ()); + db->update (cs); + tracer.count = 0; + t.tracer (tracer); + s.flush (*db); + assert (tracer.count == 1); + t.commit (); + } + + // Rollback after update. + // + cs->symbol ("CSI"); + + try + { + transaction t (db->begin ()); + tracer.count = 0; + t.tracer (tracer); + s.flush (*db); + assert (tracer.count == 1); + throw failed (); + t.commit (); + } + catch (const failed&) + { + transaction t (db->begin ()); + tracer.count = 0; + t.tracer (tracer); + s.flush (*db); + assert (tracer.count == 1); + t.commit (); + } } - // Explicit update. + // Test session destruction before transaction is commited. // - cs->symbol ("CS"); - st->symbol ("ST"); - { transaction t (db->begin ()); - db->update (cs); - tracer.count = 0; - t.tracer (tracer); - s.flush (*db); - assert (tracer.count == 1); + { + session s; + shared_ptr<employer> st (db->load<employer> ("Simple Tech Ltd")); + st->symbol ("STL"); + tracer.count = 0; + t.tracer (tracer); + s.flush (*db); + assert (tracer.count == 1); + } t.commit (); - s.mark (); } } catch (const odb::exception& e) |