Author: wyoung
Date: Thu Oct 25 20:17:10 2007
New Revision: 1791

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1791&view=rev
Log:
- Added examples/deadlock.cpp
- Added -m command line flag to examples, needed by deadlock
- resetdb creates two new tables for deadlock example to use

Added:
    trunk/examples/deadlock.cpp   (with props)
Modified:
    trunk/   (props changed)
    trunk/CREDITS
    trunk/examples/cmdline.cpp
    trunk/examples/resetdb.cpp
    trunk/mysql++.bkl

Propchange: trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Thu Oct 25 20:17:10 2007
@@ -39,6 +39,7 @@
 complic1
 custom[0-9]
 dbinfo
+deadlock
 fieldinf
 for_each
 load_jpeg

Modified: trunk/CREDITS
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/CREDITS?rev=1791&r1=1790&r2=1791&view=diff
==============================================================================
--- trunk/CREDITS (original)
+++ trunk/CREDITS Thu Oct 25 20:17:10 2007
@@ -13,61 +13,64 @@
 
 Other contributors of note since 1.7.10:
 
-       Chris Frey <[EMAIL PROTECTED]>: Lots of GCC warning fixes for
-       the bleeding-edge compiler versions, and Gentoo ebuild support.
-       Also, if there were a "steering committee" for MySQL++, he'd be
-       on it.
+    Chris Frey <[EMAIL PROTECTED]>: Lots of GCC warning fixes for the
+    bleeding-edge compiler versions, and Gentoo ebuild support.
 
-       Mark Meredino <[EMAIL PROTECTED]>: Several fixes and
-       additions, including a lot of work on Microsoft Visual C++
-       compatibility, and discoveries made while spelunking in the
-       library.
+    Mark Meredino <[EMAIL PROTECTED]>: Several fixes and
+    additions, including a lot of work on Microsoft Visual C++
+    compatibility, and discoveries made while spelunking in the
+    library.
 
-       Evan Wies <[EMAIL PROTECTED]>: Contributed several C++ code
-       style cleanups.
+    Evan Wies <[EMAIL PROTECTED]>: Contributed several C++ code
+    style cleanups.
 
-       Arnon Jalon <[EMAIL PROTECTED]>: Added the multi-query
-       result set handling features, and multiquery example to demonstrate
-       it.
+    Arnon Jalon <[EMAIL PROTECTED]>: Added the multi-query
+    result set handling features, and examples/multiquery.cpp to
+    demonstrate it.
 
-       Korolyov Ilya has submitted several patches in many different
-       areas of the library.
+    Korolyov Ilya has submitted several patches in many different
+    areas of the library.
 
-       Remi Collet <[EMAIL PROTECTED]> is maintaining offical
-       RPMs for Fedora, with other systems on the way.  His work has
-       improved the RPM spec file we distribute greatly.
+    Remi Collet <[EMAIL PROTECTED]> is maintaining offical RPMs
+    for Fedora, with other systems on the way.  His work has improved
+    the RPM spec file we distribute greatly.
 
-       Joel Fielder <[EMAIL PROTECTED]> came up with the
-       original idea for Query::for_each() and Query::store_in(),
-       provided the basis for examples/for_each.cpp, and provided
-       a fix for exception flag propagation in Query.
+    Joel Fielder <[EMAIL PROTECTED]> came up with the
+    original idea for Query::for_each() and Query::store_in(),
+    provided the basis for examples/for_each.cpp, and provided a fix
+    for exception flag propagation in Query.
+
+    Jim Wallace <[EMAIL PROTECTED]> demonstrated the need
+    for BadQuery::errnum(), and contributed the patches and also
+    examples/deadlock.cpp to test that this feature does what it is
+    supposed to.
 
 
 Here are the personal credits from the old 1.7.9 documentation,
 apparently written by Kevin Atkinson:
 
-       Chris Halverson - For helping me get it to compile under Solaris.
+    Chris Halverson - For helping me get it to compile under Solaris.
 
-       Fredric Fredricson - For a long talk about automatic conversions.
+    Fredric Fredricson - For a long talk about automatic conversions.
 
-       Michael Widenius - MySQL developer who has been very supportive of
-               my efforts.
+    Michael Widenius - MySQL developer who has been very supportive of
+        my efforts.
 
-       Paul J. Lucas - For the original idea of treating the query object
-               like a stream.
+    Paul J. Lucas - For the original idea of treating the query object
+        like a stream.
 
-       Scott Barron - For helping me with the shared libraries.
+    Scott Barron - For helping me with the shared libraries.
 
