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

The following examples show how to use org.apache.shardingsphere.transaction.core.TransactionTypeHolder#set() . 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: 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 2
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 3
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 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: 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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 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: 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 16
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 17
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 18
Source File: ShardingTransactionalNameSpaceTest.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));
}
 
Example 19
Source File: ShardingTransactionJDBCAspect.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
/**
 * Set transaction type before transaction begin.
 *
 * @param joinPoint join point
 */
@Before(value = "shardingTransactionalJDBCPointCut()")
public void setTransactionTypeBeforeTransaction(final JoinPoint joinPoint) {
    ShardingTransactionType shardingTransactionType = getAnnotation(joinPoint);
    TransactionTypeHolder.set(shardingTransactionType.value());
}
 
Example 20
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));
}