Java Code Examples for org.apache.shardingsphere.transaction.core.TransactionTypeHolder#clear()

The following examples show how to use org.apache.shardingsphere.transaction.core.TransactionTypeHolder#clear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OrderServiceImpl.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@Override
public void processSuccess() throws SQLException {
    System.out.println("-------------------- Process Start ---------------------");
    TransactionTypeHolder.set(TransactionType.XA);
    try (Connection connection = dataSource.getConnection()) {
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
        doInsert(preparedStatement);
        connection.commit();
        System.out.println("INSERT 10 orders success");
    } finally {
        TransactionTypeHolder.clear();
    }
    int quantity = selectAll();
    System.out.printf("Commit, expect:10, actual:%d \n", quantity);
    printData();
    System.out.println("-------------------- Process End -----------------------");
}
 
Example 2
Source File: OrderServiceImpl.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@Override
public void processFailure() throws SQLException {
    System.out.println("-------------------- Process Start ---------------------");
    TransactionTypeHolder.set(TransactionType.XA);
    try (Connection connection = dataSource.getConnection()) {
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
        doInsert(preparedStatement);
        connection.rollback();
        System.out.println("INSERT 10 orders failed");
    } finally {
        TransactionTypeHolder.clear();
    }
    int quantity = selectAll();
    System.out.printf("Rollback, expect:0, actual:%d \n", quantity);
    printData();
    System.out.println("-------------------- Process End -----------------------");
}
 
Example 3
Source File: ConnectionAdapterTest.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@Test
public void assertShardingTransactionAutoCommit() throws SQLException {
    TransactionTypeHolder.set(TransactionType.XA);
    try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
        actual.createStatement().executeQuery(sql);
        actual.setAutoCommit(false);
        actual.createStatement().executeQuery(sql);
        assertTrue(actual.getShardingTransactionManager().isInTransaction());
        Multimap<String, Connection> cachedConnections = getCachedConnections(actual);
        assertThat(cachedConnections.size(), is(1));
        for (Connection each : cachedConnections.values()) {
            assertTrue(each.getAutoCommit());
        }
    } finally {
        TransactionTypeHolder.clear();
    }
}
 
Example 4
Source File: ConnectionAdapterTest.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@Test
public void assertShardingTransactionSkipAutoCommit() throws SQLException {
    TransactionTypeHolder.set(TransactionType.XA);
    try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
        actual.setAutoCommit(true);
        assertFalse(actual.getShardingTransactionManager().isInTransaction());
    } finally {
        TransactionTypeHolder.clear();
    }
    TransactionTypeHolder.set(TransactionType.XA);
    try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
        actual.setAutoCommit(false);
        assertTrue(actual.getShardingTransactionManager().isInTransaction());
        assertThat(XAShardingTransactionManagerFixture.getInvocations().size(), is(1));
        actual.setAutoCommit(false);
        assertThat(XAShardingTransactionManagerFixture.getInvocations().size(), is(1));
    } finally {
        TransactionTypeHolder.clear();
    }
}
 
Example 5
Source File: SeataATOrderService.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Execute XA.
 *
 * @throws SQLException SQL exception
 */
void insert() throws SQLException {
    TransactionTypeHolder.set(TransactionType.BASE);
    try (Connection connection = dataSource.getConnection()) {
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
        doInsert(preparedStatement);
        connection.commit();
    } finally {
        TransactionTypeHolder.clear();
    }
}
 
Example 6
Source File: SeataATOrderService.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Execute XA with exception.
 *
 * @throws SQLException SQL exception
 */
void insertFailed() throws SQLException {
    TransactionTypeHolder.set(TransactionType.BASE);
    try (Connection connection = dataSource.getConnection()) {
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
        doInsert(preparedStatement);
        connection.rollback();
    } finally {
        TransactionTypeHolder.clear();
    }
}
 
Example 7
Source File: XAOrderService.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Execute XA.
 *
 * @throws SQLException SQL exception
 */
void insert() throws SQLException {
    TransactionTypeHolder.set(TransactionType.XA);
    try (Connection connection = dataSource.getConnection()) {
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
        doInsert(preparedStatement);
        connection.commit();
    } finally {
        TransactionTypeHolder.clear();
    }
}
 
Example 8
Source File: XAOrderService.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Execute XA with exception.
 *
 * @throws SQLException SQL exception
 */
void insertFailed() throws SQLException {
    TransactionTypeHolder.set(TransactionType.XA);
    try (Connection connection = dataSource.getConnection()) {
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
        doInsert(preparedStatement);
        connection.rollback();
    } finally {
        TransactionTypeHolder.clear();
    }
}
 
Example 9
Source File: ConnectionAdapterTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertShardingTransactionCommit() throws SQLException {
    TransactionTypeHolder.set(TransactionType.XA);
    try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
        actual.commit();
        assertTrue(XAShardingTransactionManagerFixture.getInvocations().contains(TransactionOperationType.COMMIT));
    } finally {
        TransactionTypeHolder.clear();
    }
}
 
Example 10
Source File: ConnectionAdapterTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertShardingTransactionForceCommit() throws SQLException {
    TransactionTypeHolder.set(TransactionType.XA);
    try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
        actual.setAutoCommit(false);
        actual.setAutoCommit(true);
        assertTrue(XAShardingTransactionManagerFixture.getInvocations().contains(TransactionOperationType.COMMIT));
    } finally {
        TransactionTypeHolder.clear();
    }
}
 
Example 11
Source File: ConnectionAdapterTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertShardingTransactionRollback() throws SQLException {
    TransactionTypeHolder.set(TransactionType.XA);
    try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
        actual.rollback();
        assertTrue(XAShardingTransactionManagerFixture.getInvocations().contains(TransactionOperationType.ROLLBACK));
    } finally {
        TransactionTypeHolder.clear();
    }
}
 
Example 12
Source File: ShardingSphereConnectionTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@After
public void clear() {
    try {
        connection.close();
        TransactionTypeHolder.clear();
        XAShardingTransactionManagerFixture.getInvocations().clear();
        BASEShardingTransactionManagerFixture.getInvocations().clear();
    } catch (final SQLException ignored) {
    }
}
 
Example 13
Source File: ShardingTransactionTypeInterceptor.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
    ShardingTransactionType shardingTransactionType = getAnnotation(methodInvocation);
    Objects.requireNonNull(shardingTransactionType, "could not found sharding transaction type annotation");
    TransactionType preTransactionType = TransactionTypeHolder.get();
    TransactionTypeHolder.set(shardingTransactionType.value());
    try {
        return methodInvocation.proceed();
    } finally {
        TransactionTypeHolder.clear();
        if (null != preTransactionType) {
            TransactionTypeHolder.set(preTransactionType);
        }
    }
}
 
Example 14
Source File: ConnectionAdapterTest.java    From shardingsphere with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    TransactionTypeHolder.clear();
    XAShardingTransactionManagerFixture.getInvocations().clear();
    BASEShardingTransactionManagerFixture.getInvocations().clear();
}
 
Example 15
Source File: ShardingTransactionJDBCAspect.java    From opensharding-spi-impl with Apache License 2.0 2 votes vote down vote up
/**
 * Clean transaction type after transaction commit/rollback.
 *
 * @param joinPoint join point
 */
@After(value = "shardingTransactionalJDBCPointCut()")
public void cleanTransactionTypeAfterTransaction(final JoinPoint joinPoint) {
    TransactionTypeHolder.clear();
}