本文整理汇总了Java中com.taobao.tddl.group.config.GroupIndex类的典型用法代码示例。如果您正苦于以下问题:Java GroupIndex类的具体用法?Java GroupIndex怎么用?Java GroupIndex使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GroupIndex类属于com.taobao.tddl.group.config包,在下文中一共展示了GroupIndex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeQuery
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
@Override
public ResultSet executeQuery(String sql) throws SQLException {
checkClosed();
ensureResultSetIsEmpty();
boolean gotoRead = SqlType.SELECT.equals(SqlTypeParser.getSqlType(sql)) && tGroupConnection.getAutoCommit();
Connection conn = tGroupConnection.getBaseConnection(sql, gotoRead);
if (conn != null) {
sql = GroupHintParser.removeTddlGroupHint(sql);
return executeQueryOnConnection(conn, sql);
} else {
// hint优先
GroupIndex dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeTddlGroupHint(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
return this.tGroupDataSource.getDBSelector(gotoRead).tryExecute(executeQueryTryer,
retryingTimes,
sql,
dataSourceIndex);
}
}
示例2: getIndex
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的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));
}
}
示例3: executeQuery
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
@Override
public ResultSet executeQuery() throws SQLException {
checkClosed();
ensureResultSetIsEmpty();
boolean gotoRead = SqlType.SELECT.equals(SqlTypeParser.getSqlType(sql)) && tGroupConnection.getAutoCommit();
Connection conn = tGroupConnection.getBaseConnection(sql, gotoRead);
if (conn != null) {
sql = GroupHintParser.removeTddlGroupHint(sql);
return executeQueryOnConnection(conn, sql);
} else {
// hint优先
GroupIndex dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeTddlGroupHint(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
return tGroupDataSource.getDBSelector(gotoRead).tryExecute(executeQueryTryer,
retryingTimes,
sql,
dataSourceIndex);
}
}
示例4: executeQuery
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
public ResultSet executeQuery(String sql) throws SQLException {
checkClosed();
ensureResultSetIsEmpty();
boolean gotoRead = SqlType.SELECT.equals(SqlTypeParser.getSqlType(sql)) && tGroupConnection.getAutoCommit();
Connection conn = tGroupConnection.getBaseConnection(sql, gotoRead);
if (conn != null) {
sql = GroupHintParser.removeTddlGroupHint(sql);
return executeQueryOnConnection(conn, sql);
} else {
// hint优先
GroupIndex dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeTddlGroupHint(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
return this.tGroupDataSource.getDBSelector(gotoRead).tryExecute(executeQueryTryer,
retryingTimes,
sql,
dataSourceIndex);
}
}
示例5: executeQuery
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
public ResultSet executeQuery() throws SQLException {
checkClosed();
ensureResultSetIsEmpty();
boolean gotoRead = SqlType.SELECT.equals(SqlTypeParser.getSqlType(sql)) && tGroupConnection.getAutoCommit();
Connection conn = tGroupConnection.getBaseConnection(sql, gotoRead);
if (conn != null) {
sql = GroupHintParser.removeTddlGroupHint(sql);
return executeQueryOnConnection(conn, sql);
} else {
// hint优先
GroupIndex dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeTddlGroupHint(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
return tGroupDataSource.getDBSelector(gotoRead).tryExecute(executeQueryTryer,
retryingTimes,
sql,
dataSourceIndex);
}
}
示例6: executeUpdateInternal
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
private int executeUpdateInternal(String sql, int autoGeneratedKeys, int[] columnIndexes, String[] columnNames)
throws SQLException {
checkClosed();
ensureResultSetIsEmpty();
Connection conn = tGroupConnection.getBaseConnection(sql, false);
if (conn != null) {
sql = GroupHintParser.removeTddlGroupHint(sql);
this.updateCount = executeUpdateOnConnection(conn, sql, autoGeneratedKeys, columnIndexes, columnNames);
return this.updateCount;
} else {
GroupIndex dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeTddlGroupHint(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
this.updateCount = this.tGroupDataSource.getDBSelector(false).tryExecute(null,
executeUpdateTryer,
retryingTimes,
sql,
autoGeneratedKeys,
columnIndexes,
columnNames,
dataSourceIndex);
return this.updateCount;
}
}
示例7: executeBatch
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
@Override
public int[] executeBatch() throws SQLException {
try {
checkClosed();
ensureResultSetIsEmpty();
if (pstArgs == null || pstArgs.isEmpty()) {
return new int[0];
}
Connection conn = tGroupConnection.getBaseConnection(sql, false);
if (conn != null) {
sql = GroupHintParser.removeTddlGroupHint(sql);
// 如果当前已经有连接,则不做任何重试。对于更新来说,不管有没有事务,
// 用户总期望getConnection获得连接之后,后续的一系列操作都在这同一个库,同一个连接上执行
return executeBatchOnConnection(conn);
} else {
GroupIndex dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeTddlGroupHint(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
return tGroupDataSource.getDBSelector(false).tryExecute(null,
executeBatchTryer,
retryingTimes,
dataSourceIndex);
}
} finally {
if (pstArgs != null) {
pstArgs.clear();
}
}
}
示例8: testSingleIndex
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
@Test
public void testSingleIndex() {
String sql = "/*+TDDL_GROUP({groupIndex:12})*/select * from tab";
GroupIndex index = GroupHintParser.convertHint2Index(sql);
Assert.assertEquals(index.index, 12);
Assert.assertEquals(index.failRetry, false);
}
示例9: testIndexWithRetry
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
@Test
public void testIndexWithRetry() {
String sql = "/*+TDDL_GROUP({groupIndex:12,failRetry:true})*/select * from tab";
GroupIndex index = GroupHintParser.convertHint2Index(sql);
Assert.assertEquals(index.index, 12);
Assert.assertEquals(index.failRetry, true);
}
示例10: prepareCall
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
public TGroupCallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
checkClosed();
CallableStatement target;
Connection conn = this.getBaseConnection(sql, false); // 存储过程默认走写库
if (conn != null) {
sql = GroupHintParser.removeTddlGroupHint(sql);
target = getCallableStatement(conn, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
} else {
// hint优先
GroupIndex dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeTddlGroupHint(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
target = tGroupDataSource.getDBSelector(false).tryExecute(null,
getCallableStatementTryer,
this.tGroupDataSource.getRetryingTimes(),
sql,
resultSetType,
resultSetConcurrency,
resultSetHoldability,
dataSourceIndex);
}
TGroupCallableStatement stmt = new TGroupCallableStatement(tGroupDataSource, this, target, sql);
if (resultSetType != Integer.MIN_VALUE) {
stmt.setResultSetType(resultSetType);
stmt.setResultSetConcurrency(resultSetConcurrency);
}
if (resultSetHoldability != Integer.MIN_VALUE) {
stmt.setResultSetHoldability(resultSetHoldability);
}
openedStatements.add(stmt);
return stmt;
}
示例11: executeBatch
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
public int[] executeBatch() throws SQLException {
try {
checkClosed();
ensureResultSetIsEmpty();
if (pstArgs == null || pstArgs.isEmpty()) {
return new int[0];
}
Connection conn = tGroupConnection.getBaseConnection(sql, false);
if (conn != null) {
sql = GroupHintParser.removeTddlGroupHint(sql);
// 如果当前已经有连接,则不做任何重试。对于更新来说,不管有没有事务,
// 用户总期望getConnection获得连接之后,后续的一系列操作都在这同一个库,同一个连接上执行
return executeBatchOnConnection(conn);
} else {
GroupIndex dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeTddlGroupHint(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
return tGroupDataSource.getDBSelector(false).tryExecute(null,
executeBatchTryer,
retryingTimes,
dataSourceIndex);
}
} finally {
if (pstArgs != null) {
pstArgs.clear();
}
}
}
示例12: tryOnDataSourceHolderWithIndex
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
/**
* 在指定单库上执行,不过调用方为直接设定group index的,如果指定的数据库不可用,
* 然后又指定了ThreadLocalString.RETRY_IF_SET_DS_INDEX 为true,那么走权重(如果有权重的话)
*
* @param <T>
* @param dsHolder
* @param failedDataSources
* @param tryer
* @param times
* @param args
* @return
* @throws SQLException
*/
protected <T> T tryOnDataSourceHolderWithIndex(DataSourceHolder dsHolder,
Map<DataSource, SQLException> failedDataSources,
DataSourceTryer<T> tryer, int times, GroupIndex index,
Object... args) throws SQLException {
List<SQLException> exceptions = new LinkedList<SQLException>();
if (failedDataSources != null) {
exceptions.addAll(failedDataSources.values());
}
if (failedDataSources != null && failedDataSources.containsKey(dsHolder.dsw)) {
return tryer.onSQLException(exceptions, exceptionSorter, args);
}
try {
if (dsHolder.isNotAvailable) {
boolean toTry = System.currentTimeMillis() - dsHolder.lastRetryTime > retryBadDbInterval;
if (toTry && dsHolder.lock.tryLock()) {
try {
T t = tryer.tryOnDataSource(dsHolder.dsw, args); // 同一个时间只会有一个线程继续使用这个数据源。
dsHolder.isNotAvailable = false; // 用一个线程重试,执行成功则标记为可用,自动恢复
return t;
} finally {
dsHolder.lastRetryTime = System.currentTimeMillis();
dsHolder.lock.unlock();
}
} else if (index.failRetry) {
// FIXME:这里需要看下,如果在事务中,是否应该重试。
return tryExecuteInternal(failedDataSources, tryer, times, args);
} else {
exceptions.add(new GroupNotAvaliableException("dsKey:" + dsHolder.dsw.getDataSourceKey()
+ " not Available,toTry:" + toTry));
return tryer.onSQLException(exceptions, exceptionSorter, args);
}
} else {
return tryer.tryOnDataSource(dsHolder.dsw, args); // 有一次成功直接返回
}
} catch (SQLException e) {
if (exceptionSorter.isExceptionFatal(e)) {
dsHolder.isNotAvailable = true;
}
exceptions.add(e);
return tryer.onSQLException(exceptions, exceptionSorter, args);
}
}
示例13: getBaseConnection
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
/**
* 获取事务中的上一个操作的链接
*/
Connection getBaseConnection(String sql, boolean isRead) throws SQLException {
GroupIndex dataSourceIndex = DEFAULT_GROUPINDEX;
if (sql == null) {
// 如果当前的数据源索引与上一次的数据源索引不一样,说明上一次缓存的Connection已经无用了,需要关闭后重建。
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
} else {
dataSourceIndex = GroupHintParser.convertHint2Index(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
}
// 代表出现自定义index请求
if (dataSourceIndex.index != DBSelector.NOT_EXIST_USER_SPECIFIED_INDEX) {
if (log.isDebugEnabled()) {
log.debug("dataSourceIndex=" + dataSourceIndex);
}
// 在事务状态下,设置不同的数据源索引会导致异常。
if (!isAutoCommit) {
if (wBaseDsWrapper != null && !wBaseDsWrapper.isMatchDataSourceIndex(dataSourceIndex.index)) {
throw new SQLException("Transaction in another dataSourceIndex: " + dataSourceIndex);
}
}
if (isRead) {
if (rBaseDsWrapper != null && !rBaseDsWrapper.isMatchDataSourceIndex(dataSourceIndex.index)) {
closeReadConnection();
}
} else {
if (wBaseDsWrapper != null && !wBaseDsWrapper.isMatchDataSourceIndex(dataSourceIndex.index)) {
closeWriteConnection();
}
}
}
// 为了保证事务正确关闭,在事务状态下只会取回写连接
if (isRead && isAutoCommit) {
// 只要有写连接,并且对应的库可读,则复用。否则返回读连接
return wBaseConnection != null && wBaseDsWrapper.hasReadWeight() ? wBaseConnection : rBaseConnection;
// 先写后读,重用写连接读后,rBaseConnection仍然是null
} else {
if (wBaseConnection != null) {
this.tGroupDataSource.setWriteTarget(wBaseDsWrapper);
return wBaseConnection;
} else if (rBaseConnection != null && rBaseDsWrapper.hasWriteWeight()) {
// 在写连接null的情况下,如果读连接已经建立,且对应的库可写,则复用
wBaseConnection = rBaseConnection; // wBaseConnection赋值,以确保事务能够正确提交回滚
// 在写连接上设置事务
if (wBaseConnection.getAutoCommit() != isAutoCommit) {
wBaseConnection.setAutoCommit(isAutoCommit);
}
// wBaseDsKey = rBaseDsKey;
wBaseDsWrapper = rBaseDsWrapper;
this.tGroupDataSource.setWriteTarget(wBaseDsWrapper);
return wBaseConnection;
} else {
return null;
}
}
}
示例14: prepareCall
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
@Override
public TGroupCallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
checkClosed();
CallableStatement target;
Connection conn = this.getBaseConnection(sql, false); // 存储过程默认走写库
if (conn != null) {
sql = GroupHintParser.removeTddlGroupHint(sql);
target = getCallableStatement(conn, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
} else {
// hint优先
GroupIndex dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeTddlGroupHint(sql);
if (dataSourceIndex == null) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
target = tGroupDataSource.getDBSelector(false).tryExecute(null,
getCallableStatementTryer,
this.tGroupDataSource.getRetryingTimes(),
sql,
resultSetType,
resultSetConcurrency,
resultSetHoldability,
dataSourceIndex);
}
TGroupCallableStatement stmt = new TGroupCallableStatement(tGroupDataSource,
this,
target,
sql,
this.tGroupDataSource.getAppName());
if (resultSetType != Integer.MIN_VALUE) {
stmt.setResultSetType(resultSetType);
stmt.setResultSetConcurrency(resultSetConcurrency);
}
if (resultSetHoldability != Integer.MIN_VALUE) {
stmt.setResultSetHoldability(resultSetHoldability);
}
openedStatements.add(stmt);
return stmt;
}
示例15: testNoIndexHint
import com.taobao.tddl.group.config.GroupIndex; //导入依赖的package包/类
@Test
public void testNoIndexHint() {
String sql = "select * from tab";
GroupIndex index = GroupHintParser.convertHint2Index(sql);
Assert.assertNull(index);
}