|
|
|
@ -11,8 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
|
import org.springframework.scheduling.annotation.Async; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
@ -43,7 +41,8 @@ public class DocumentProcessingService { |
|
|
|
|
|
|
|
/** |
|
|
|
* 异步处理文档:分块 → 关键词提取 → 向量化 → 更新状态 |
|
|
|
* 使用 REQUIRES_NEW 在独立事务中执行,避免与主事务冲突 |
|
|
|
* 不加跨方法事务:AI 关键词提取与向量化属于慢速网络调用,期间不占用数据库连接, |
|
|
|
* 避免文件夹批量上传时多个异步任务把连接池占满导致连接超时。 |
|
|
|
* |
|
|
|
* @param docId 文档ID |
|
|
|
* @param documents 已解析的原始文档列表 |
|
|
|
@ -54,8 +53,7 @@ public class DocumentProcessingService { |
|
|
|
* @param chunkSize 分块大小(可选,覆盖全局配置) |
|
|
|
* @param overlap 重叠大小(可选,覆盖全局配置) |
|
|
|
*/ |
|
|
|
@Async |
|
|
|
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) |
|
|
|
@Async("documentExecutor") |
|
|
|
public void processDocumentAsync(Long docId, List<Document> documents, String sourceName, |
|
|
|
String title, Long categoryId, List<String> tags, |
|
|
|
Integer chunkSize, Integer overlap) { |
|
|
|
@ -117,8 +115,7 @@ public class DocumentProcessingService { |
|
|
|
* @param docId 文档ID |
|
|
|
* @param documents 解析后的文档列表 |
|
|
|
*/ |
|
|
|
@Async |
|
|
|
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) |
|
|
|
@Async("documentExecutor") |
|
|
|
public void reprocessDocumentAsync(Long docId, List<Document> documents) { |
|
|
|
// 等待主事务提交,确保文档记录可见 |
|
|
|
KnowledgeDocument doc = waitForDocument(docId); |
|
|
|
|