本文整理汇总了Java中com.taobao.tddl.group.jdbc.TGroupDataSource类的典型用法代码示例。如果您正苦于以下问题:Java TGroupDataSource类的具体用法?Java TGroupDataSource怎么用?Java TGroupDataSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TGroupDataSource类属于com.taobao.tddl.group.jdbc包,在下文中一共展示了TGroupDataSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setGroupMockInfo
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
protected static void setGroupMockInfo(String groupPath, String key) throws Exception {
// 获取group信息
String groupStr = LoadPropsUtil.loadProps2OneLine(groupPath, key);
if (groupStr == null || StringUtils.isBlank(groupStr)) {
throw new Exception("指定path = " + groupPath + ",key = " + key + "的group信息为null或者为空字符。");
}
// 获取atom信息
dataMap = new HashMap<String, String>();
String[] atomArr = groupStr.split(",");
for (String atom : atomArr) {
atom = atom.trim();
atom = atom.substring(0, atom.indexOf(":"));
initAtomConfig(ATOM_PATH + File.separator + atom, APPNAME, atom);
}
// 获取groupkey
dataMap.put(TGroupDataSource.getFullDbGroupKey(key), groupStr);
// 建立MockServer
MockServer.setConfigInfos(dataMap);
// 获取JdbcTemplate
tddlJT = getJT(key);
}
示例2: testThreadLocalDataSourceIndex
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的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.group.jdbc.TGroupDataSource; //导入依赖的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: javax_sql_DataSource_api_support
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
@Test
public void javax_sql_DataSource_api_support() throws Exception {
TGroupDataSource ds = new TGroupDataSource();
assertEquals(ds.getLoginTimeout(), 0);
assertEquals(ds.getLogWriter(), null);
PrintWriter writer = new PrintWriter(System.out);
ds.setLoginTimeout(100);
ds.setLogWriter(writer);
assertEquals(ds.getLoginTimeout(), 100);
assertEquals(ds.getLogWriter(), writer);
Connection conn = ds.getConnection();
assertTrue((conn instanceof TGroupConnection));
conn = ds.getConnection("username", "password");
assertTrue((conn instanceof TGroupConnection));
}
示例5: testCrud_Read
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
private void testCrud_Read(TGroupDataSource ds) throws SQLException {
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
assertEquals(stmt.executeUpdate("insert into tddl_test_0000(id,name,gmt_create,gmt_modified) values(100,'str',now(),now())"),
1);
// 在只写库上更新后,会保留写连接,
// 但是因为写连接对应的数据源被配置成只写,所以接下来的读操作不允许在写连接上进行
// 因为db2,db3都没有数据,所以rs.next()返回false
ResultSet rs = stmt.executeQuery("select id,name from tddl_test_0000 where id=100");
assertFalse(rs.next());
rs.close();
assertEquals(stmt.executeUpdate("delete from tddl_test_0000 where id=100"), 1);
stmt.close();
conn.close();
}
示例6: doInit
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
/**
* 初试化
*/
@Override
public void doInit() throws TddlException {
// 如果应用名为空,直接抛出
if (StringUtils.isEmpty(appName)) {
SequenceException sequenceException = new SequenceException("appName is Null ");
logger.error("没有配置appName", sequenceException);
throw sequenceException;
}
if (dbGroupKeys == null || dbGroupKeys.size() == 0) {
logger.error("没有配置dbgroupKeys");
throw new SequenceException("dbgroupKeys为空!");
}
dataSourceMap = new HashMap<String, DataSource>();
for (String dbGroupKey : dbGroupKeys) {
if (dbGroupKey.toUpperCase().endsWith("-OFF")) {
continue;
}
// TGroupDataSource tGroupDataSource = new TGroupDataSource(
// dbGroupKey, appName, dataSourceType);
TGroupDataSource tGroupDataSource = new TGroupDataSource(dbGroupKey, appName);
tGroupDataSource.setUnitName(this.getUnitName());
tGroupDataSource.init();
dataSourceMap.put(dbGroupKey, tGroupDataSource);
}
if (dbGroupKeys.size() >= dscount) {
dscount = dbGroupKeys.size();
} else {
for (int ii = dbGroupKeys.size(); ii < dscount; ii++) {
dbGroupKeys.add(dscount + "-OFF");
}
}
outStep = innerStep * dscount;// 计算外步长
outputInitResult();
}
示例7: adjustUpdate
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
/**
* 更新
*
* @param index
* @param value
* @param name
* @throws SequenceException
* @throws SQLException
*/
private void adjustUpdate(int index, long value, String name) throws SequenceException, SQLException {
long newValue = (value - value % outStep) + outStep + index * innerStep;// 设置成新的调整值
TGroupDataSource tGroupDataSource = (TGroupDataSource) dataSourceMap.get(dbGroupKeys.get(index));
Connection conn = null;
PreparedStatement stmt = null;
// ResultSet rs = null;
try {
conn = tGroupDataSource.getConnection();
stmt = conn.prepareStatement(getUpdateSql());
stmt.setLong(1, newValue);
stmt.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
stmt.setString(3, name);
stmt.setLong(4, value);
GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
int affectedRows = stmt.executeUpdate();
if (affectedRows == 0) {
throw new SequenceException("faild to auto adjust init value at " + name + " update affectedRow =0");
}
logger.info(dbGroupKeys.get(index) + "更新初值成功!" + "sequence Name:" + name + "更新过程:" + value + "-->"
+ newValue);
} catch (SQLException e) { // 吃掉SQL异常,抛Sequence异常
logger.error("由于SQLException,更新初值自适应失败!dbGroupIndex:" + dbGroupKeys.get(index) + ",sequence Name:" + name
+ "更新过程:" + value + "-->" + newValue, e);
throw new SequenceException(e, "由于SQLException,更新初值自适应失败!dbGroupIndex:" + dbGroupKeys.get(index)
+ ",sequence Name:" + name + "更新过程:" + value + "-->" + newValue);
} finally {
closeDbResource(null, stmt, conn);
}
}
示例8: adjustInsert
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
/**
* 插入新值
*
* @param index
* @param name
* @return
* @throws SequenceException
* @throws SQLException
*/
private void adjustInsert(int index, String name) throws SequenceException, SQLException {
TGroupDataSource tGroupDataSource = (TGroupDataSource) dataSourceMap.get(dbGroupKeys.get(index));
long newValue = index * innerStep;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = tGroupDataSource.getConnection();
stmt = conn.prepareStatement(getInsertSql());
stmt.setString(1, name);
stmt.setLong(2, newValue);
stmt.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
int affectedRows = stmt.executeUpdate();
if (affectedRows == 0) {
throw new SequenceException("faild to auto adjust init value at " + name + " update affectedRow =0");
}
logger.info(dbGroupKeys.get(index) + " name:" + name + "插入初值:" + name + "value:" + newValue);
} catch (SQLException e) {
logger.error("由于SQLException,插入初值自适应失败!dbGroupIndex:" + dbGroupKeys.get(index) + ",sequence Name:" + name
+ " value:" + newValue, e);
throw new SequenceException(e, "由于SQLException,插入初值自适应失败!dbGroupIndex:" + dbGroupKeys.get(index)
+ ",sequence Name:" + name + " value:" + newValue);
} finally {
closeDbResource(rs, stmt, conn);
}
}
示例9: doInit
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
/**
* 初试化
*/
public void doInit() throws TddlException {
// 如果应用名为空,直接抛出
if (StringUtils.isEmpty(appName)) {
SequenceException sequenceException = new SequenceException("appName is Null ");
logger.error("没有配置appName", sequenceException);
throw sequenceException;
}
if (dbGroupKeys == null || dbGroupKeys.size() == 0) {
logger.error("没有配置dbgroupKeys");
throw new SequenceException("dbgroupKeys为空!");
}
// 取得unit数据
this.changeConfig(false, null);
RouterUnitsHelper.registerUnitsListener(this);
dataSourceMap = new HashMap<String, DataSource>();
for (String dbGroupKey : dbGroupKeys) {
if (dbGroupKey.toUpperCase().endsWith("-OFF")) {
continue;
}
// TGroupDataSource tGroupDataSource = new TGroupDataSource(
// dbGroupKey, appName, dataSourceType);
TGroupDataSource tGroupDataSource = new TGroupDataSource(dbGroupKey, appName);
tGroupDataSource.init();
dataSourceMap.put(dbGroupKey, tGroupDataSource);
}
outputInitResult();
}
示例10: setUp
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
s1 = new TGroupDataSource();
s1.setAppName("TDDLSEQUENCE");
s1.setDbGroupKey("TDDL_SEQUENCE_GROUP0");
s1.init();
s2 = new TGroupDataSource();
s2.setAppName("TDDLSEQUENCE");
s2.setDbGroupKey("TDDL_SEQUENCE_GROUP1");
s2.init();
dao = new GroupSequenceDao();
// 设置adjust为true,这样不必关心怎么样去设置有规律的值。单元化部署必须设置这个值为true,切换的时候自动更新
dao.setAdjust(true);
dao.setAppName("TDDLSEQUENCE");
List<String> dbGroups = new ArrayList<String>();
dbGroups.add("TDDL_SEQUENCE_GROUP0");
dbGroups.add("TDDL_SEQUENCE_GROUP1");
dao.setDbGroupKeys(dbGroups);
dao.setDscount(2);
dao.setInnerStep(1000);
dao.init();
updateNormalTable(0);
updateTestTable(testStart);
GroupSequence s = new GroupSequence();
s.setName("ladygaga");
s.setSequenceDao(dao);
s.init();
sequence = s;
}
示例11: main
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
TGroupDataSource ds = new TGroupDataSource();
// init a datasource with dynamic config on diamond
ds.setAppName("ICBU_PB_METADATA_TEST_APP");
ds.setDbGroupKey("PBSERVER_GROUP");
ds.init();
System.out.println("init done");
Connection conn = ds.getConnection();
PreparedStatement ps = conn.prepareStatement("select * from auks_task_detail_normal");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
StringBuilder sb = new StringBuilder();
int count = rs.getMetaData().getColumnCount();
for (int i = 1; i <= count; i++) {
String key = rs.getMetaData().getColumnLabel(i);
Object val = rs.getObject(i);
sb.append("[" + rs.getMetaData().getTableName(i) + "." + key + "->" + val + "]");
}
System.out.println(sb.toString());
}
rs.close();
ps.close();
conn.close();
System.out.println("query done");
}
示例12: getJT
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
protected static JdbcTemplate getJT(String dbGroupKey) {
tds = new TGroupDataSource();
tds.setAppName(APPNAME);
tds.setDbGroupKey(dbGroupKey);
try {
tds.init();
} catch (TddlException e) {
Assert.fail();
}
return new JdbcTemplate(tds);
}
示例13: GroupConfigManager
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
public GroupConfigManager(TGroupDataSource tGroupDataSource){
this.tGroupDataSource = tGroupDataSource;
this.configReceiver = new ConfigReceiver();
this.extraGroupConfigReceiver = new ExtraGroupConfigReceiver();
((ConfigReceiver) this.configReceiver).setConfigManager(this);
}
示例14: setUpBeforeClass
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
@BeforeClass
public static void setUpBeforeClass() throws Exception {
tgds = new TGroupDataSource();
tgds.setDbGroupKey("dbKey0");
List<DataSourceWrapper> dataSourceWrappers = new ArrayList<DataSourceWrapper>();
DataSourceWrapper dsw1 = new DataSourceWrapper("db1", "rw", db1, DBType.MYSQL);
DataSourceWrapper dsw2 = new DataSourceWrapper("db2", "r", db2, DBType.MYSQL);
dataSourceWrappers.add(dsw1);
dataSourceWrappers.add(dsw2);
tgds.init(dataSourceWrappers);
}
示例15: setBeforeClass
import com.taobao.tddl.group.jdbc.TGroupDataSource; //导入依赖的package包/类
@BeforeClass
public static void setBeforeClass() throws TddlException {
TGroupDataSource ds = new TGroupDataSource("tddl_sample_group_0", "tddl_sample");
ds.init();
dataSource = ds;
jdbcTemplate = new JdbcTemplate(dataSource);
}