本文整理匯總了Java中com.taobao.tddl.group.jdbc.TGroupDataSource.getConnection方法的典型用法代碼示例。如果您正苦於以下問題:Java TGroupDataSource.getConnection方法的具體用法?Java TGroupDataSource.getConnection怎麽用?Java TGroupDataSource.getConnection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.taobao.tddl.group.jdbc.TGroupDataSource
的用法示例。
在下文中一共展示了TGroupDataSource.getConnection方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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);
}
}
示例2: 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);
}
}
示例3: 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));
}
示例4: 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();
}
示例5: 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);
}
}
示例6: 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);
}
}
示例7: 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");
}
示例8: 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("由於SQLException,更新初值自適應失敗!dbGroupIndex:" + dbGroupKeys.get(index)
+ ",sequence Name:" + name + "更新過程:" + value + "-->" + newValue, e);
} finally {
closeDbResource(null, stmt, conn);
}
}
示例9: 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("由於SQLException,插入初值自適應失敗!dbGroupIndex:" + dbGroupKeys.get(index)
+ ",sequence Name:" + name + " value:" + newValue, e);
} finally {
closeDbResource(rs, stmt, conn);
}
}
示例10: adjust
import com.taobao.tddl.group.jdbc.TGroupDataSource; //導入方法依賴的package包/類
/**
* <pre>
* 檢查並初試某個sequence。
*
* 1、如果sequece不處在,插入值,並初始化值。
* 2、如果已經存在,但有重疊,重新生成。
* 3、如果已經存在,且無重疊。
*
* @throws SequenceException
* </pre>
*/
public void adjust(String name) throws SequenceException, SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
for (int i = 0; i < dbGroupKeys.size(); i++) {
if (dbGroupKeys.get(i).toUpperCase().endsWith("-OFF"))// 已經關掉,不處理
{
continue;
}
TGroupDataSource tGroupDataSource = (TGroupDataSource) dataSourceMap.get(dbGroupKeys.get(i));
try {
conn = tGroupDataSource.getConnection();
stmt = conn.prepareStatement(getSelectSql());
stmt.setString(1, name);
GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
rs = stmt.executeQuery();
int item = 0;
while (rs.next()) {
item++;
long val = rs.getLong(this.getValueColumnName());
if (!check(i, val)) // 檢驗初值
{
if (this.isAdjust()) {
this.adjustUpdate(i, val, name);
} else {
logger.error("數據庫中配置的初值出錯!請調整你的數據庫,或者啟動adjust開關");
throw new SequenceException("數據庫中配置的初值出錯!請調整你的數據庫,或者啟動adjust開關");
}
}
}
if (item == 0)// 不存在,插入這條記錄
{
if (this.isAdjust()) {
this.adjustInsert(i, name);
} else {
logger.error("數據庫中未配置該sequence!請往數據庫中插入sequence記錄,或者啟動adjust開關");
throw new SequenceException("數據庫中未配置該sequence!請往數據庫中插入sequence記錄,或者啟動adjust開關");
}
}
} catch (SQLException e) {// 吞掉SQL異常,我們允許不可用的庫存在
logger.error("初值校驗和自適應過程中出錯.", e);
throw e;
} finally {
closeDbResource(rs, stmt, conn);
}
}
}
示例11: main
import com.taobao.tddl.group.jdbc.TGroupDataSource; //導入方法依賴的package包/類
public static void main(String[] args) throws TddlException, SQLException {
TGroupDataSource ds = new TGroupDataSource();
ds.setDbGroupKey("CBU_WORKOPERATIONS1_TEST_GROUP");
ds.setAppName("CBU_WORKOPERATIONS_TEST_APP");
// ds.setRuleFile("rule.xml");
//
// Map cp = new HashMap();
// cp.put("ALLOW_TEMPORARY_TABLE", "True");
// cp.put(ConnectionProperties.TEMP_TABLE_DIR, ".\\temp\\");
// cp.put(ConnectionProperties.TEMP_TABLE_CUT_ROWS, false);
// cp.put(ConnectionProperties.TEMP_TABLE_MAX_ROWS, 100);
// ds.setConnectionProperties(cp);
ds.init();
System.out.println("init done");
long start = System.currentTimeMillis();
Connection conn = ds.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT a.custId, a.id,b.adgroupid, SUM(c.cost) / SUM(c.click) AS ppc,c.thedate, c.memberid, c.campaignid, c.productlineid, c.adgroupid, SUM(c.impression), SUM(c.cost), SUM(c.click), SUM(c.click) / SUM(c.impression) AS ctr, b.title, a.onlinestate, a.reason FROM Lunaadgroup a join lunaadgroupinfo b on a.id=b.adgroupid LEFT JOIN rpt_solar_adgroup_ob c ON a.id = c.adgroupid where a.custid='1102000884' and b.custid='1102000884' GROUP BY c.thedate, c.memberid, c.campaignid, c.productlineid, c.adgroupid HAVING ppc > 1 ORDER BY ppc DESC");
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("done " + (System.currentTimeMillis() - start));
}
示例12: main
import com.taobao.tddl.group.jdbc.TGroupDataSource; //導入方法依賴的package包/類
public static void main(String[] args) throws TddlException, SQLException {
TGroupDataSource ds = new TGroupDataSource();
// init a datasource with dynamic config on diamond
ds.setAppName("wap");
ds.setDbGroupKey("wap_group");
ds.init();
System.out.println("init done");
Connection conn = ds.getConnection();
// insert a record
// conn.prepareStatement("replace into sample_table (id,name,address) values (1,'sun','hz')").executeUpdate();
// System.out.println("insert done");
// select all records
PreparedStatement ps = conn.prepareStatement("/*+TDDL({\"type\":\"direct\",\"realtabs\":[traa],\"dbid\":\"tddl5_sample_group\",\"vtab\":\"sample_table\"})*/ SELECT * from sample_table");
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");
}