org.apache.shardingsphere.transaction.core.TransactionTypeHolder Java Examples

The following examples show how to use org.apache.shardingsphere.transaction.core.TransactionTypeHolder. 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: 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 #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: 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 #4
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 #5
Source File: ShardingSphereDataSourceTest.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@Test
public void assertGetXaConnectionThenGetLocalConnection() throws SQLException {
    DataSource dataSource = mockDataSource(DatabaseTypes.getActualDatabaseType("MySQL"));
    Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
    dataSourceMap.put("ds", dataSource);
    TransactionTypeHolder.set(TransactionType.XA);
    ShardingSphereDataSource shardingSphereDataSource = createShardingSphereDataSource(dataSourceMap);
    ShardingSphereConnection connection = shardingSphereDataSource.getConnection();
    assertThat(connection.getDataSourceMap().size(), is(1));
    assertThat(connection.getTransactionType(), is(TransactionType.XA));
    assertThat(connection.getShardingTransactionManager(), instanceOf(XAShardingTransactionManagerFixture.class));
    TransactionTypeHolder.set(TransactionType.LOCAL);
    connection = shardingSphereDataSource.getConnection();
    assertThat(connection.getConnection("ds"), is(dataSource.getConnection()));
    assertThat(connection.getDataSourceMap(), is(dataSourceMap));
    assertThat(connection.getTransactionType(), is(TransactionType.LOCAL));
    assertThat(connection.getShardingTransactionManager() == null, is(true));
}
 
Example #6
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 #7
Source File: ShardingTransactionTypeScannerTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertShardingTransactionType() {
    TransactionType preTransactionType = TransactionTypeHolder.get();
    mockService.executeLocal();
    assertThat(TransactionTypeHolder.get(), is(preTransactionType));
    mockService.executeBase();
    assertThat(TransactionTypeHolder.get(), is(preTransactionType));
    mockService.execute();
    assertThat(TransactionTypeHolder.get(), is(preTransactionType));
}
 
Example #8
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 #9
Source File: XAOrderService.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Execute XA.
 *
 * @param count insert record count
 * @return transaction type
 */
@Transactional
@ShardingTransactionType(TransactionType.XA)
public TransactionType insert(final int count) {
    return jdbcTemplate.execute("INSERT INTO t_order (user_id, status) VALUES (?, ?)", (PreparedStatementCallback<TransactionType>) preparedStatement -> {
        doInsert(count, preparedStatement);
        return TransactionTypeHolder.get();
    });
}
 
Example #10
Source File: BusinessServiceImpl.java    From seata-samples with Apache License 2.0 5 votes vote down vote up
@Override
@GlobalTransactional(name = "dubbo-purchase")
public void purchase(){
    TransactionTypeHolder.set(TransactionType.BASE);
    OrderEntity orderEntity = new OrderEntity();
    orderEntity.setOrderId(123);
    orderEntity.setStatus("seata");
    orderEntity.setUserId(123);
    orderService.insertOrder(orderEntity);
    System.out.println("XID:"+ RootContext.getXID());
    throw new RuntimeException("回滚测试");
}
 
Example #11
Source File: XAOrderService.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Execute XA.
 *
 * @param count insert record count
 * @return transaction type
 */
@Transactional
@ShardingTransactionType(TransactionType.XA)
public TransactionType insert(final int count) {
    return jdbcTemplate.execute("INSERT INTO t_order (user_id, status) VALUES (?, ?)", (PreparedStatementCallback<TransactionType>) preparedStatement -> {
        doInsert(count, preparedStatement);
        return TransactionTypeHolder.get();
    });
}
 
Example #12
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 #13
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 #14
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 #15
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 #16
Source File: SeataATOrderService.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Execute XA.
 *
 * @param count insert record count
 * @return transaction type
 */
@Transactional
@ShardingTransactionType(TransactionType.BASE)
public TransactionType insert(final int count) {
    return jdbcTemplate.execute("INSERT INTO t_order (user_id, status) VALUES (?, ?)", (PreparedStatementCallback<TransactionType>) preparedStatement -> {
        doInsert(count, preparedStatement);
        return TransactionTypeHolder.get();
    });
}
 
Example #17
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 #18
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 #19
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 #20
Source File: ConnectionAdapterTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertCloseShouldNotClearTransactionType() throws SQLException {
    TransactionTypeHolder.set(TransactionType.XA);
    TransactionType currentTransactionType = TransactionTypeHolder.get();
    try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
        actual.createStatement().executeQuery(sql);
    }
    assertThat(TransactionTypeHolder.get(), is(currentTransactionType));
}
 
Example #21
Source File: ShardingSphereDataSourceTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertGetXaConnection() throws SQLException {
    DataSource dataSource = mockDataSource(DatabaseTypes.getActualDatabaseType("MySQL"));
    Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
    dataSourceMap.put("ds", dataSource);
    TransactionTypeHolder.set(TransactionType.XA);
    ShardingSphereDataSource shardingSphereDataSource = createShardingSphereDataSource(dataSourceMap);
    assertThat(shardingSphereDataSource.getDataSourceMap().size(), is(1));
    ShardingSphereConnection connection = shardingSphereDataSource.getConnection();
    assertThat(connection.getDataSourceMap().size(), is(1));
}
 
Example #22
Source File: ShardingTransactionalSpringBootTest.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@Test
public void assertChangeTransactionTypeInClass() {
    testService.testChangeTransactionTypeInClass();
    assertThat(TransactionTypeHolder.get(), is(TransactionType.LOCAL));
}
 
Example #23
Source File: ShardingTransactionalTestService.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@ShardingTransactionType
public void testChangeTransactionTypeToLOCAL() {
    assertThat(TransactionTypeHolder.get(), is(TransactionType.LOCAL));
}
 
Example #24
Source File: ShardingTransactionalTestService.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@ShardingTransactionType(TransactionType.XA)
public void testChangeTransactionTypeToXA() {
    assertThat(TransactionTypeHolder.get(), is(TransactionType.XA));
}
 
Example #25
Source File: ShardingTransactionalTestService.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@ShardingTransactionType(TransactionType.BASE)
public void testChangeTransactionTypeToBASE() {
    assertThat(TransactionTypeHolder.get(), is(TransactionType.BASE));
}
 
Example #26
Source File: ShardingTransactionalTestService.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
public void testChangeTransactionTypeInClass() {
    assertThat(TransactionTypeHolder.get(), is(TransactionType.XA));
}
 
Example #27
Source File: ShardingTransactionalSpringBootTest.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@Test
public void assertChangeTransactionTypeToXA() {
    testService.testChangeTransactionTypeToXA();
    assertThat(TransactionTypeHolder.get(), is(TransactionType.LOCAL));
}
 
Example #28
Source File: ShardingSphereDataSourceTest.java    From shardingsphere with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    TransactionTypeHolder.set(TransactionType.LOCAL);
}
 
Example #29
Source File: ShardingTransactionalSpringBootTest.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@Test
public void assertChangeTransactionTypeToBASE() {
    testService.testChangeTransactionTypeToBASE();
    assertThat(TransactionTypeHolder.get(), is(TransactionType.LOCAL));
}
 
Example #30
Source File: ShardingTransactionalSpringBootTest.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@Test
public void assertChangeTransactionTypeToLocal() {
    TransactionTypeHolder.set(TransactionType.XA);
    testService.testChangeTransactionTypeToLOCAL();
    assertThat(TransactionTypeHolder.get(), is(TransactionType.LOCAL));
}