Skip to content

Commit cd73655

Browse files
committed
update Transactional control
1 parent 51e3014 commit cd73655

4 files changed

Lines changed: 26 additions & 33 deletions

File tree

.idea/workspace.xml

Lines changed: 16 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/phantoms/phantomsbackend/common/config/SchedulingConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class SchedulingConfig {
1414
@Bean
1515
public ThreadPoolTaskScheduler taskScheduler() {
1616
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
17-
scheduler.setPoolSize(5); // 核心线程数5,避免单线程阻塞
17+
scheduler.setPoolSize(10); // 核心线程数10,避免单线程阻塞
1818
scheduler.setThreadNamePrefix("FF14-Scheduler-");
1919
scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
2020
scheduler.setWaitForTasksToCompleteOnShutdown(true);

src/main/java/com/phantoms/phantomsbackend/service/scheduler/DataSyncScheduler.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.data.domain.Pageable;
1414
import org.springframework.scheduling.annotation.Scheduled;
1515
import org.springframework.stereotype.Service;
16+
import org.springframework.transaction.annotation.Transactional;
1617
import org.springframework.util.StopWatch;
1718

1819
import java.time.LocalDateTime;
@@ -99,6 +100,7 @@ public class DataSyncScheduler {
99100
private com.phantoms.phantomsbackend.repository.secondary.SecondaryExpeditionaryTeamRepository secondaryExpeditionaryTeamRepository;
100101

101102
@Scheduled(fixedRate = 600000) // 每10分钟执行一次
103+
@Transactional(timeout = 300)
102104
public void syncData() {
103105
StopWatch totalStopWatch = new StopWatch("TotalSync");
104106
totalStopWatch.start("total-sync");
@@ -252,41 +254,29 @@ private List<com.phantoms.phantomsbackend.pojo.entity.secondary.onebot.ChatRecor
252254
}
253255

254256
/**
255-
* 优化后的批量插入方法
257+
* 优化后的批量插入方法 - 使用 Spring 事务管理
256258
*/
259+
@Transactional(timeout = 120) // 2分钟超时
257260
private void batchInsertOptimized(List<com.phantoms.phantomsbackend.pojo.entity.secondary.onebot.ChatRecord> records) {
258261
if (records.isEmpty()) {
259262
return;
260263
}
261264

262-
EntityManager entityManager = entityManagerFactory.createEntityManager();
263-
EntityTransaction transaction = entityManager.getTransaction();
264-
265265
try {
266-
transaction.begin();
267-
266+
// 使用 Spring Data JPA 的批量保存
268267
for (int i = 0; i < records.size(); i++) {
269-
entityManager.persist(records.get(i));
268+
secondaryChatRecordRepository.save(records.get(i));
270269

271270
// 分批刷新,避免内存溢出
272271
if ((i + 1) % INSERT_BATCH_SIZE == 0) {
273-
entityManager.flush();
274-
entityManager.clear();
272+
secondaryChatRecordRepository.flush();
275273
}
276274
}
277-
278-
entityManager.flush();
279-
entityManager.clear();
280-
transaction.commit();
275+
secondaryChatRecordRepository.flush();
281276

282277
} catch (Exception e) {
283-
if (transaction.isActive()) {
284-
transaction.rollback();
285-
}
286278
logger.error("Batch insert failed for {} records", records.size(), e);
287279
throw e;
288-
} finally {
289-
entityManager.close();
290280
}
291281
}
292282

src/main/java/com/phantoms/phantomsbackend/service/scheduler/RecruitmentScheduler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class RecruitmentScheduler {
4646
private RedisUtil redisUtil;
4747

4848
@Scheduled(fixedRate = 300000) // 每300秒执行一次
49-
@Transactional
49+
@Transactional(timeout = 60, readOnly = false)
5050
public void fetchAndFilterRecruitments() {
5151
try {
5252
List<RecruitmentResponse> allResponses = LittlenightmareClient.fetchAllRecruitmentListings(

0 commit comments

Comments
 (0)