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
|
/*
* 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 java.lang.reflect.Proxy;
import java.sql.Connection;
import java.util.ArrayList;
import javax.transaction.*;
import org.slf4j.*;
import bitronix.tm.*;
import bitronix.tm.resource.jdbc.JdbcConnectionHandle;
/**
*
* @author lorban
*/
public class JdbcSharedConnectionTest extends AbstractMockJdbcTest {
private final static Logger log = LoggerFactory.getLogger(NewJdbcProperUsageMockTest.class);
public void testSharedConnectionMultithreaded() throws Exception {
if (log.isDebugEnabled()) log.debug("*** getting TM");
final BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager();
tm.setTransactionTimeout(120);
if (log.isDebugEnabled()) log.debug("*** before begin");
tm.begin();
if (log.isDebugEnabled()) log.debug("*** after begin");
final Transaction suspended = tm.suspend();
final ArrayList twoConnections = new ArrayList();
Thread thread1 = new Thread() {
public void run() {
try {
tm.resume(suspended);
if (log.isDebugEnabled()) log.debug("*** getting connection from DS1");
Connection connection = poolingDataSource1.getConnection();
connection.createStatement();
twoConnections.add(connection);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
};
thread1.start();
thread1.join();
Thread thread2 = new Thread() {
public void run() {
try {
tm.resume(suspended);
if (log.isDebugEnabled()) log.debug("*** getting connection from DS1");
Connection connection = poolingDataSource1.getConnection();
connection.createStatement();
twoConnections.add(connection);
tm.commit();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
};
thread2.start();
thread2.join();
JdbcConnectionHandle handle1 = (JdbcConnectionHandle) Proxy.getInvocationHandler(twoConnections.get(0));
JdbcConnectionHandle handle2 = (JdbcConnectionHandle) Proxy.getInvocationHandler(twoConnections.get(1));
assertNotSame(handle1.getConnection(), handle2.getConnection());
}
public void testUnSharedConnection() throws Exception {
if (log.isDebugEnabled()) log.debug("*** getting TM");
BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager();
tm.setTransactionTimeout(120);
if (log.isDebugEnabled()) log.debug("*** before begin");
tm.begin();
if (log.isDebugEnabled()) log.debug("*** after begin");
if (log.isDebugEnabled()) log.debug("*** getting connection from DS2");
Connection connection1 = poolingDataSource2.getConnection();
// createStatement causes enlistment
connection1.createStatement();
if (log.isDebugEnabled()) log.debug("*** getting second connection from DS2");
Connection connection2 = poolingDataSource2.getConnection();
JdbcConnectionHandle handle1 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection1);
JdbcConnectionHandle handle2 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection2);
assertNotSame(handle1.getConnection(), handle2.getConnection());
connection1.close();
connection2.close();
tm.commit();
}
public void testSharedConnectionInLocalTransaction() throws Exception {
if (log.isDebugEnabled()) log.debug("*** getting connection from DS1");
Connection connection1 = poolingDataSource1.getConnection();
// createStatement causes enlistment
connection1.createStatement();
if (log.isDebugEnabled()) log.debug("*** getting second connection from DS1");
Connection connection2 = poolingDataSource1.getConnection();
JdbcConnectionHandle handle1 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection1);
JdbcConnectionHandle handle2 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection2);
assertNotSame(handle1.getConnection(), handle2.getConnection());
connection1.close();
connection2.close();
}
public void testUnSharedConnectionInLocalTransaction() throws Exception {
if (log.isDebugEnabled()) log.debug("*** getting connection from DS2");
Connection connection1 = poolingDataSource2.getConnection();
// createStatement causes enlistment
connection1.createStatement();
if (log.isDebugEnabled()) log.debug("*** getting second connection from DS2");
Connection connection2 = poolingDataSource2.getConnection();
JdbcConnectionHandle handle1 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection1);
JdbcConnectionHandle handle2 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection2);
assertNotSame(handle1.getConnection(), handle2.getConnection());
connection1.close();
connection2.close();
}
public void testSharedConnectionInGlobal() throws Exception {
if (log.isDebugEnabled()) log.debug("*** getting TM");
BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager();
tm.setTransactionTimeout(120);
if (log.isDebugEnabled()) log.debug("*** before begin");
tm.begin();
if (log.isDebugEnabled()) log.debug("*** after begin");
if (log.isDebugEnabled()) log.debug("*** getting connection from DS1");
Connection connection1 = poolingDataSource1.getConnection();
if (log.isDebugEnabled()) log.debug("*** getting second connection from DS1");
Connection connection2 = poolingDataSource1.getConnection();
JdbcConnectionHandle handle1 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection1);
JdbcConnectionHandle handle2 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection2);
assertSame(handle1.getConnection(), handle2.getConnection());
connection1.close();
connection2.close();
tm.commit();
}
}
|