-       Jools Enticknap - For giving me the Template Queries idea.
+    Jools Enticknap - For giving me the Template Queries idea.
 
-       M. S. Sriram - For a detailed dission of how the Template Queries
-               should be implemented, the suggestion to throw exceptions on bad
-               queries, and the idea of having a back-end independent query
-               object (ie SQLQuery).
+    M. S. Sriram - For a detailed dission of how the Template Queries
+        should be implemented, the suggestion to throw exceptions on bad
+        queries, and the idea of having a back-end independent query
+        object (ie SQLQuery).
 
-       Sinisa Milivojevic - For becoming the new offical maintainer.
+    Sinisa Milivojevic - For becoming the new offical maintainer.
 
-       D. Hawkins and E. Loic for their autoconf + automake contribution.
+    D. Hawkins and E. Loic for their autoconf + automake contribution.
 
 
 See the ChangeLog for further credits, and details about the differences
@@ -78,7 +81,7 @@
 MySQL++. All of us who are still active in MySQL++ development read the
 mailing list, so questions sent there do get to us:
 
-       http://lists.mysql.com/plusplus
+    http://lists.mysql.com/plusplus
 
 The mailing list is superior to private email because the answers are
 archived for future questioners to find, and because you are likely to

Modified: trunk/examples/cmdline.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/cmdline.cpp?rev=1791&r1=1790&r2=1791&view=diff
==============================================================================
--- trunk/examples/cmdline.cpp (original)
+++ trunk/examples/cmdline.cpp Thu Oct 25 20:17:10 2007
@@ -35,6 +35,7 @@
 //// globals and constants /////////////////////////////////////////////
 
 bool dtest_mode = false;               // true when running under dtest
+int run_mode = 0;                              // -m switch's value
 const char* kpcSampleDatabase = "mysql_cpp_data";
 
 
@@ -155,12 +156,13 @@
     }
 
     int ch;
