2 * Copyright (c) 2011, 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.util.Arrays;
21 import java.util.List;
22 import java.util.Properties;
24 import testsuite.clusterj.model.Employee;
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;
34 public class ConnectionPoolTest extends AbstractClusterJTest {
37 public boolean getDebug() {
41 protected boolean runSpecificNodeIdTests() {
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;
55 public void testNoPooling() {
56 Properties modifiedProperties = new Properties();
57 modifiedProperties.putAll(props);
58 SessionFactory sessionFactory1 = null;
59 SessionFactory sessionFactory2 = null;
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);
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);
75 SessionFactory sessionFactory3 = ClusterJHelper.getSessionFactory(modifiedProperties);
76 sessionFactory3.close();
77 } catch (ClusterJFatalUserException ex) {
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);
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);
96 public void testConnectionPoolSizeAndNodeIds() {
97 if (!runSpecificNodeIdTests()) {
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);
108 public void testConnectionNodeIds() {
109 if (!runSpecificNodeIdTests()) {
112 Properties modifiedProperties = new Properties();
113 modifiedProperties.putAll(props);
114 modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "4,5");
115 checkConnectionPoolSize2("testConnectionNodeIds", modifiedProperties);
119 public void testConnectionSingleNodeIdAndConnectionPoolSize() {
120 if (!runSpecificNodeIdTests()) {
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);
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});
158 checkSessions(where + " after close session1", sessionFactory1, new Integer[] {2, 3});
160 checkSessions(where + " after close session4", sessionFactory1, new Integer[] {2, 2});
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});
170 sessionFactory1.close();
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");
179 ClusterJHelper.getSessionFactory(modifiedProperties);
180 } catch (ClusterJFatalUserException ex) {
181 if (getDebug()) ex.printStackTrace();
183 String expected = "4\t5";
184 if (!ex.getMessage().contains(expected)) {
185 error("Mismatch error message should contain " + expected);
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");
197 ClusterJHelper.getSessionFactory(modifiedProperties);
198 } catch (ClusterJFatalUserException ex) {
199 if (getDebug()) ex.printStackTrace();
201 String expected = "NumberFormatException";
202 if (!ex.getMessage().contains(expected)) {
203 error("Mismatch error message '" + ex.getMessage() + "' should contain '" + expected + '"');
209 public void testNegativeConnectionPoolIllegalNodeids() {
210 Properties modifiedProperties = new Properties();
211 modifiedProperties.putAll(props);
212 modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "256");
214 ClusterJHelper.getSessionFactory(modifiedProperties);
215 } catch (ClusterJFatalUserException ex) {
216 if (getDebug()) ex.printStackTrace();
218 String expected = "illegal";
219 if (!ex.getMessage().contains(expected)) {
220 error("Mismatch error message '" + ex.getMessage() + "' should contain '" + expected + '"');
226 public void testNegativeConnectionPoolNoNodeId() {
227 if (!runSpecificNodeIdTests()) {
230 Properties modifiedProperties = new Properties();
231 modifiedProperties.putAll(props);
232 modifiedProperties.put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS, "48");
234 ClusterJHelper.getSessionFactory(modifiedProperties);
235 } catch (ClusterJFatalUserException ex) {
236 if (getDebug()) ex.printStackTrace();
238 String expected = "No node defined";
239 if (!ex.getMessage().contains(expected)) {
240 error("Mismatch error message '" + ex.getMessage() + "' should contain '" + expected + '"');
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);
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);
262 if (getDebug()) System.out.println();