use of javax.sql.XAConnection in project aries by apache.
the class XATxContextBindingEntityManagerTest method testActiveTransactionWithPreviousCommonTxControl.
@Test
public void testActiveTransactionWithPreviousCommonTxControl() throws SQLException {
TransactionControl previous = Mockito.mock(TransactionControl.class);
Connection con = Mockito.mock(Connection.class, withSettings().extraInterfaces(XAConnection.class));
Mockito.when(((XAConnection) con).getXAResource()).thenReturn(xaResource);
Mockito.when(rawEm.unwrap(Connection.class)).thenReturn(con);
setupActiveTransaction();
commonTxControl.set(previous);
em.isOpen();
em.isOpen();
Mockito.verify(rawEm, times(2)).isOpen();
Mockito.verify(rawEm).joinTransaction();
checkPostCompletion(previous);
}
use of javax.sql.XAConnection in project aries by apache.
the class XATxContextBindingEntityManagerTest method testActiveTransactionUnwrappableXAConnection.
@Test
public void testActiveTransactionUnwrappableXAConnection() throws SQLException {
XAConnection xaCon = Mockito.mock(XAConnection.class);
Mockito.when(xaCon.getXAResource()).thenReturn(xaResource);
Connection con = Mockito.mock(Connection.class);
Mockito.when(con.unwrap(XAConnection.class)).thenReturn(xaCon);
Mockito.when(con.isWrapperFor(XAConnection.class)).thenReturn(true);
Mockito.when(rawEm.unwrap(Connection.class)).thenReturn(con);
setupActiveTransaction();
em.isOpen();
em.isOpen();
Mockito.verify(rawEm, times(2)).isOpen();
Mockito.verify(rawEm).joinTransaction();
checkPostCompletion(null);
}
use of javax.sql.XAConnection in project geode by apache.
the class GemFireTransactionDataSource method getConnection.
/**
* Implementation of datasource function. This method is used to get the connection from the pool.
* Default user name and password will be used.
*
* @throws SQLException
* @return ???
*/
@Override
public Connection getConnection() throws SQLException {
if (!isActive) {
throw new SQLException(LocalizedStrings.GemFireTransactionDataSource_GEMFIRETRANSACTIONDATASOURCEGETCONNECTIONNO_VALID_CONNECTION_AVAILABLE.toLocalizedString());
}
XAConnection xaConn = null;
try {
xaConn = (XAConnection) provider.borrowConnection();
Connection conn = getSQLConnection(xaConn);
registerTranxConnection(xaConn);
return conn;
} catch (Exception ex) {
SQLException se = new SQLException(ex.getMessage());
se.initCause(ex);
throw se;
}
}
use of javax.sql.XAConnection in project tomee by apache.
the class ManagedConnection method invoke.
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
// first some Object method management
final String mtdName = method.getName();
if ("toString".equals(mtdName)) {
return "ManagedConnection{" + delegate + "}";
}
if ("hashCode".equals(mtdName)) {
return hashCode();
}
if ("equals".equals(mtdName)) {
InvocationHandler handler;
return args[0] == this || ((handler = unwrapHandler(args[0])) == this) || (delegate != null && delegate.equals(unwrapDelegate(args[0], handler)));
}
// allow to get delegate if needed by the underlying program
if (Wrapper.class == method.getDeclaringClass() && args.length == 1 && Connection.class == args[0]) {
if ("isWrapperFor".equals(mtdName)) {
return true;
}
if ("unwrap".equals(mtdName)) {
return delegate;
}
}
// here the real logic starts
try {
final Transaction transaction = transactionManager.getTransaction();
// shouldn't be used without a transaction but if so just delegate to the actual connection
if (transaction == null) {
if ("close".equals(mtdName)) {
if (delegate == null) {
// no need to get a connection
return close();
}
closeConnection(true);
return null;
}
if ("isClosed".equals(mtdName) && closed) {
return true;
}
if (delegate == null) {
newConnection();
}
return invoke(method, delegate, args);
}
// if we have a tx check it is the same this connection is linked to
if (currentTransaction != null && isUnderTransaction(currentTransaction.getStatus())) {
if (!currentTransaction.equals(transaction)) {
throw new SQLException("Connection can not be used while enlisted in another transaction");
}
return invokeUnderTransaction(method, args);
}
// get the already bound connection to the current transaction or enlist this one in the tx
final int transactionStatus = transaction.getStatus();
if (isUnderTransaction(transactionStatus)) {
Connection connection = Connection.class.cast(registry.getResource(key));
if (connection == null && delegate == null) {
newConnection();
currentTransaction = transaction;
try {
if (!transaction.enlistResource(getXAResource())) {
closeConnection(true);
throw new SQLException("Unable to enlist connection in transaction: enlistResource returns 'false'.");
}
} catch (final RollbackException ignored) {
// no-op
} catch (final SystemException e) {
throw new SQLException("Unable to enlist connection the transaction", e);
}
registry.putResource(key, delegate);
transaction.registerSynchronization(new ClosingSynchronization());
if (xaConnection == null) {
try {
setAutoCommit(false);
} catch (final SQLException xae) {
// we are alreay in a transaction so this can't be called from a user perspective - some XA DataSource prevents it in their code
final String message = "Can't set auto commit to false cause the XA datasource doesn't support it, this is likely an issue";
final Logger logger = Logger.getInstance(LogCategory.OPENEJB_RESOURCE_JDBC, ManagedConnection.class);
if (logger.isDebugEnabled()) {
// we don't want to print the exception by default
logger.warning(message, xae);
} else {
logger.warning(message);
}
}
}
} else if (delegate == null) {
// shouldn't happen
delegate = connection;
}
return invokeUnderTransaction(method, args);
}
if ("isClosed".equals(mtdName) && closed) {
return true;
}
if ("close".equals(mtdName)) {
// let it be handled by the ClosingSynchronisation since we have a tx there
return close();
}
// we shouldn't come here, tempted to just throw an exception
if (delegate == null) {
newConnection();
}
return invoke(method, delegate, args);
} catch (final InvocationTargetException ite) {
throw ite.getTargetException();
}
}
use of javax.sql.XAConnection in project druid by alibaba.
the class MySqlUtils method createXAConnection.
public static XAConnection createXAConnection(Driver driver, Connection physicalConn) throws SQLException {
final int major = driver.getMajorVersion();
if (major == 5) {
if (utilClass == null && !utilClassError) {
try {
utilClass = Class.forName("com.mysql.jdbc.Util");
Method method = utilClass.getMethod("isJdbc4");
utilClass_isJdbc4 = (Boolean) method.invoke(null);
connectionClass = Class.forName("com.mysql.jdbc.Connection");
getPinGlobalTxToPhysicalConnectionMethod = connectionClass.getMethod("getPinGlobalTxToPhysicalConnection");
suspendableXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.SuspendableXAConnection");
suspendableXAConnectionConstructor = suspendableXAConnectionClass.getConstructor(connectionClass);
JDBC4SuspendableXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.JDBC4SuspendableXAConnection");
JDBC4SuspendableXAConnectionConstructor = JDBC4SuspendableXAConnectionClass.getConstructor(connectionClass);
MysqlXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.MysqlXAConnection");
MysqlXAConnectionConstructor = MysqlXAConnectionClass.getConstructor(connectionClass, boolean.class);
} catch (Exception ex) {
ex.printStackTrace();
utilClassError = true;
}
}
try {
boolean pinGlobTx = (Boolean) getPinGlobalTxToPhysicalConnectionMethod.invoke(physicalConn);
if (pinGlobTx) {
if (!utilClass_isJdbc4) {
return (XAConnection) suspendableXAConnectionConstructor.newInstance(physicalConn);
}
return (XAConnection) JDBC4SuspendableXAConnectionConstructor.newInstance(physicalConn);
}
return (XAConnection) MysqlXAConnectionConstructor.newInstance(physicalConn, Boolean.FALSE);
} catch (Exception e) {
e.printStackTrace();
}
}
throw new SQLFeatureNotSupportedException();
}
Aggregations