Search in sources :

Example 1 with TransactionalTemplate

use of io.seata.tm.api.TransactionalTemplate in project jboot by yangfuhai.

the class JbootSeataManager method init.

public void init() {
    if (!config.isEnable()) {
        return;
    }
    if (!config.isConfigOk()) {
        throw new JbootIllegalConfigException("please config applicationId and txServiceGroup for seata");
    }
    transactionManager = new SeataGlobalTransactionManager(config.getApplicationId(), config.getTxServiceGroup(), config.getMode());
    transactionManager.init();
    this.transactionalTemplate = new TransactionalTemplate();
    this.globalLockTemplate = new GlobalLockTemplate();
    this.enable = true;
}
Also used : JbootIllegalConfigException(io.jboot.exception.JbootIllegalConfigException) GlobalLockTemplate(io.seata.rm.GlobalLockTemplate) TransactionalTemplate(io.seata.tm.api.TransactionalTemplate)

Example 2 with TransactionalTemplate

use of io.seata.tm.api.TransactionalTemplate in project seata by seata.

the class AppTest method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
public static void main(String[] args) throws Throwable {
    TMClient.init(APPLICATION_ID, TX_SERVICE_GROUP);
    RMClient.init(APPLICATION_ID, TX_SERVICE_GROUP);
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("basic-test-context.xml");
    final JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
    jdbcTemplate.update("delete from undo_log");
    jdbcTemplate.update("delete from user0");
    jdbcTemplate.update("insert into user0 (id, name, gmt) values (1, 'user0', '2019-01-01')");
    jdbcTemplate.update("delete from user1");
    final MyBusinessException bizException = new MyBusinessException("mock bizException");
    TransactionalTemplate transactionalTemplate = new TransactionalTemplate();
    try {
        transactionalTemplate.execute(new TransactionalExecutor() {

            @Override
            public Object execute() throws Throwable {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("Exception Rollback Business Begin ...");
                }
                jdbcTemplate.update("update user0 set name = 'xxx' where id = ?", new Object[] { 1 });
                jdbcTemplate.update("insert into user1 (id, name, gmt) values (1, 'user1', '2019-01-01')");
                throw bizException;
            }

            @Override
            public TransactionInfo getTransactionInfo() {
                TransactionInfo txInfo = new TransactionInfo();
                txInfo.setTimeOut(TX_TIME_OUT);
                txInfo.setName(TX_NAME);
                return txInfo;
            }
        });
    } catch (TransactionalExecutor.ExecutionException e) {
        TransactionalExecutor.Code code = e.getCode();
        if (code == TransactionalExecutor.Code.RollbackDone) {
            Throwable businessEx = e.getOriginalException();
            if (businessEx instanceof MyBusinessException) {
                Assertions.assertEquals(((MyBusinessException) businessEx).getBusinessErrorCode(), bizException.businessErrorCode);
            }
        } else {
            Assertions.assertFalse(false, "Not expected," + e.getMessage());
        }
    }
    new ApplicationKeeper(context).keep();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TransactionalExecutor(io.seata.tm.api.TransactionalExecutor) TransactionInfo(io.seata.tm.api.transaction.TransactionInfo) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) TransactionalTemplate(io.seata.tm.api.TransactionalTemplate) ApplicationKeeper(io.seata.common.ApplicationKeeper)

Aggregations

TransactionalTemplate (io.seata.tm.api.TransactionalTemplate)2 JbootIllegalConfigException (io.jboot.exception.JbootIllegalConfigException)1 ApplicationKeeper (io.seata.common.ApplicationKeeper)1 GlobalLockTemplate (io.seata.rm.GlobalLockTemplate)1 TransactionalExecutor (io.seata.tm.api.TransactionalExecutor)1 TransactionInfo (io.seata.tm.api.transaction.TransactionInfo)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1