Search in sources :

Example 1 with XAConnection

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);
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 2 with XAConnection

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);
}
Also used : TransactionControl(org.osgi.service.transaction.control.TransactionControl) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 3 with XAConnection

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());
    }
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 4 with XAConnection

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());
    }
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 5 with XAConnection

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();
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) Properties(java.util.Properties) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Aggregations

XAConnection (javax.sql.XAConnection)116 Connection (java.sql.Connection)78 XAResource (javax.transaction.xa.XAResource)57 XADataSource (javax.sql.XADataSource)52 Xid (javax.transaction.xa.Xid)44 Statement (java.sql.Statement)34 SQLException (java.sql.SQLException)31 PreparedStatement (java.sql.PreparedStatement)23 PooledConnection (javax.sql.PooledConnection)23 ResultSet (java.sql.ResultSet)22 CallableStatement (java.sql.CallableStatement)18 Test (org.junit.Test)18 XAException (javax.transaction.xa.XAException)16 Transaction (javax.transaction.Transaction)8 DataSource (javax.sql.DataSource)7 XidImpl (org.firebirdsql.jca.TestXABase.XidImpl)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 NotSupportedException (javax.transaction.NotSupportedException)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 J2EEDataSource (org.apache.derbyTesting.junit.J2EEDataSource)4