dtm-labs / dtmcli-java Goto Github PK
View Code? Open in Web Editor NEWOfficial java client for distributed transaction framework dtm
License: MIT License
Official java client for distributed transaction framework dtm
License: MIT License
// pub.dtm.client.barrier.BranchBarrier#insertBarrier
private boolean insertBarrier(Connection connection) throws SQLException {
log.info("insert barrier {}", this);
if (Objects.isNull(connection)) {
return false;
}
PreparedStatement preparedStatement = null;
try {
String sql = "insert ignore into barrier(trans_type, gid, branch_id, op, barrier_id, reason) values(?,?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, this.getTransTypeEnum().getValue());
preparedStatement.setString(2, this.getGid());
preparedStatement.setString(3, branchId);
preparedStatement.setString(4, op);
preparedStatement.setString(5, String.format("%02d", barrierId));
preparedStatement.setString(6, op);
if (preparedStatement.executeUpdate() == 0) {
return false;
}
if (ParamFieldConstants.CANCEL.equals(op)) {
int opIndex = 4;
preparedStatement.setString(opIndex, ParamFieldConstants.TRY);
if (preparedStatement.executeUpdate() > 0) {
return false;
}
}
} finally {
if (Objects.nonNull(preparedStatement)) {
preparedStatement.close();
}
}
return true;
}
这个地方,只判断了ParamFieldConstants.CANCEL.equals(op),而Saga模式的补偿op是compensate,不是cancel
tcc模式下想要解析 response的body内容
但response.body().string()只能请求一次,在dtm内已经调用过此方法
请问是否还有其他解决方案可以获取返回值吗?
public static void checkResult(Response response) throws Exception { if (response.code() >= 400) { throw new FailureException(response.message()); } else { ResponseBody body = response.body(); String result; if (body != null && !StringUtils.isBlank(result = body.string())) { if (result.contains("FAILURE")) { throw new FailureException("Service returned failed"); } } else { throw new FailureException("response is null"); } } }
// pub.dtm.client.saga.Saga#setTimeoutToFail
public Saga setTimeoutToFail(long timeoutToFail) {
this.setTimeoutToFail(timeoutToFail);
return this;
}
这个代码,是一个死递归,会导致栈溢出
创建saga事务的时候设置http头可能出现栈溢出。
CREATE TABLE barrier
(
id
bigint NOT NULL AUTO_INCREMENT,
user_id
int DEFAULT NULL,
amount
int DEFAULT NULL,
trans_type
varchar(255) DEFAULT NULL,
gid
varchar(100) DEFAULT NULL,
branch_id
varchar(100) DEFAULT NULL,
op
varchar(255) DEFAULT NULL,
barrier_id
varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
reason
varchar(255) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
看了一下Java SDK的源码,发现如下几个问题(由大到小排序)
---大问题---
---小问题---
本地事务屏障只支持MySQL
不会自动创建barrier表
No support for 2-phase message yet?
BUG:
FILE: tcc.java
HttpResponse response2 = HttpRequest.post(tryUrl)
.body(JSONUtil.toJsonStr(body))
.form(paramMap2) // HERE
.execute();
maybe FIX :
HttpResponse response2 = HttpRequest.post(tryUrl + "?" + mapToQueryString(paramMap2))
.body(JSONUtil.toJsonStr(body))
//.form(paramMap2)
.execute();
private static String mapToQueryString(Map<String, Object> queryParams) {
String queryString = queryParams.entrySet().stream()
.map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.joining("&"));
return URLUtil.encode(queryString);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.