1 22 package org.jboss.test.jca.test; 23 24 import javax.resource.spi.ManagedConnectionFactory ; 25 import javax.transaction.Transaction ; 26 import javax.transaction.TransactionManager ; 27 28 import junit.framework.TestCase; 29 30 import org.jboss.logging.Logger; 31 import org.jboss.resource.connectionmanager.BaseConnectionManager2; 32 import org.jboss.resource.connectionmanager.CachedConnectionManager; 33 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool; 34 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool; 35 import org.jboss.resource.connectionmanager.ManagedConnectionPool; 36 import org.jboss.resource.connectionmanager.NoTxConnectionManager; 37 import org.jboss.resource.connectionmanager.PreFillPoolSupport; 38 import org.jboss.resource.connectionmanager.TransactionSynchronizer; 39 import org.jboss.resource.connectionmanager.TxConnectionManager; 40 import org.jboss.test.jca.adapter.TestConnection; 41 import org.jboss.test.jca.adapter.TestConnectionRequestInfo; 42 import org.jboss.test.jca.adapter.TestManagedConnection; 43 import org.jboss.test.jca.adapter.TestManagedConnectionFactory; 44 import org.jboss.test.jca.support.PoolHelper; 45 import org.jboss.test.jca.support.PoolHelper.PoolType; 46 import org.jboss.tm.TransactionManagerLocator; 47 48 55 public class PoolingUnitTestCase extends TestCase 56 { 57 58 Logger log = Logger.getLogger(getClass()); 59 60 static CachedConnectionManager ccm = new CachedConnectionManager(); 61 static TransactionManager tm = TransactionManagerLocator.getInstance().locate(); 62 static TestConnectionRequestInfo cri1 = new TestConnectionRequestInfo("info1"); 63 static TestConnectionRequestInfo cri2 = new TestConnectionRequestInfo("info2"); 64 65 public PoolingUnitTestCase (String name) 66 { 67 super(name); 68 } 69 70 private ManagedConnectionPool getPool(PoolType type, boolean noTxnSeperatePools, ManagedConnectionFactory mcf, InternalManagedConnectionPool.PoolParams pp){ 71 72 return PoolHelper.getManagedConnectionPool(type, mcf, noTxnSeperatePools, pp, log); 73 } 74 75 private ManagedConnectionPool getOnePool(int maxSize) 76 throws Exception 77 { 78 79 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 80 pp.minSize = 0; 81 pp.maxSize = maxSize; 82 pp.blockingTimeout = 10000; 83 pp.idleTimeout = 0; 84 ManagedConnectionFactory mcf = new TestManagedConnectionFactory(); 85 ManagedConnectionPool poolingStrategy = getPool(PoolHelper.PoolType.ONE_POOL, false, mcf, pp); 86 return poolingStrategy; 87 } 88 89 private ManagedConnectionPool getPoolByCri(int maxSize) 90 throws Exception 91 { 92 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 93 pp.minSize = 0; 94 pp.maxSize = maxSize; 95 pp.blockingTimeout = 10000; 96 pp.idleTimeout = 10000; 97 ManagedConnectionFactory mcf = new TestManagedConnectionFactory(); 98 ManagedConnectionPool poolingStrategy = getPool(PoolType.CRI_POOL, false, mcf, pp); 99 return poolingStrategy; 100 } 101 102 private BaseConnectionManager2 getNoTxCM(ManagedConnectionPool poolingStrategy) 103 { 104 TransactionSynchronizer.setTransactionManager(tm); 105 BaseConnectionManager2 cm = new NoTxConnectionManager(ccm, poolingStrategy); 106 poolingStrategy.setConnectionListenerFactory(cm); 107 return cm; 108 } 109 110 private BaseConnectionManager2 getTxCM(ManagedConnectionPool poolingStrategy) 111 { 112 TransactionSynchronizer.setTransactionManager(tm); 113 BaseConnectionManager2 cm = new TxConnectionManager(ccm, poolingStrategy, tm); 114 poolingStrategy.setConnectionListenerFactory(cm); 115 return cm; 116 } 117 118 private BaseConnectionManager2 getTxTrackCM(ManagedConnectionPool poolingStrategy) 119 { 120 TransactionSynchronizer.setTransactionManager(tm); 121 TxConnectionManager cm = new TxConnectionManager(ccm, poolingStrategy, tm); 122 cm.setTrackConnectionByTx(true); 123 poolingStrategy.setConnectionListenerFactory(cm); 124 return cm; 125 } 126 127 private TestConnection allocate(BaseConnectionManager2 cm, TestConnectionRequestInfo cri) throws Exception 128 { 129 JBossManagedConnectionPool.BasePool mcp = (JBossManagedConnectionPool.BasePool) cm.getPoolingStrategy(); 130 ManagedConnectionFactory mcf = mcp.getManagedConnectionFactory(); 131 return (TestConnection) cm.allocateConnection(mcf, cri); 132 } 133 134 private void shutdown(ManagedConnectionPool mcp) 135 { 136 JBossManagedConnectionPool.BasePool pool = (JBossManagedConnectionPool.BasePool) mcp; 137 pool.shutdown(); 138 } 139 140 protected void setUp() 141 { 142 log.debug("================> Start " + getName()); 143 } 144 145 protected void tearDown() 146 { 147 log.debug("================> End " + getName()); 148 } 149 150 public void testOnePoolNoTx() throws Exception 151 { 152 ManagedConnectionPool mcp = getOnePool(1); 153 BaseConnectionManager2 cm = getNoTxCM(mcp); 154 try 155 { 156 doOnePool(cm); 157 } 158 finally 159 { 160 shutdown(mcp); 161 } 162 } 163 164 public void testOnePoolTx() throws Exception 165 { 166 ManagedConnectionPool mcp = getOnePool(1); 167 BaseConnectionManager2 cm = getTxCM(mcp); 168 try 169 { 170 doOnePool(cm); 172 tm.begin(); 173 doOnePool(cm); 175 tm.commit(); 176 doOnePool(cm); 178 } 179 finally 180 { 181 shutdown(mcp); 182 } 183 } 184 185 public void testOnePoolTxTrack() throws Exception 186 { 187 ManagedConnectionPool mcp = getOnePool(1); 188 BaseConnectionManager2 cm = getTxTrackCM(mcp); 189 try 190 { 191 doOnePool(cm); 193 tm.begin(); 194 doOnePool(cm); 196 tm.commit(); 197 doOnePool(cm); 199 } 200 finally 201 { 202 shutdown(mcp); 203 } 204 } 205 206 public void testTrackConnectionByTx() throws Exception 207 { 208 ManagedConnectionPool mcp = getOnePool(2); 209 BaseConnectionManager2 cm = getTxTrackCM(mcp); 210 try 211 { 212 tm.begin(); 213 TestConnection c1 = allocate(cm, cri1); 214 TestManagedConnection mc1 = c1.getMC(); 215 c1.close(); 216 TestConnection c2 = allocate(cm, cri1); 217 TestManagedConnection mc2 = c2.getMC(); 218 c2.close(); 219 assertTrue("Connections should be equal in same transaction", mc1.equals(mc2)); 220 Transaction tx1 = tm.suspend(); 221 tm.begin(); 222 c2 = allocate(cm, cri1); 223 mc2 = c2.getMC(); 224 c2.close(); 225 assertTrue("Connections should not be equal in a different transaction", mc1.equals(mc2) == false); 226 tm.commit(); 227 c2 = allocate(cm, cri1); 228 mc2 = c2.getMC(); 229 c2.close(); 230 assertTrue("Connections should not be equal outside a transaction", mc1.equals(mc2) == false); 231 tm.resume(tx1); 232 c2 = allocate(cm, cri1); 233 mc2 = c2.getMC(); 234 c2.close(); 235 assertTrue("Connections should still be equal in same transaction", mc1.equals(mc2)); 236 tm.commit(); 237 assertTrue("All connections should be recycled", mcp.getAvailableConnectionCount() == 2); 238 } 239 finally 240 { 241 shutdown(mcp); 242 } 243 } 244 245 public void testTrackConnectionByTxAndCRI() throws Exception 246 { 247 ManagedConnectionPool mcp = getPoolByCri(2); 248 BaseConnectionManager2 cm = getTxTrackCM(mcp); 249 try 250 { 251 tm.begin(); 252 TestConnection c1 = allocate(cm, cri1); 253 TestManagedConnection mc1 = c1.getMC(); 254 c1.close(); 255 TestConnection c2 = allocate(cm, cri1); 256 TestManagedConnection mc2 = c2.getMC(); 257 c2.close(); 258 assertTrue("Connections should be equal in same transaction and criteria", mc1.equals(mc2)); 259 c2 = allocate(cm, cri2); 260 mc2 = c2.getMC(); 261 c2.close(); 262 assertTrue("Connections should not be equal in same transaction but different criteria", mc1.equals(mc2) == false); 263 tm.commit(); 264 } 265 finally 266 { 267 shutdown(mcp); 268 } 269 } 270 271 public void doOnePool(BaseConnectionManager2 cm) throws Exception 272 { 273 TestConnection c1 = allocate(cm, cri1); 274 TestManagedConnection mc1 = c1.getMC(); 275 c1.close(); 276 TestConnection c2 = allocate(cm, cri1); 277 TestManagedConnection mc2 = c2.getMC(); 278 c2.close(); 279 assertEquals("Should get the same connection for same criteria", mc1, mc2); 280 c2 = allocate(cm, cri2); 281 mc2 = c2.getMC(); 282 c2.close(); 283 assertEquals("Should get the same connection for different cri", mc1, mc2); 284 } 285 286 287 288 289 290 } 291 | Popular Tags |