]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
44aa1b6ffba67901fd9287c87f73c5e0e1c935c5
[packages/trusty/mysql-wsrep-5.6.git] /
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
26 /** Test that mysql session variable ndb_coordinated_transaction_id can be
27  * read and written by jdbc.
28  */
29 @Ignore
30 public class CoordinatedTransactionIdVariableTest extends AbstractClusterJTest {
31
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";
37
38     @Override
39     protected void localSetUp() {
40         createSessionFactory();
41         closeConnection();
42         getConnection();
43         setAutoCommit(connection, false);
44     }
45
46     @Override
47     protected boolean getDebug() {
48         return false;
49     }
50
51     /** Verify that the initial value of the variable ndb_coordinated_transaction_id is null.
52      */
53     public void checkInitialValue() {
54         getConnection();
55         String id = getJDBCCoordinatedTransactionId("checkInitialValue");
56         errorIfNotEqual("Coordinated transaction id must default to null.", null, id);
57     }
58
59     /** Try to set the ndb_coordinated_transaction_id variable to a new value
60      * and verify that it can be read back.
61      */
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
70         closeConnection();
71     }
72
73     /** Verify that after an ndb transaction is started the coordinated transaction id is not null
74      * and is null after commit.
75      */
76     public void checkIdAfterTransactionStartAndCommit() {
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);
84         commitConnection();
85         id = getJDBCCoordinatedTransactionId("checkIdAfterTransactionStartAndCommit");
86         errorIfNotEqual("Coordinated transaction id must be null after commit.", null, id);
87         }
88
89     /** Verify that after an ndb transaction is started the coordinated transaction id is not null
90      * and is null after rollback.
91      */
92     public void checkIdAfterTransactionStartAndRollback() {
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);
100         rollbackConnection();
101         id = getJDBCCoordinatedTransactionId("checkIdAfterTransactionStartAndRollback");
102         errorIfNotEqual("Coordinated transaction id must be null after rollback.", null, id);
103         }
104
105     /** Execute a SQL query. Throw away the results. Keep the transaction open.
106      */
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
135     /** Set the coordinated_transaction_id variable in the server.
136      * @param newId the id to set
137      */
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
152     /** Get the coordinated_transaction_id variable from the server.
153      * @return the id from the server
154      */
155     protected String getJDBCCoordinatedTransactionId(String where) {
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
171     /** Commit the connection to clean it up for the next use.
172      */
173     protected void commitConnection() {
174         try {
175             connection.commit();
176         } catch (SQLException e) {
177             error("connection.commit threw exception: ", e);
178         }
179     }
180
181     /** Roll back the connection to clean it up for the next use.
182      */
183     protected void rollbackConnection() {
184         try {
185             connection.rollback();
186         } catch (SQLException e) {
187             error("connection.rollback threw exception: ", e);
188         }
189     }
190
191 }