TCC anti-suspension prevents empty rollbacks. An empty rollback occurs when the second-phase cancel method executes before the first-phase try method. TCC idempotence control ensures that the first-phase try method of a TCC participant is executed exactly once within the same distributed transaction.
Follow these steps to configure TCC anti-suspension and idempotence in the TCC participant service provider's application:
Create a table
Execute the following Data Definition Language (DDL) statement to create the dtx_tcc_action table in your business database:
CREATE TABLE `dtx_tcc_action`(
`action_id` varchar(96) NOT NULL COMMENT 'Branch transaction ID',
`action_name` varchar(64) DEFAULT NULL COMMENT 'Participant name',
`tx_id` varchar(128) NOT NULL COMMENT 'Primary transaction ID',
`action_group` varchar(32) DEFAULT NULL COMMENT 'Action group',
`status` varchar(10) DEFAULT NULL COMMENT 'Status',
`param_data` varchar(4000) DEFAULT NULL COMMENT 'Parameter data of the first-phase method',
`gmt_create` datetime NOT NULL COMMENT 'Creation time',
`gmt_modified` datetime NOT NULL COMMENT 'Modification time',
`sharding_key` varchar(128) DEFAULT NULL COMMENT 'Sharding key',
PRIMARY KEY (`action_id`),
UNIQUE KEY `idx_tx_id`(`tx_id`,`action_name`)
);Configure the DAO
If you use Ibatis, configure the following bean:
<bean class="com.alipay.sofa.dtx.tcc.dao.ibatis.IbatisTccAntiSuspendDAO"> <property name="dataSource" ref="yourDataSourceBean"/> </bean>If you use Mybatis, configure the following bean:
<bean class="com.alipay.sofa.dtx.tcc.dao.mybatis.MybatisTccAntiSuspendDAO"> <property name="dataSource" ref="yourDataSourceBean"/> </bean>
Set the dataSource property to the DataSource bean of your business database.
Modify the interface definition
Set the antiSuspend property of the @TwoPhaseBusinessAction annotation to true to enable the anti-suspension feature:
public interface TccAction{
@TwoPhaseBusinessAction(name ="yourTccActionName", commitMethod ="confirm", rollbackMethod ="cancel", antiSuspend =true)
public boolean try(BusinessActionContext businessActionContext,@ShardingKeyint a,int b);
public boolean confirm(BusinessActionContext businessActionContext);
public boolean cancel(BusinessActionContext businessActionContext);
}If your business database uses sharding, add the @ShardingKey annotation before the sharding parameter in the first-phase method. This is not required for non-partitioned table databases.
Modify the interface implementation
In the first-phase try method of the TCC participant, call the TccTransactionController.doAntiSuspendControl() method within the business database transaction to add an anti-suspension record:
public class TccActionImpl implements TccAction {
@Override
public boolean try(
BusinessActionContext businessActionContext, int a, int b)
{
transactionTemplate.execute(newTransactionCallback < Object > () {
@Override
public Object doInTransaction (TransactionStatus status){
// Manually enable anti-suspension control.
TccTransactionController.doAntiSuspendControl();
}
});
}
@Override
public boolean confirm(BusinessActionContext businessActionContext) {
}
@Override
public boolean cancel(BusinessActionContext businessActionContext) {
}
}