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");
}
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;
}
});
}
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);
}
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;
}
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();
}
}
}
Aggregations