MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CoordinatedTransactionIdVariableTest.java
1 /*
2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 package testsuite.clusterj;
19 
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 
24 import org.junit.Ignore;
25 
29 @Ignore
31 
33  private String newId = "1+1:9000000000000099";
34  private String sqlQuery = "select id from t_basic where id = 0";
35  private String getTransactionIdVariableName = "@@ndb_transaction_id";
36  private String setTransactionIdVariableName = "@@ndb_join_transaction_id";
37 
38  @Override
39  protected void localSetUp() {
40  createSessionFactory();
42  getConnection();
43  setAutoCommit(connection, false);
44  }
45 
46  @Override
47  protected boolean getDebug() {
48  return false;
49  }
50 
53  public void checkInitialValue() {
54  getConnection();
55  String id = getJDBCCoordinatedTransactionId("checkInitialValue");
56  errorIfNotEqual("Coordinated transaction id must default to null.", null, id);
57  }
58 
62  public void checkNewValue() {
63  getConnection();
64  // set the coordinated_transaction_id to some random value
65  setJDBCCoordinatedTransactionId("checkNewValue", newId);
66  String id = getJDBCCoordinatedTransactionId("checkNewValue");
67  errorIfNotEqual("failed to set coordinated transaction id.", newId, id);
68  executeJDBCQuery("checkNewValue");
69  // close the connection so the value isn't accidentally used by a new transaction
71  }
72 
77  getConnection();
78  // execute a query statement that will cause the server to start an ndb transaction
79  executeJDBCQuery("checkIdAfterTransactionStartAndCommit");
80  // the coordinated transaction id should now be available
81  String id = getJDBCCoordinatedTransactionId("checkIdAfterTransactionStartAndCommit");
82  // we can only test for not null since we cannot predict the transaction id
83  errorIfEqual("Coordinated transaction must not be null after transaction start", null, id);
85  id = getJDBCCoordinatedTransactionId("checkIdAfterTransactionStartAndCommit");
86  errorIfNotEqual("Coordinated transaction id must be null after commit.", null, id);
87  }
88 
93  getConnection();
94  // execute a query statement that will cause the server to start an ndb transaction
95  executeJDBCQuery("checkIdAfterTransactionStartAndRollback");
96  // the coordinated transaction id should now be available
97  String id = getJDBCCoordinatedTransactionId("checkIdAfterTransactionStartAndRollback");
98  // we can only test for not null since we cannot predict the transaction id
99  errorIfEqual("Coordinated transaction must not be null after transaction start", null, id);
101  id = getJDBCCoordinatedTransactionId("checkIdAfterTransactionStartAndRollback");
102  errorIfNotEqual("Coordinated transaction id must be null after rollback.", null, id);
103  }
104 
107  protected void executeJDBCQuery(String where) {
108  PreparedStatement statement = null;
109  ResultSet rs = null;
110  try {
111  statement = connection.prepareStatement(sqlQuery);
112  rs = statement.executeQuery();
113  boolean hasNext = rs.next();
114  if (getDebug()) System.out.println(where + " executeJDBCQuery rs.next() returned " + hasNext);
115  } catch (SQLException e) {
116  error(where + " query threw exception ", e);
117  } finally {
118  if (rs != null) {
119  try {
120  rs.close();
121  } catch (SQLException e) {
122  error(where + " rs.close threw exception " + e.getMessage());
123  }
124  }
125  if (statement != null) {
126  try {
127  statement.close();
128  } catch (SQLException e) {
129  error(where + " statement.close threw exception ", e);
130  }
131  }
132  }
133  }
134 
138  protected void setJDBCCoordinatedTransactionId(String where, String newId) {
139  if (newId == null) {
140  fail(where + " test case error: coordinated transaction id must not be null.");
141  }
142  try {
143  String setSql = "set " + setTransactionIdVariableName + " = '" + newId + "'";
144  PreparedStatement setCoordinatedTransactionIdStatement = connection.prepareStatement(setSql);
145  boolean result = setCoordinatedTransactionIdStatement.execute();
146  errorIfNotEqual(where + " set coordinated transaction id returned true.", false, result);
147  } catch (SQLException e) {
148  error(where + " caught exception on set coordinated transaction id:", e);
149  }
150  }
151 
156  String getId = "select " + getTransactionIdVariableName;
157  String result = null;
158  try {
159  PreparedStatement getCoordinatedTransactionIdStatement = connection.prepareStatement(getId);
160  ResultSet rs = getCoordinatedTransactionIdStatement.executeQuery();
161  boolean hasResult = rs.next();
162  errorIfNotEqual(where + " select coordinated transaction id returned false.", true, hasResult);
163  result = rs.getString(1);
164  if (getDebug()) System.out.println(where + " getJDBCCoordinatedTransactionId returns " + result);
165  } catch (SQLException e) {
166  error(where + " caught exception on get coordinated transaction id.", e);
167  }
168  return result;
169  }
170 
173  protected void commitConnection() {
174  try {
175  connection.commit();
176  } catch (SQLException e) {
177  error("connection.commit threw exception: ", e);
178  }
179  }
180 
183  protected void rollbackConnection() {
184  try {
185  connection.rollback();
186  } catch (SQLException e) {
187  error("connection.rollback threw exception: ", e);
188  }
189  }
190 
191 }