1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
/*
* Bitronix Transaction Manager
*
* Copyright (c) 2010, Bitronix Software.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package bitronix.tm.mock;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.mock.resource.jdbc.MockitoXADataSource;
import bitronix.tm.recovery.RecoveryException;
import bitronix.tm.resource.ResourceConfigurationException;
import bitronix.tm.resource.common.XAPool;
import bitronix.tm.resource.jdbc.PoolingDataSource;
import junit.framework.TestCase;
import javax.sql.DataSource;
import javax.sql.XADataSource;
import javax.transaction.TransactionManager;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author lorban
*/
public class JdbcPoolTest extends TestCase {
private PoolingDataSource pds;
protected void setUp() throws Exception {
TransactionManagerServices.getConfiguration().setJournal("null").setGracefulShutdownInterval(0);
TransactionManagerServices.getTransactionManager();
MockitoXADataSource.setStaticCloseXAConnectionException(null);
MockitoXADataSource.setStaticGetXAConnectionException(null);
pds = new PoolingDataSource();
pds.setMinPoolSize(1);
pds.setMaxPoolSize(2);
pds.setMaxIdleTime(1);
pds.setClassName(MockitoXADataSource.class.getName());
pds.setUniqueName("pds");
pds.setAllowLocalTransactions(true);
pds.setAcquisitionTimeout(1);
pds.init();
}
protected void tearDown() throws Exception {
pds.close();
TransactionManagerServices.getTransactionManager().shutdown();
}
public void testObjectProperties() throws Exception {
pds.close();
pds = new PoolingDataSource();
pds.setUniqueName("pds");
pds.setClassName(MockitoXADataSource.class.getName());
pds.setMinPoolSize(1);
pds.setMaxPoolSize(1);
pds.getDriverProperties().put("uselessThing", new Object());
pds.init();
}
public void testInitFailure() throws Exception {
pds.close();
pds = new PoolingDataSource();
pds.setMinPoolSize(0);
pds.setMaxPoolSize(2);
pds.setMaxIdleTime(1);
pds.setClassName(MockitoXADataSource.class.getName());
pds.setUniqueName("pds");
pds.setAllowLocalTransactions(true);
pds.setAcquisitionTimeout(1);
TransactionManagerServices.getTransactionManager().begin();
MockitoXADataSource.setStaticGetXAConnectionException(new SQLException("not yet started"));
try {
pds.init();
fail("expected ResourceConfigurationException");
} catch (ResourceConfigurationException ex) {
Throwable rootCause = ex.getCause().getCause();
assertEquals(SQLException.class, rootCause.getClass());
assertEquals("not yet started", rootCause.getMessage());
}
MockitoXADataSource.setStaticGetXAConnectionException(null);
pds.init();
pds.getConnection().prepareStatement("");
TransactionManagerServices.getTransactionManager().commit();
}
public void testReEnteringRecovery() throws Exception {
pds.startRecovery();
try {
pds.startRecovery();
fail("expected RecoveryException");
} catch (RecoveryException ex) {
assertEquals("recovery already in progress on a PoolingDataSource containing an XAPool of resource pds with 1 connection(s) (0 still available)", ex.getMessage());
}
// make sure startRecovery() can be called again once endRecovery() has been called
pds.endRecovery();
pds.startRecovery();
pds.endRecovery();
}
public void testPoolGrowth() throws Exception {
Field poolField = pds.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
XAPool pool = (XAPool) poolField.get(pds);
assertEquals(1, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c1 = pds.getConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c2 = pds.getConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(2, pool.totalPoolSize());
try {
pds.getConnection();
fail("should not be able to get a 3rd connection");
} catch (SQLException ex) {
assertEquals("unable to get a connection from pool of a PoolingDataSource containing an XAPool of resource pds with 2 connection(s) (0 still available)", ex.getMessage());
}
c1.close();
c2.close();
assertEquals(2, pool.inPoolSize());
assertEquals(2, pool.totalPoolSize());
}
public void testPoolShrink() throws Exception {
Field poolField = pds.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
XAPool pool = (XAPool) poolField.get(pds);
assertEquals(1, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c1 = pds.getConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c2 = pds.getConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(2, pool.totalPoolSize());
c1.close();
c2.close();
Thread.sleep(1100); // leave enough time for the ide connections to expire
TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
Thread.sleep(1100); // leave enough time for the scheduled shrinking task to do its work
assertEquals(1, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
}
public void testPoolShrinkErrorHandling() throws Exception {
Field poolField = pds.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
XAPool pool = (XAPool) poolField.get(pds);
pds.setMinPoolSize(0);
pds.reset();
pds.setMinPoolSize(1);
MockitoXADataSource.setStaticCloseXAConnectionException(new SQLException("close fails because datasource broken"));
pds.reset();
// the pool is now loaded with one connection which will throw an exception when closed
Thread.sleep(1100); // leave enough time for the ide connections to expire
TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
Thread.sleep(100); // leave enough time for the scheduled shrinking task to do its work
assertEquals(1, pool.inPoolSize());
MockitoXADataSource.setStaticGetXAConnectionException(new SQLException("getXAConnection fails because datasource broken"));
Thread.sleep(1100); // leave enough time for the ide connections to expire
TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
Thread.sleep(100); // leave enough time for the scheduled shrinking task to do its work
assertEquals(0, pool.inPoolSize());
MockitoXADataSource.setStaticGetXAConnectionException(null);
Thread.sleep(1100); // leave enough time for the ide connections to expire
TransactionManagerServices.getTaskScheduler().interrupt(); // wake up the task scheduler
Thread.sleep(100); // leave enough time for the scheduled shrinking task to do its work
assertEquals(1, pool.inPoolSize());
}
public void testPoolReset() throws Exception {
Field poolField = pds.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
XAPool pool = (XAPool) poolField.get(pds);
assertEquals(1, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c1 = pds.getConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
Connection c2 = pds.getConnection();
assertEquals(0, pool.inPoolSize());
assertEquals(2, pool.totalPoolSize());
c1.close();
c2.close();
pds.reset();
assertEquals(1, pool.inPoolSize());
assertEquals(1, pool.totalPoolSize());
}
public void testPoolResetErrorHandling() throws Exception {
Field poolField = pds.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
XAPool pool = (XAPool) poolField.get(pds);
pds.setMinPoolSize(0);
pds.reset();
pds.setMinPoolSize(1);
MockitoXADataSource.setStaticCloseXAConnectionException(new SQLException("close fails because datasource broken"));
pds.reset();
// the pool is now loaded with one connection which will throw an exception when closed
pds.reset();
try {
MockitoXADataSource.setStaticGetXAConnectionException(new SQLException("getXAConnection fails because datasource broken"));
pds.reset();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("getXAConnection fails because datasource broken", ex.getMessage());
assertEquals(0, pool.inPoolSize());
}
MockitoXADataSource.setStaticGetXAConnectionException(null);
pds.reset();
assertEquals(1, pool.inPoolSize());
}
public void testCloseLocalContext() throws Exception {
Connection c = pds.getConnection();
Statement stmt = c.createStatement();
stmt.close();
c.close();
assertTrue(c.isClosed());
try {
c.createStatement();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("connection handle already closed", ex.getMessage());
}
}
public void testCloseGlobalContextRecycle() throws Exception {
TransactionManager tm = TransactionManagerServices.getTransactionManager();
tm.begin();
Connection c1 = pds.getConnection();
c1.createStatement();
c1.close();
assertTrue(c1.isClosed());
try {
c1.createStatement();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("connection handle already closed", ex.getMessage());
}
Connection c2 = pds.getConnection();
c2.createStatement();
try {
c2.commit();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("cannot commit a resource enlisted in a global transaction", ex.getMessage());
}
tm.commit();
assertFalse(c2.isClosed());
c2.close();
assertTrue(c2.isClosed());
try {
c2.createStatement();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("connection handle already closed", ex.getMessage());
}
try {
c2.commit();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("connection handle already closed", ex.getMessage());
}
}
public void testCloseGlobalContextNoRecycle() throws Exception {
TransactionManager tm = TransactionManagerServices.getTransactionManager();
tm.begin();
Connection c1 = pds.getConnection();
Connection c2 = pds.getConnection();
c1.createStatement();
c1.close();
assertTrue(c1.isClosed());
try {
c1.createStatement();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("connection handle already closed", ex.getMessage());
}
c2.createStatement();
try {
c2.commit();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("cannot commit a resource enlisted in a global transaction", ex.getMessage());
}
tm.commit();
assertFalse(c2.isClosed());
c2.close();
assertTrue(c2.isClosed());
try {
c2.createStatement();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("connection handle already closed", ex.getMessage());
}
try {
c2.commit();
fail("expected SQLException");
} catch (SQLException ex) {
assertEquals("connection handle already closed", ex.getMessage());
}
}
public void testPoolNotStartingTransactionManager() throws Exception {
// make sure TM is not running
TransactionManagerServices.getTransactionManager().shutdown();
PoolingDataSource pds = new PoolingDataSource();
pds.setMinPoolSize(1);
pds.setMaxPoolSize(2);
pds.setMaxIdleTime(1);
pds.setClassName(MockitoXADataSource.class.getName());
pds.setUniqueName("pds2");
pds.setAllowLocalTransactions(true);
pds.setAcquisitionTimeout(1);
pds.init();
assertFalse(TransactionManagerServices.isTransactionManagerRunning());
Connection c = pds.getConnection();
Statement stmt = c.createStatement();
stmt.close();
c.close();
assertFalse(TransactionManagerServices.isTransactionManagerRunning());
pds.close();
assertFalse(TransactionManagerServices.isTransactionManagerRunning());
}
public void testWrappers() throws Exception {
try {
Connection.class.getMethod("isWrapperFor");
} catch (NoSuchMethodException e) {
// if there is no such method this means the JVM is running with pre-JDBC4 classes
// so this test becomes pointless and must be skipped
return;
}
// XADataSource
assertTrue(pds.isWrapperFor(XADataSource.class));
assertFalse(pds.isWrapperFor(DataSource.class));
XADataSource unwrappedXads = (XADataSource) pds.unwrap(XADataSource.class);
assertEquals(MockitoXADataSource.class.getName(), unwrappedXads.getClass().getName());
// Connection
Connection c = pds.getConnection();
assertTrue(isWrapperFor(c, Connection.class));
Connection unwrappedConnection = (Connection) unwrap(c, Connection.class);
assertTrue(unwrappedConnection.getClass().getName().contains("java.sql.Connection") && unwrappedConnection.getClass().getName().contains("EnhancerByMockito"));
// Statement
Statement stmt = c.createStatement();
assertTrue(isWrapperFor(stmt, Statement.class));
Statement unwrappedStmt = (Statement) unwrap(stmt, Statement.class);
assertTrue(unwrappedStmt.getClass().getName().contains("java.sql.Statement") && unwrappedStmt.getClass().getName().contains("EnhancerByMockito"));
// PreparedStatement
PreparedStatement pstmt = c.prepareStatement("mock sql");
assertTrue(isWrapperFor(pstmt, PreparedStatement.class));
Statement unwrappedPStmt = (Statement) unwrap(pstmt, PreparedStatement.class);
assertTrue(unwrappedPStmt.getClass().getName().contains("java.sql.PreparedStatement") && unwrappedPStmt.getClass().getName().contains("EnhancerByMockito"));
// CallableStatement
CallableStatement cstmt = c.prepareCall("mock stored proc");
assertTrue(isWrapperFor(cstmt, CallableStatement.class));
Statement unwrappedCStmt = (Statement) unwrap(cstmt, CallableStatement.class);
assertTrue(unwrappedCStmt.getClass().getName().contains("java.sql.CallableStatement") && unwrappedCStmt.getClass().getName().contains("EnhancerByMockito"));
}
private static boolean isWrapperFor(Object obj, Class param) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method isWrapperForMethod = obj.getClass().getMethod("isWrapperFor", Class.class);
return (Boolean) isWrapperForMethod.invoke(obj, param);
}
private static Object unwrap(Object obj, Class param) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method unwrapMethod = obj.getClass().getMethod("unwrap", Class.class);
return unwrapMethod.invoke(obj, param);
}
}
|