2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
18 package testsuite.clusterj;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
24 import org.junit.Ignore;
26 /** Test that mysql session variable ndb_coordinated_transaction_id can be
27 * read and written by jdbc.
30 public class CoordinatedTransactionIdVariableTest extends AbstractClusterJTest {
32 /** Format is Uint32+Uint32:Uint64 */
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";
39 protected void localSetUp() {
40 createSessionFactory();
43 setAutoCommit(connection, false);
47 protected boolean getDebug() {
51 /** Verify that the initial value of the variable ndb_coordinated_transaction_id is null.
53 public void checkInitialValue() {
55 String id = getJDBCCoordinatedTransactionId("checkInitialValue");
56 errorIfNotEqual("Coordinated transaction id must default to null.", null, id);
59 /** Try to set the ndb_coordinated_transaction_id variable to a new value
60 * and verify that it can be read back.
62 public void checkNewValue() {
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
73 /** Verify that after an ndb transaction is started the coordinated transaction id is not null
74 * and is null after commit.
76 public void checkIdAfterTransactionStartAndCommit() {
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);
89 /** Verify that after an ndb transaction is started the coordinated transaction id is not null
90 * and is null after rollback.
92 public void checkIdAfterTransactionStartAndRollback() {
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);
100 rollbackConnection();
101 id = getJDBCCoordinatedTransactionId("checkIdAfterTransactionStartAndRollback");
102 errorIfNotEqual("Coordinated transaction id must be null after rollback.", null, id);
105 /** Execute a SQL query. Throw away the results. Keep the transaction open.
107 protected void executeJDBCQuery(String where) {
108 PreparedStatement statement = null;
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);
121 } catch (SQLException e) {
122 error(where + " rs.close threw exception " + e.getMessage());
125 if (statement != null) {
128 } catch (SQLException e) {
129 error(where + " statement.close threw exception ", e);
135 /** Set the coordinated_transaction_id variable in the server.
136 * @param newId the id to set
138 protected void setJDBCCoordinatedTransactionId(String where, String newId) {
140 fail(where + " test case error: coordinated transaction id must not be null.");
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);
152 /** Get the coordinated_transaction_id variable from the server.
153 * @return the id from the server
155 protected String getJDBCCoordinatedTransactionId(String where) {
156 String getId = "select " + getTransactionIdVariableName;
157 String result = null;
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);
171 /** Commit the connection to clean it up for the next use.
173 protected void commitConnection() {
176 } catch (SQLException e) {
177 error("connection.commit threw exception: ", e);
181 /** Roll back the connection to clean it up for the next use.
183 protected void rollbackConnection() {
185 connection.rollback();
186 } catch (SQLException e) {
187 error("connection.rollback threw exception: ", e);