Skip to content

Commit

Permalink
optimize: support oracle on delete tccfence logs (#5124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bughue authored Dec 5, 2022
1 parent 10a7470 commit 614454b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions changes/en-us/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Add changes here for all PR submitted to the develop branch.
- [[#5051](https://github.com/seata/seata/pull/5051)] undo log dirty throw BranchRollbackFailed_Unretriable
- [[#5075](https://github.com/seata/seata/pull/5075)] intercept the InsertOnDuplicateUpdate statement which has no primary key and unique index value
- [[#5104](https://github.com/seata/seata/pull/5104)] remove the druid dependency in ConnectionProxy
- [[#5124](https://github.com/seata/seata/pull/5124)] support oracle on delete tccfence logs


### test:
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
- [[#5051](https://github.com/seata/seata/pull/5051)] 回滚时undolog产生脏写需要抛出不再重试(BranchRollbackFailed_Unretriable)的异常
- [[#5075](https://github.com/seata/seata/pull/5075)] 拦截没有主键及唯一索引值的insert on duplicate update语句
- [[#5104](https://github.com/seata/seata/pull/5104)] ConnectionProxy脱离对druid的依赖
- [[#5124](https://github.com/seata/seata/pull/5124)] 支持oracle删除tccfence记录表

### test:
- [[#4411](https://github.com/seata/seata/pull/4411)] 测试Oracle数据库AT模式下类型支持
Expand Down
17 changes: 17 additions & 0 deletions tcc/src/main/java/io/seata/rm/tcc/TCCFenceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;
Expand Down Expand Up @@ -294,6 +295,12 @@ public static int deleteFenceByDate(Date datetime) {
int total = 0;
try {
connection = DataSourceUtils.getConnection(dataSource);
if (isOracle(connection)) {
// delete by date if DB is oracle
return TCC_FENCE_DAO.deleteTCCFenceDOByDate(connection, datetime);
}

//delete by id if DB is not oracle
while (true) {
Set<String> xidSet = TCC_FENCE_DAO.queryEndStatusXidsByDate(connection, datetime, LIMIT_DELETE);
if (xidSet.isEmpty()) {
Expand All @@ -312,6 +319,16 @@ public static int deleteFenceByDate(Date datetime) {

}

private static boolean isOracle(Connection connection) {
try {
String url = connection.getMetaData().getURL();
return url.toLowerCase().contains(":oracle:");
} catch (SQLException e) {
LOGGER.error("get db type fail", e);
}
return false;
}

private static void initLogCleanExecutor() {
logCleanExecutor = new ThreadPoolExecutor(MAX_THREAD_CLEAN, MAX_THREAD_CLEAN, Integer.MAX_VALUE,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(),
Expand Down
4 changes: 2 additions & 2 deletions tcc/src/main/java/io/seata/rm/tcc/store/TCCFenceStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public interface TCCFenceStore {
* Delete tcc fence by datetime.
* @param conn the connection
* @param datetime datetime
* @param limit limit
* @return the deleted row count
*/
int deleteTCCFenceDOByDate(Connection conn, Date datetime, int limit);
int deleteTCCFenceDOByDate(Connection conn, Date datetime);


/**
* Set LogTable Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,12 @@ public int deleteTCCFenceDO(Connection conn, List<String> xids) {
}

@Override
public int deleteTCCFenceDOByDate(Connection conn, Date datetime, int limit) {
public int deleteTCCFenceDOByDate(Connection conn, Date datetime) {
PreparedStatement ps = null;
try {
String sql = TCCFenceStoreSqls.getDeleteSQLByDateAndStatus(logTableName);
ps = conn.prepareStatement(sql);
ps.setTimestamp(1, new Timestamp(datetime.getTime()));
ps.setInt(2, limit);
return ps.executeUpdate();
} catch (SQLException e) {
throw new StoreException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ private TCCFenceStoreSqls() {
*/
protected static final String DELETE_BY_DATE_AND_STATUS = "delete from " + LOCAL_TCC_LOG_PLACEHOLD
+ " where gmt_modified < ? "
+ " and status in (" + TCCFenceConstant.STATUS_COMMITTED + " , " + TCCFenceConstant.STATUS_ROLLBACKED + " , " + TCCFenceConstant.STATUS_SUSPENDED + ")"
+ " limit ?";
+ " and status in (" + TCCFenceConstant.STATUS_COMMITTED + " , " + TCCFenceConstant.STATUS_ROLLBACKED + " , " + TCCFenceConstant.STATUS_SUSPENDED + ")";

public static String getInsertLocalTCCLogSQL(String localTccTable) {
return INSERT_LOCAL_TCC_LOG.replace(LOCAL_TCC_LOG_PLACEHOLD, localTccTable);
Expand Down

0 comments on commit 614454b

Please sign in to comment.