本文整理汇总了Java中com.taobao.tddl.common.model.ThreadLocalString类的典型用法代码示例。如果您正苦于以下问题:Java ThreadLocalString类的具体用法?Java ThreadLocalString怎么用?Java ThreadLocalString使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ThreadLocalString类属于com.taobao.tddl.common.model包,在下文中一共展示了ThreadLocalString类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getIndex
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
public static GroupIndex getIndex() {
Integer indexObject = null;
try {
indexObject = (Integer) ThreadLocalMap.get(ThreadLocalString.DATASOURCE_INDEX);
// 不存在索引时返回-1,这样调用者只要知道返回值是-1就会认为业务层没有设置过索引
if (indexObject == null) {
return new GroupIndex(DBSelector.NOT_EXIST_USER_SPECIFIED_INDEX, false);
}
int index = indexObject.intValue();
// 如果业务层已设置了索引,此时索引不能为负值
if (index < 0) {
throw new IllegalArgumentException(msg(indexObject));
}
boolean failRetryFlag = ThreadLocalDataSourceIndex.getFailRetryFlag();
return new GroupIndex(index, failRetryFlag);
} catch (Exception e) {
throw new IllegalArgumentException(msg(indexObject));
}
}
示例2: testThreadLocalDataSourceIndex
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
@Test
public void testThreadLocalDataSourceIndex() throws Exception {
try {
TGroupDataSource ds = new TGroupDataSource(GROUP0, APPNAME);
MockServer.setConfigInfo(ds.getFullDbGroupKey(), DSKEY0 + ":rw" + "," + DSKEY1 + ":r");
ds.init();
ThreadLocalMap.put(ThreadLocalString.DATASOURCE_INDEX, 0);
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
assertEquals(stmt.executeUpdate("insert into tddl_test_0000(id,name) values(100,'str')"), 1);
ResultSet rs = stmt.executeQuery("select id,name from tddl_test_0000 where id=100");
assertTrue(rs.next());
// 如果指定了index,忽略rw限制
ThreadLocalMap.put(ThreadLocalString.DATASOURCE_INDEX, 1);
assertEquals(stmt.executeUpdate("insert into tddl_test_0000(id,name) values(100,'str')"), 1);
rs = stmt.executeQuery("select count(*) from tddl_test_0000 where id=100");
assertTrue(rs.next());
assertEquals(rs.getInt(1), 1);
stmt.close();
conn.close();
} finally {
ThreadLocalMap.put(ThreadLocalString.DATASOURCE_INDEX, null);
}
}
示例3: testGroupDataSourceRouteHelper
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
@Test
public void testGroupDataSourceRouteHelper() throws Exception {
try {
TGroupDataSource ds = new TGroupDataSource(GROUP0, APPNAME);
MockServer.setConfigInfo(ds.getFullDbGroupKey(), DSKEY0 + ":rw" + "," + DSKEY1 + ":r");
ds.init();
GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
assertEquals(stmt.executeUpdate("insert into tddl_test_0000(id,name) values(100,'str')"), 1);
ResultSet rs = stmt.executeQuery("select id,name from tddl_test_0000 where id=100");
assertTrue(rs.next());
// 如果指定了index,忽略rw限制
ThreadLocalMap.put(ThreadLocalString.DATASOURCE_INDEX, 1);
assertEquals(stmt.executeUpdate("insert into tddl_test_0000(id,name) values(100,'str')"), 1);
rs = stmt.executeQuery("select count(*) from tddl_test_0000 where id=100");
assertTrue(rs.next());
assertEquals(rs.getInt(1), 1);
stmt.close();
conn.close();
} finally {
ThreadLocalMap.put(ThreadLocalString.DATASOURCE_INDEX, null);
}
}
示例4: resetContext
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
private Map<Object, Object> resetContext(Map<Object, Object> threadContext) {
Map<Object, Object> context = new HashMap(threadContext);
if (context.containsKey(ThreadLocalString.TXC_CONTEXT_MANAGER) == true) {
if (ThreadLocalMap.containsKey(ThreadLocalString.TXC_CONTEXT_MANAGER) == false) {
context.remove(ThreadLocalString.TXC_CONTEXT_MANAGER);
context.put(ThreadLocalString.TXC_CONTEXT_MANAGER, ThreadLocalString.TXC_MANAGER_NAME);
}
}
return context;
}
示例5: executeByDBAndTab
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
/**
* 直接在某个库上,执行一条sql 这时候TDDL只做两件事情,第一个是协助判断是否在事务状态。 第二个事情是,进行表名的替换。
*
* @param dbIndex dbIndex列表
* @param logicTable 逻辑表名
* @param table 实际表名
* @param routeType 决定这个hint是在连接关闭的时候清空,还是在执行时就清空
*/
public static void executeByDBAndTab(String dbIndex, String logicTable, ROUTE_TYPE routeType, String... tables) {
DirectlyRouteCondition condition = new DirectlyRouteCondition();
if (tables == null) {
throw new IllegalArgumentException("tables is null");
}
for (String table : tables) {
condition.addTable(table);
}
condition.setVirtualTableName(logicTable);
condition.setDBId(dbIndex);
condition.setRouteType(routeType);
ThreadLocalMap.put(ThreadLocalString.DB_SELECTOR, condition);
}
示例6: executeByDB
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
/**
* 根据db index 执行一条sql. sql就是你通过Ibatis输入的sql. 只做一件事情,就是协助判断是否需要进行事务
*
* @param dbIndex dbIndex列表
* @param routeType 决定这个hint是在连接关闭的时候清空,还是在执行时就清空
*/
public static void executeByDB(String dbIndex, ROUTE_TYPE routeType) {
DirectlyRouteCondition condition = new DirectlyRouteCondition();
condition.setDBId(dbIndex);
condition.setRouteType(routeType);
ThreadLocalMap.put(ThreadLocalString.DB_SELECTOR, condition);
}
示例7: executeByAdvancedCondition
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
/**
* 指定多列的条件
*
* @param logicTable
* @param param
* @param routeType
*/
public static void executeByAdvancedCondition(String logicTable, Map<String, Comparable<?>> param,
ROUTE_TYPE routeType) {
RuleRouteCondition condition = new RuleRouteCondition();
condition.setVirtualTableName(logicTable);
for (Map.Entry<String, Comparable<?>> entry : param.entrySet()) {
condition.put(entry.getKey(), entry.getValue());
}
condition.setRouteType(routeType);
ThreadLocalMap.put(ThreadLocalString.ROUTE_CONDITION, condition);
}
示例8: removeTxcContext
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
/**
* 首先检查TXC_CONTEXT_STATE,存在则清理事务上下文
*/
private void removeTxcContext() {
Object value = ThreadLocalMap.get(ThreadLocalString.TXC_CONTEXT_MANAGER);
if (value != null && value.equals(ThreadLocalString.TXC_MANAGER_NAME)) {
ThreadLocalMap.remove(ThreadLocalString.TXC_CONTEXT);
ThreadLocalMap.remove(ThreadLocalString.TXC_CONTEXT_MANAGER);
}
}
示例9: getIndexAsObject
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
public static Integer getIndexAsObject() {
Integer indexObject = null;
try {
indexObject = (Integer) ThreadLocalMap.get(ThreadLocalString.DATASOURCE_INDEX);
if (indexObject == null) {
return null;
}
return indexObject;
} catch (Exception e) {
throw new IllegalArgumentException(msg(indexObject));
}
}
示例10: getFailRetryFlag
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
private static boolean getFailRetryFlag() {
Boolean failOver = (Boolean) ThreadLocalMap.get(ThreadLocalString.RETRY_IF_SET_DS_INDEX);
if (failOver == null) {
return false;
} else {
return failOver;
}
}
示例11: convertHint2RouteCondition
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
public static RouteCondition convertHint2RouteCondition(String sql, Map<Integer, ParameterContext> parameterSettings) {
// 检查下thread local hint
RouteCondition condition = getRouteContiongFromThreadLocal(ThreadLocalString.ROUTE_CONDITION);
if (condition != null) {
return condition;
}
condition = getRouteContiongFromThreadLocal(ThreadLocalString.DB_SELECTOR);
if (condition != null) {
return condition;
}
OldHintParser.checkOldThreadLocalHint();
String tddlHint = extractHint(sql, parameterSettings);
if (StringUtils.isNotEmpty(tddlHint)) {
if (tddlHint.contains("type:executeBy") || tddlHint.contains("type:'executeBy")) {
// 使用老hint
RouteCondition routeCondition = OldHintParser.extractHint(tddlHint);
if (routeCondition != null) {
return routeCondition;
}
}
try {
JSONObject jsonObject = JSON.parseObject(tddlHint);
String type = jsonObject.getString(TYPE);
if (TYPE_DIRECT.equalsIgnoreCase(type)) {
return decodeDirect(jsonObject);
} else if (TYPE_CONDITION.equalsIgnoreCase(type)) {
return decodeCondition(jsonObject);
} else {
return decodeExtra(jsonObject);
}
} catch (JSONException e) {
logger.error("convert tddl hint to RouteContion faild,check the hint string!", e);
throw e;
}
}
return null;
}
示例12: prepareCall
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
checkClosed();
// 先检查下是否有db hint
DirectlyRouteCondition route = (DirectlyRouteCondition) SimpleHintParser.getRouteContiongFromThreadLocal(ThreadLocalString.DB_SELECTOR);
String defaultDbIndex = null;
if (route != null) {
defaultDbIndex = route.getDbId();
} else {
// 针对存储过程,直接下推到default库上执行
defaultDbIndex = this.ds.getConfigHolder().getOptimizerContext().getRule().getDefaultDbIndex(null);
}
IGroupExecutor groupExecutor = this.ds.getConfigHolder()
.getExecutorContext()
.getTopologyHandler()
.get(defaultDbIndex);
Object groupDataSource = groupExecutor.getRemotingExecutableObject();
if (groupDataSource instanceof DataSource) {
GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
Connection conn = ((DataSource) groupDataSource).getConnection();
CallableStatement target = null;
if (resultSetType != Integer.MIN_VALUE && resultSetHoldability != Integer.MIN_VALUE) {
target = conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
} else if (resultSetType != Integer.MIN_VALUE) {
target = conn.prepareCall(sql, resultSetType, resultSetConcurrency);
} else {
target = conn.prepareCall(sql);
}
ExecutionContext context = prepareExecutionContext();
TCallableStatement stmt = new TCallableStatement(ds, this, sql, context, target);
openedStatements.add(stmt);
return stmt;
} else {
throw new UnsupportedOperationException();
}
}
示例13: flush_hint
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
/**
* 最终清空缓存,无论是否在TStatement的时候清空了hint.
*/
public void flush_hint() {
flushOne(ThreadLocalString.ROUTE_CONDITION);
flushOne(ThreadLocalString.DB_SELECTOR);
}
示例14: clearIndex
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
public static void clearIndex() {
ThreadLocalMap.remove(ThreadLocalString.DATASOURCE_INDEX);
ThreadLocalMap.remove(ThreadLocalString.RETRY_IF_SET_DS_INDEX);
}
示例15: executeByGroupDataSourceIndex
import com.taobao.tddl.common.model.ThreadLocalString; //导入依赖的package包/类
public static void executeByGroupDataSourceIndex(int dataSourceIndex, boolean failRetry) {
ThreadLocalMap.put(ThreadLocalString.DATASOURCE_INDEX, dataSourceIndex);
ThreadLocalMap.put(ThreadLocalString.RETRY_IF_SET_DS_INDEX, failRetry);
}