MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndbapi_50compat1.cpp
1 /*
2  Copyright (C) 2007 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 //
20 // ndbapi_async1.cpp: Using asynchronous transactions in NDB API
21 //
22 // Execute ndbapi_example1 to create the table "MYTABLENAME"
23 // before executing this program.
24 //
25 // Correct output from this program is:
26 //
27 // Successful insert.
28 // Successful insert.
29 
30 #include <mysql.h>
31 #include <NdbApi.hpp>
32 
33 // Used for cout
34 #include <stdio.h>
35 #include <NdbOut.hpp>
36 
37 #define APIERROR(error) \
38  { ndbout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \
39  << error.code << ", msg: " << error.message << "." << endl; \
40  exit(-1); }
41 
42 static void callback(int result, NdbTransaction* NdbObject, void* aObject);
43 
44 int main()
45 {
46  ndb_init();
47 
48  Ndb_cluster_connection *cluster_connection=
49  new Ndb_cluster_connection(); // Object representing the cluster
50 
51  if (cluster_connection->wait_until_ready(30,30))
52  {
53  ndbout << "Cluster was not ready within 30 secs." << endl;
54  exit(-1);
55  }
56 
57  int r= cluster_connection->connect(5 /* retries */,
58  3 /* delay between retries */,
59  1 /* verbose */);
60  if (r > 0)
61  {
62  ndbout
63  << "Cluster connect failed, possibly resolved with more retries.\n";
64  exit(-1);
65  }
66  else if (r < 0)
67  {
68  ndbout
69  << "Cluster connect failed.\n";
70  exit(-1);
71  }
72 
73  if (cluster_connection->wait_until_ready(30,30))
74  {
75  ndbout << "Cluster was not ready within 30 secs." << endl;
76  exit(-1);
77  }
78 
79  Ndb* myNdb = new Ndb( cluster_connection,
80  "TEST_DB_2" ); // Object representing the database
81 
82  NdbTransaction* myNdbTransaction[2]; // For transactions
83  NdbOperation* myNdbOperation; // For operations
84 
85  if (myNdb->init(2) == -1) { // Want two parallel insert transactions
86  APIERROR(myNdb->getNdbError());
87  exit(-1);
88  }
89 
90  /******************************************************
91  * Insert (we do two insert transactions in parallel) *
92  ******************************************************/
93  const NdbDictionary::Dictionary* myDict= myNdb->getDictionary();
94  const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
95  if (myTable == NULL)
96  APIERROR(myDict->getNdbError());
97  for (int i = 0; i < 2; i++) {
98  myNdbTransaction[i] = myNdb->startTransaction();
99  if (myNdbTransaction[i] == NULL) APIERROR(myNdb->getNdbError());
100 
101  myNdbOperation = myNdbTransaction[i]->getNdbOperation(myTable);
102  if (myNdbOperation == NULL) APIERROR(myNdbTransaction[i]->getNdbError());
103 
104  myNdbOperation->insertTuple();
105  myNdbOperation->equal("ATTR1", 20 + i);
106  myNdbOperation->setValue("ATTR2", 20 + i);
107 
108  // Prepare transaction (the transaction is NOT yet sent to NDB)
109  myNdbTransaction[i]->executeAsynchPrepare(NdbTransaction::Commit,
110  &callback, NULL);
111  }
112 
113  // Send all transactions to NDB
114  myNdb->sendPreparedTransactions(0);
115 
116  // Poll all transactions
117  myNdb->pollNdb(3000, 2);
118 
119  // Close all transactions
120  for (int i = 0; i < 2; i++)
121  myNdb->closeTransaction(myNdbTransaction[i]);
122 
123  delete myNdb;
124  delete cluster_connection;
125 
126  ndb_end(0);
127  return 0;
128 }
129 
130 /*
131  * callback : This is called when the transaction is polled
132  *
133  * (This function must have three arguments:
134  * - The result of the transaction,
135  * - The NdbTransaction object, and
136  * - A pointer to an arbitrary object.)
137  */
138 
139 static void
140 callback(int result, NdbTransaction* myTrans, void* aObject)
141 {
142  if (result == -1) {
143  ndbout << "Poll error: " << endl;
144  APIERROR(myTrans->getNdbError());
145  } else {
146  ndbout << "Successful insert." << endl;
147  }
148 }