MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConnectionPoolTest.java
1 /*
2  * Copyright (c) 2011, 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.util.Arrays;
21 import java.util.List;
22 import java.util.Properties;
23 
24 import testsuite.clusterj.model.Employee;
25 
26 import com.mysql.clusterj.ClusterJFatalUserException;
27 import com.mysql.clusterj.ClusterJHelper;
28 import com.mysql.clusterj.Constants;
29 import com.mysql.clusterj.Session;
30 import com.mysql.clusterj.SessionFactory;
31 import com.mysql.clusterj.core.SessionFactoryImpl;
32 
33 
35 
36  @Override
37  public boolean getDebug() {
38  return false;
39  }
40 
41  protected boolean runSpecificNodeIdTests() {
42  return false;
43  }
44 
45  @Override
46  public void localSetUp() {
48  // close the existing session factory because it uses one of the cluster connection (api) nodes
49  if (sessionFactory != null) {
50  sessionFactory.close();
51  sessionFactory = null;
52  }
53  }
54 
55  public void testNoPooling() {
56  Properties modifiedProperties = new Properties();
57  modifiedProperties.putAll(props);
58  SessionFactory sessionFactory1 = null;
59  SessionFactory sessionFactory2 = null;
60 
61  // with connection.pool.size set to 1 each session factory should be the same
62  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 1);
63  sessionFactory1 = ClusterJHelper.getSessionFactory(modifiedProperties);
64  sessionFactory2 = ClusterJHelper.getSessionFactory(modifiedProperties);
65  sessionFactory1.close();
66  sessionFactory2.close();
67  errorIfNotEqual("With connection pooling, SessionFactory1 should be the same object as SessionFactory2",
68  true, sessionFactory1 == sessionFactory2);
69 
70  // with connection.pool.size set to 0 each session factory should be unique
71  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 0);
72  sessionFactory1 = ClusterJHelper.getSessionFactory(modifiedProperties);
73  sessionFactory2 = ClusterJHelper.getSessionFactory(modifiedProperties);
74  try {
75  SessionFactory sessionFactory3 = ClusterJHelper.getSessionFactory(modifiedProperties);
76  sessionFactory3.close();
77  } catch (ClusterJFatalUserException ex) {
78  // good catch
79  }
80  sessionFactory1.close();
81  sessionFactory2.close();
82  errorIfNotEqual("With no connection pooling, SessionFactory1 should not be the same object as SessionFactory2",
83  false, sessionFactory1 == sessionFactory2);
84 
85  failOnError();
86  }
87 
88  public void testConnectionPoolSize() {
89  Properties modifiedProperties = new Properties();
90  modifiedProperties.putAll(props);
91  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 2);
92  checkConnectionPoolSize2("testConnectionPoolSize", modifiedProperties);
93  failOnError();
94  }
95 
96  public void testConnectionPoolSizeAndNodeIds() {
97  if (!runSpecificNodeIdTests()) {
98  return;
99  }
100  Properties modifiedProperties = new Properties();
101  modifiedProperties.putAll(props);
102  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 2);
103  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "4;5");
104  checkConnectionPoolSize2("testConnectionPoolSizeAndNodeIds", modifiedProperties);
105  failOnError();
106  }
107 
108  public void testConnectionNodeIds() {
109  if (!runSpecificNodeIdTests()) {
110  return;
111  }
112  Properties modifiedProperties = new Properties();
113  modifiedProperties.putAll(props);
114  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "4,5");
115  checkConnectionPoolSize2("testConnectionNodeIds", modifiedProperties);
116  failOnError();
117  }
118 
119  public void testConnectionSingleNodeIdAndConnectionPoolSize() {
120  if (!runSpecificNodeIdTests()) {
121  return;
122  }
123  Properties modifiedProperties = new Properties();
124  modifiedProperties.putAll(props);
125  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 2);
126  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "4");
127  checkConnectionPoolSize2("testConnectionNodeIds", modifiedProperties);
128  failOnError();
129  }
130 
131  private void checkConnectionPoolSize2(String where, Properties modifiedProperties) {
132  SessionFactory sessionFactory1 = null;
133  SessionFactory sessionFactory2 = null;
134  SessionFactory sessionFactory3 = null;
135  sessionFactory1 = ClusterJHelper.getSessionFactory(modifiedProperties);
136  sessionFactory2 = ClusterJHelper.getSessionFactory(modifiedProperties);
137  sessionFactory3 = ClusterJHelper.getSessionFactory(modifiedProperties);
138  errorIfNotEqual(where + " SessionFactory1 should be the same object as SessionFactory2", true,
139  sessionFactory1 == sessionFactory2);
140  errorIfNotEqual(where + " SessionFactory1 should be the same object as SessionFactory3", true,
141  sessionFactory1 == sessionFactory3);
142  Session session1 = sessionFactory1.getSession();
143  Employee e1 = session1.find(Employee.class, 0);
144  checkSessions(where + " after get session1", sessionFactory1, new Integer[] {1, 0});
145  Session session2 = sessionFactory1.getSession();
146  Employee e2 = session2.find(Employee.class, 0);
147  checkSessions(where + " after get session2", sessionFactory1, new Integer[] {1, 1});
148  Session session3 = sessionFactory1.getSession();
149  checkSessions(where + " nafter get session3", sessionFactory1, new Integer[] {2, 1});
150  Session session4 = sessionFactory1.getSession();
151  checkSessions(where + " after get session4", sessionFactory1, new Integer[] {2, 2});
152  Session session5 = sessionFactory1.getSession();
153  checkSessions(where + " after get session5", sessionFactory1, new Integer[] {3, 2});
154  Session session6 = sessionFactory1.getSession();
155  checkSessions(where + " after get session6", sessionFactory1, new Integer[] {3, 3});
156 
157  session1.close();
158  checkSessions(where + " after close session1", sessionFactory1, new Integer[] {2, 3});
159  session4.close();
160  checkSessions(where + " after close session4", sessionFactory1, new Integer[] {2, 2});
161  session5.close();
162  checkSessions(where + " after close session5", sessionFactory1, new Integer[] {1, 2});
163  Session session7 = sessionFactory1.getSession();
164  checkSessions(where + " after get session7", sessionFactory1, new Integer[] {2, 2});
165 
166  session2.close();
167  session3.close();
168  session6.close();
169  session7.close();
170  sessionFactory1.close();
171  }
172 
173  public void testNegativeMismatchConnectionPoolSizeAndConnectionPoolNodeids() {
174  Properties modifiedProperties = new Properties();
175  modifiedProperties.putAll(props);
176  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 3);
177  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "4\t5");
178  try {
179  ClusterJHelper.getSessionFactory(modifiedProperties);
180  } catch (ClusterJFatalUserException ex) {
181  if (getDebug()) ex.printStackTrace();
182  // good catch
183  String expected = "4\t5";
184  if (!ex.getMessage().contains(expected)) {
185  error("Mismatch error message should contain " + expected);
186  }
187  }
188  failOnError();
189  }
190 
191  public void testNegativeConnectionPoolNodeidsFormatError() {
192  Properties modifiedProperties = new Properties();
193  modifiedProperties.putAll(props);
194  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 2);
195  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "7 t");
196  try {
197  ClusterJHelper.getSessionFactory(modifiedProperties);
198  } catch (ClusterJFatalUserException ex) {
199  if (getDebug()) ex.printStackTrace();
200  // good catch
201  String expected = "NumberFormatException";
202  if (!ex.getMessage().contains(expected)) {
203  error("Mismatch error message '" + ex.getMessage() + "' should contain '" + expected + '"');
204  }
205  }
206  failOnError();
207  }
208 
209  public void testNegativeConnectionPoolIllegalNodeids() {
210  Properties modifiedProperties = new Properties();
211  modifiedProperties.putAll(props);
212  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "256");
213  try {
214  ClusterJHelper.getSessionFactory(modifiedProperties);
215  } catch (ClusterJFatalUserException ex) {
216  if (getDebug()) ex.printStackTrace();
217  // good catch
218  String expected = "illegal";
219  if (!ex.getMessage().contains(expected)) {
220  error("Mismatch error message '" + ex.getMessage() + "' should contain '" + expected + '"');
221  }
222  }
223  failOnError();
224  }
225 
226  public void testNegativeConnectionPoolNoNodeId() {
227  if (!runSpecificNodeIdTests()) {
228  return;
229  }
230  Properties modifiedProperties = new Properties();
231  modifiedProperties.putAll(props);
232  modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "48");
233  try {
234  ClusterJHelper.getSessionFactory(modifiedProperties);
235  } catch (ClusterJFatalUserException ex) {
236  if (getDebug()) ex.printStackTrace();
237  // good catch
238  String expected = "No node defined";
239  if (!ex.getMessage().contains(expected)) {
240  error("Mismatch error message '" + ex.getMessage() + "' should contain '" + expected + '"');
241  }
242  }
243  failOnError();
244  }
245 
246  private void checkSessions(String where, SessionFactory sessionFactory1, Integer[] expected) {
247  SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl)sessionFactory1;
248  List<Integer> connectionCounts = sessionFactoryImpl.getConnectionPoolSessionCounts();
249  if (expected.length != connectionCounts.size()) {
250  error(where + " wrong number of connections in pool\n"
251  + "Expected: " + Arrays.toString(expected)
252  + " Actual: " + connectionCounts);
253  return;
254  }
255  int i = 0;
256  for (Integer connectionCount: connectionCounts) {
257  if (getDebug()) System.out.println("Connection " + i + " has " + connectionCount + " sessions.");
258  if (i >= expected.length) break;
259  errorIfNotEqual(where + " wrong count on connection " + i, expected[i], connectionCount);
260  i++;
261  }
262  if (getDebug()) System.out.println();
263  }
264 }