Search in sources :

Example 1 with GlobalLockConfig

use of io.seata.core.model.GlobalLockConfig in project seata by seata.

the class GlobalLockConfigHolderTest method setAndReturnPrevious.

@Test
void setAndReturnPrevious() {
    GlobalLockConfig config1 = new GlobalLockConfig();
    assertNull(GlobalLockConfigHolder.setAndReturnPrevious(config1), "should return null");
    assertSame(config1, GlobalLockConfigHolder.getCurrentGlobalLockConfig(), "holder fail to store config");
    GlobalLockConfig config2 = new GlobalLockConfig();
    assertSame(config1, GlobalLockConfigHolder.setAndReturnPrevious(config2), "fail to get previous config");
    assertSame(config2, GlobalLockConfigHolder.getCurrentGlobalLockConfig(), "holder fail to store latest config");
}
Also used : GlobalLockConfig(io.seata.core.model.GlobalLockConfig) Test(org.junit.jupiter.api.Test)

Example 2 with GlobalLockConfig

use of io.seata.core.model.GlobalLockConfig in project seata by seata.

the class BaseTransactionalExecutorTest method testExecuteWithGlobalLockSet.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testExecuteWithGlobalLockSet() throws Throwable {
    // initial objects
    ConnectionProxy connectionProxy = new ConnectionProxy(null, null);
    StatementProxy statementProxy = new StatementProxy<>(connectionProxy, null);
    BaseTransactionalExecutor<Object, Statement> baseTransactionalExecutor = new BaseTransactionalExecutor<Object, Statement>(statementProxy, null, (SQLRecognizer) null) {

        @Override
        protected Object doExecute(Object... args) {
            return null;
        }
    };
    GlobalLockTemplate template = new GlobalLockTemplate();
    // not in global lock context
    try {
        baseTransactionalExecutor.execute(new Object());
        Assertions.assertFalse(connectionProxy.isGlobalLockRequire(), "connection context set!");
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
    // in global lock context
    template.execute(new GlobalLockExecutor() {

        @Override
        public Object execute() throws Throwable {
            baseTransactionalExecutor.execute(new Object());
            Assertions.assertTrue(connectionProxy.isGlobalLockRequire(), "connection context not set!");
            return null;
        }

        @Override
        public GlobalLockConfig getGlobalLockConfig() {
            return null;
        }
    });
}
Also used : GlobalLockConfig(io.seata.core.model.GlobalLockConfig) GlobalLockTemplate(io.seata.rm.GlobalLockTemplate) GlobalLockExecutor(io.seata.rm.GlobalLockExecutor) Statement(java.sql.Statement) StatementProxy(io.seata.rm.datasource.StatementProxy) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) Test(org.junit.jupiter.api.Test)

Example 3 with GlobalLockConfig

use of io.seata.core.model.GlobalLockConfig in project seata by seata.

the class LockRetryControllerTest method setUp.

@BeforeEach
void setUp() {
    config = new GlobalLockConfig();
    config.setLockRetryInternal(10);
    config.setLockRetryTimes(3);
    GlobalLockConfigHolder.setAndReturnPrevious(config);
}
Also used : GlobalLockConfig(io.seata.core.model.GlobalLockConfig) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with GlobalLockConfig

use of io.seata.core.model.GlobalLockConfig in project seata by seata.

the class GlobalLockTemplateTest method generateGlobalLockConfig.

private GlobalLockConfig generateGlobalLockConfig() {
    GlobalLockConfig config = new GlobalLockConfig();
    config.setLockRetryInternal(100);
    config.setLockRetryTimes(3);
    return config;
}
Also used : GlobalLockConfig(io.seata.core.model.GlobalLockConfig)

Example 5 with GlobalLockConfig

use of io.seata.core.model.GlobalLockConfig in project seata by seata.

the class GlobalLockTemplate method execute.

public Object execute(GlobalLockExecutor executor) throws Throwable {
    boolean alreadyInGlobalLock = RootContext.requireGlobalLock();
    if (!alreadyInGlobalLock) {
        RootContext.bindGlobalLockFlag();
    }
    // set my config to config holder so that it can be access in further execution
    // for example, LockRetryController can access it with config holder
    GlobalLockConfig myConfig = executor.getGlobalLockConfig();
    GlobalLockConfig previousConfig = GlobalLockConfigHolder.setAndReturnPrevious(myConfig);
    try {
        return executor.execute();
    } finally {
        // otherwise, the outer caller would lose global lock flag
        if (!alreadyInGlobalLock) {
            RootContext.unbindGlobalLockFlag();
        }
        // so that the outer logic can still use their config
        if (previousConfig != null) {
            GlobalLockConfigHolder.setAndReturnPrevious(previousConfig);
        } else {
            GlobalLockConfigHolder.remove();
        }
    }
}
Also used : GlobalLockConfig(io.seata.core.model.GlobalLockConfig)

Aggregations

GlobalLockConfig (io.seata.core.model.GlobalLockConfig)8 Test (org.junit.jupiter.api.Test)2 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)1 TransactionException (io.seata.core.exception.TransactionException)1 GlobalLockExecutor (io.seata.rm.GlobalLockExecutor)1 GlobalLockTemplate (io.seata.rm.GlobalLockTemplate)1 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)1 StatementProxy (io.seata.rm.datasource.StatementProxy)1 Propagation (io.seata.tm.api.transaction.Propagation)1 SuspendedResourcesHolder (io.seata.tm.api.transaction.SuspendedResourcesHolder)1 TransactionInfo (io.seata.tm.api.transaction.TransactionInfo)1 Statement (java.sql.Statement)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1