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 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 TransactionLogTest method testRequiredNoRecovery.
@Test
public void testRequiredNoRecovery() throws Exception {
XAConnection xaConn = dataSource.getXAConnection();
try {
txControl.required(() -> {
txControl.getCurrentContext().registerXAResource(xaConn.getXAResource(), null);
Connection conn = xaConn.getConnection();
return conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
});
} finally {
xaConn.close();
}
try (Connection conn = dataSource.getConnection()) {
ResultSet rs = conn.createStatement().executeQuery("Select * from TEST_TABLE");
rs.next();
assertEquals("Hello World!", rs.getString(1));
assertFalse(rs.next());
}
}
use of javax.sql.XAConnection in project aries by apache.
the class TransactionLogTest method testRequiredWithRollback.
@Test
public void testRequiredWithRollback() throws Exception {
XAConnection xaConn = dataSource.getXAConnection();
try {
txControl.required(() -> {
txControl.getCurrentContext().registerXAResource(xaConn.getXAResource(), null);
Connection conn = xaConn.getConnection();
// conn.setAutoCommit(false);
conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
txControl.setRollbackOnly();
return null;
});
} finally {
xaConn.close();
}
try (Connection conn = dataSource.getConnection()) {
ResultSet rs = conn.createStatement().executeQuery("Select * from TEST_TABLE");
assertFalse(rs.next());
}
}
use of javax.sql.XAConnection in project spring-boot by spring-projects.
the class NarayanaDataSourceBeanTests method shouldGetConnectionAndCommitWithCredentials.
@Test
public void shouldGetConnectionAndCommitWithCredentials() throws SQLException {
String username = "testUsername";
String password = "testPassword";
Connection mockConnection = mock(Connection.class);
XAConnection mockXaConnection = mock(XAConnection.class);
given(mockXaConnection.getConnection()).willReturn(mockConnection);
given(this.dataSource.getXAConnection(username, password)).willReturn(mockXaConnection);
Properties properties = new Properties();
properties.put(TransactionalDriver.XADataSource, this.dataSource);
properties.put(TransactionalDriver.userName, username);
properties.put(TransactionalDriver.password, password);
Connection connection = this.dataSourceBean.getConnection(username, password);
assertThat(connection).isInstanceOf(ConnectionImple.class);
connection.commit();
verify(this.dataSource, times(1)).getXAConnection(username, password);
verify(mockXaConnection, times(1)).getConnection();
verify(mockConnection, times(1)).commit();
}
Aggregations