diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2014-11-26 15:04:56 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2014-11-26 15:04:56 +0200 |
commit | 59e3ebcafb1dc4d54484143fc926ef8377bdb154 (patch) | |
tree | e832c9eff072039b19d8bf67e74d1796b03ff860 /common/bulk/driver.cxx | |
parent | 3d022058bc8b61c14c2d16e760ea1393d775c0d0 (diff) |
Implement optimistic concurrency support in bulk operations
Bulk update and SQL Server ROWVERSION not yet supported.
Diffstat (limited to 'common/bulk/driver.cxx')
-rw-r--r-- | common/bulk/driver.cxx | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/common/bulk/driver.cxx b/common/bulk/driver.cxx index 2903ae4..7d0b726 100644 --- a/common/bulk/driver.cxx +++ b/common/bulk/driver.cxx @@ -1015,6 +1015,196 @@ main (int argc, char* argv[]) } } + // Test optimistic concurrency. + // + { + using namespace test7; + + std::vector<object> v (fill<object> (4)); + + // persist + // + { + transaction t (db->begin ()); + db->persist (v.begin (), v.end ()); + t.commit (); + + assert (v[0].v != 0 && + v[1].v != 0 && + v[2].v != 0 && + v[3].v != 0); + } + + // update + // + { + std::vector<object> c (v); + + transaction t (db->begin ()); + db->update (v.begin (), v.end ()); + t.commit (); + + assert (v[0].v > c[0].v && + v[1].v > c[1].v && + v[2].v > c[2].v && + v[3].v > c[3].v); + } + + { + object o2 (v[1]); + object o4 (v[3]); + + o2.n++; + o4.n++; + + transaction t (db->begin ()); + db->update (o2); + db->update (o4); + t.commit (); + } + + try + { + transaction t (db->begin ()); + db->update (v.begin (), v.end ()); + assert (false); + } + catch (const multiple_exceptions& e) + { + cout << e.what () << endl << endl; + } + + // erase + // + try + { + transaction t (db->begin ()); + db->erase (v.begin (), v.end ()); + assert (false); + } + catch (const multiple_exceptions& e) + { + cout << e.what () << endl << endl; + } + + { + transaction t (db->begin ()); + db->reload (v[1]); + db->reload (v[3]); + t.commit (); + } + + { + transaction t (db->begin ()); + db->erase (v.begin (), v.end ()); + t.commit (); + } + } + + // Test SQL Server optimistic concurrency with ROWVERSION. + // +#ifdef DATABASE_MSSQL + { + using namespace test8; + + std::vector<object> v (fill<object> (4)); + + v[0].id = 1; + v[2].id = 2; + v[3].id = 3; + v[4].id = 4; + + + // persist + // + { + transaction t (db->begin ()); + db->persist (v.begin (), v.end ()); + t.commit (); + + assert (v[0].v != 0 && + v[1].v != 0 && + v[2].v != 0 && + v[3].v != 0); + + //cerr << v[0].v << endl + // << v[0].v << endl + // << v[0].v << endl + // << v[0].v << endl; + } + + // update + // + + /* + { + std::vector<object> c (v); + + transaction t (db->begin ()); + db->update (v.begin (), v.end ()); + t.commit (); + + assert (v[0].v > c[0].v && + v[1].v > c[1].v && + v[2].v > c[2].v && + v[3].v > c[3].v); + } + */ + + { + object o2 (v[1]); + object o4 (v[3]); + + o2.n++; + o4.n++; + + transaction t (db->begin ()); + db->update (o2); + db->update (o4); + t.commit (); + } + + /* + try + { + transaction t (db->begin ()); + db->update (v.begin (), v.end ()); + assert (false); + } + catch (const multiple_exceptions& e) + { + cout << e.what () << endl << endl; + } + */ + + // erase + // + try + { + transaction t (db->begin ()); + db->erase (v.begin (), v.end ()); + assert (false); + } + catch (const multiple_exceptions& e) + { + assert (e.attempted () == 4 && e.failed () == 4); + } + + { + transaction t (db->begin ()); + db->reload (v[1]); + db->reload (v[3]); + t.commit (); + } + + { + transaction t (db->begin ()); + db->erase (v.begin (), v.end ()); + t.commit (); + } + } +#endif + #endif } catch (const odb::exception& e) |