-    while ((ch = att_getopt(argc, argv, "p:s:u:D")) != EOF) {
+    while ((ch = att_getopt(argc, argv, "m:p:s:u:D")) != EOF) {
         switch (ch) {
-            case 'p': *pppass = ag_optarg;   break;
-            case 's': *ppserver = ag_optarg; break;
-            case 'u': *ppuser = ag_optarg;   break;
-            case 'D': dtest_mode = true;     break;
+                       case 'm': run_mode = atoi(ag_optarg); break;
+            case 'p': *pppass = ag_optarg;        break;
+            case 's': *ppserver = ag_optarg;      break;
+            case 'u': *ppuser = ag_optarg;        break;
+            case 'D': dtest_mode = true;          break;
             default:
                 print_usage(argv[0], extra_parms);
                 return false;

Added: trunk/examples/deadlock.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/deadlock.cpp?rev=1791&view=auto
==============================================================================
--- trunk/examples/deadlock.cpp (added)
+++ trunk/examples/deadlock.cpp Thu Oct 25 20:17:10 2007
@@ -1,0 +1,111 @@
+/***********************************************************************
+ deadlock.cpp - Demonstrates how MySQL's deadlock detection interacts
+       with MySQL++'s Transaction class an exception handling mechanism.
+       Run one copy of this program with the -m1 command line switch, then
+       while it's waiting for you to press Enter, run another copy with -m2
+       instead.
+
+ Copyright (c) 2007 by Jim Wallace and (c) 2007 by Educational
+ Technology Resources, Inc.  Others may also hold copyrights on code
+ in this file.  See the CREDITS file in the top directory of the
+ distribution for details.
+
+ This file is part of MySQL++.
+
+ MySQL++ is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ MySQL++ is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with MySQL++; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+***********************************************************************/
+
+#include "cmdline.h"
+
+#include <mysql++.h>
+#include <mysqld_error.h>
+
+#include <iostream>
+
+using namespace std;
+
+// Bring in global holding the value given to the -m switch
+extern int run_mode;
+
+
+int
+main(int argc, char *argv[])
+{
+       // Get database access parameters from command line
+    const char* db = 0, *server = 0, *user = 0, *pass = "";
+       if (!parse_command_line(argc, argv, &db, &server, &user, &pass)) {
+               return 1;
+       }
+
+       // Check that the mode parameter was also given and it makes sense
+       if ((run_mode != 1) && (run_mode != 2)) {
+               cerr << argv[0] << " must be run with -m1 or -m2 as one of "
+                               "its command-line arguments." << endl;
+               return 1;
+       }
+
+       mysqlpp::Connection con;
+       try {
+               // Establish the connection to the database server
+               con.connect(db, server, user, pass);
+
+               // Start a transaction set.  Transactions create mutex locks on
+               // modified rows, so if two programs both touch the same pair of
+               // rows but in opposite orders at the wrong time, one of the two
+               // programs will deadlock.  The MySQL server knows how to detect
+               // this situation, and its error return causes MySQL++ to throw
+               // a BadQuery exception.  The point of this example is that if
+               // you want to detect this problem, you would check the value of
+               // BadQuery::errnum(), not Connection::errnum(), because the
+               // transaction rollback process executes a query which succeeds,
+               // setting the MySQL C API's "last error number" value to 0.
+               // The exception object carries its own copy of the error number
+               // at the point the exception was thrown for this very reason.
+               mysqlpp::Query query = con.query();
+               mysqlpp::Transaction trans(con);
+
+               // Build and run the queries, with the order depending on the -m
+               // flag, so that a second copy of the program will deadlock if
+               // run while the first is waiting for Enter.
+               char dummy[100];
+               for (int i = 0; i < 2; ++i) {
+                       int lock = run_mode + (run_mode == 1 ? i : -i);
+                       cout << "Trying lock " << lock << "..." << endl;
+
+                       query.reset();
+                       query << "select * from deadlock_test" << lock << 
+                                       " where x = " << lock << " for update";
+                       query.store();
+
+                       cout << "Acquired lock " << lock << ".  Press Enter to 
";
+                       cout << (i == 0 ? "try next lock" : "exit");
+                       cout << ": " << flush;
+                       cin.getline(dummy, sizeof(dummy));
+               }
+       }
+       catch (mysqlpp::BadQuery e) {
+               cerr << "Query error: " << e.what() << endl;
+               cerr << "Connection::errnum = " << con.errnum() <<
+                               ", BadQuery::errnum = " << e.errnum() << endl;
+               return 1;
+       }
+       catch (mysqlpp::Exception e) {
+               cerr << "General error: " << e.what() << endl;          
+               return 1;
+       }
+
+       return 0;
+}

Propchange: trunk/examples/deadlock.cpp
------------------------------------------------------------------------------
    svn:executable = *

Modified: trunk/examples/resetdb.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/resetdb.cpp?rev=1791&r1=1790&r2=1791&view=diff
==============================================================================
--- trunk/examples/resetdb.cpp (original)
+++ trunk/examples/resetdb.cpp Thu Oct 25 20:17:10 2007
@@ -94,11 +94,13 @@
                mysqlpp::NoExceptions ne(con);
                mysqlpp::Query query = con.query();
                if (con.select_db(kpcSampleDatabase)) {
-                       // Toss old table, if it exists.  If it doesn't, we 
don't
-                       // really care, as it'll get created next.
+                       // Toss old tables, ignoring errors because it would 
just
+                       // mean the table doesn't exist, which doesn't matter.
                        cout << "Dropping existing sample data tables..." << 
endl;
                        query.exec("drop table stock");
                        query.exec("drop table images");
+                       query.exec("drop table deadlock_test1");
+                       query.exec("drop table deadlock_test2");
                }
                else {
                        // Database doesn't exist yet, so create and select it.
@@ -162,6 +164,17 @@
                                ")";
                query.execute();
 
+               // Create the tables used by examples/deadlock.cpp
+               cout << "Creating deadlock testing tables..." << endl;
+               query.reset();
+               query.execute("CREATE TABLE deadlock_test1 (x INT) 
ENGINE=innodb");
+               query.reset();
+               query.execute("CREATE TABLE deadlock_test2 (x INT) 
ENGINE=innodb");
+               query.reset();
+               query.execute("INSERT INTO deadlock_test1 VALUES (1);");
+               query.reset();
+               query.execute("INSERT INTO deadlock_test2 VALUES (2);");
+
                // Report success
                cout << (new_db ? "Created" : "Reinitialized") <<
                                " sample database successfully." << endl;

Modified: trunk/mysql++.bkl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/mysql%2B%2B.bkl?rev=1791&r1=1790&r2=1791&view=diff
==============================================================================
--- trunk/mysql++.bkl (original)
+++ trunk/mysql++.bkl Thu Oct 25 20:17:10 2007
@@ -226,6 +226,9 @@
                <exe id="simple3" template="libexcommon-user,programs">
                        <sources>examples/simple3.cpp</sources>
                </exe>
+               <exe id="deadlock" template="libexcommon-user,programs">
+                       <sources>examples/deadlock.cpp</sources>
+               </exe>
                <exe id="multiquery" template="libexcommon-user,programs">
                        <sources>examples/multiquery.cpp</sources>
                </exe>


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to