You've already forked DataMate
refactor(data-management): 优化PDF文本提取服务的事务处理
- 添加TransactionSynchronization相关依赖注入 - 实现事务提交后异步执行PDF文本提取功能 - 增加数据集ID和文件ID的空值检查 - 在活跃事务中注册同步回调确保正确执行 - 避免在事务未提交时提前执行异步任务
This commit is contained in:
@@ -40,9 +40,11 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.UrlResource;
|
import org.springframework.core.io.UrlResource;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.transaction.support.TransactionSynchronization;
|
||||||
|
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -950,17 +952,31 @@ public class DatasetFileApplicationService {
|
|||||||
return addedFiles;
|
return addedFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void triggerPdfTextExtraction(Dataset dataset, DatasetFile datasetFile) {
|
private void triggerPdfTextExtraction(Dataset dataset, DatasetFile datasetFile) {
|
||||||
if (dataset == null || datasetFile == null) {
|
if (dataset == null || datasetFile == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (dataset.getDatasetType() != DatasetType.TEXT) {
|
if (dataset.getDatasetType() != DatasetType.TEXT) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String fileType = datasetFile.getFileType();
|
String fileType = datasetFile.getFileType();
|
||||||
if (fileType == null || !DOCUMENT_TEXT_FILE_TYPES.contains(fileType.toLowerCase(Locale.ROOT))) {
|
if (fileType == null || !DOCUMENT_TEXT_FILE_TYPES.contains(fileType.toLowerCase(Locale.ROOT))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pdfTextExtractAsyncService.extractPdfText(dataset.getId(), datasetFile.getId());
|
String datasetId = dataset.getId();
|
||||||
}
|
String fileId = datasetFile.getId();
|
||||||
}
|
if (datasetId == null || fileId == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||||
|
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
||||||
|
@Override
|
||||||
|
public void afterCommit() {
|
||||||
|
pdfTextExtractAsyncService.extractPdfText(datasetId, fileId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pdfTextExtractAsyncService.extractPdfText(datasetId, fileId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user