Commit d5f7c354 authored by yajun.zhang's avatar yajun.zhang

优化

parent 25a28eb7
...@@ -2,6 +2,8 @@ package cn.quantgroup.customer.config.data; ...@@ -2,6 +2,8 @@ package cn.quantgroup.customer.config.data;
import cn.hutool.log.Log; import cn.hutool.log.Log;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.sql.DataSource; import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -14,19 +16,25 @@ import org.springframework.stereotype.Component; ...@@ -14,19 +16,25 @@ import org.springframework.stereotype.Component;
@Component @Component
public class DatabaseConnectionKeeper { public class DatabaseConnectionKeeper {
private final JdbcTemplate jdbcTemplate; // private final JdbcTemplate jdbcTemplate;
private EntityManager entityManager;
// @Autowired
// public DatabaseConnectionKeeper(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
// this.jdbcTemplate = new JdbcTemplate(secondaryDataSource);
// }
@Autowired @Autowired
public DatabaseConnectionKeeper(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) { public DatabaseConnectionKeeper(@Qualifier("entityManagerSecondary") EntityManager entityManager) {
this.jdbcTemplate = new JdbcTemplate(secondaryDataSource); this.entityManager = entityManager;
} }
// 每小时执行一次查询操作,保持数据库连接活跃 // 每小时执行一次查询操作,保持数据库连接活跃
@Scheduled(fixedDelay = 60000) // 每小时执行一次 @Scheduled(fixedDelay = 60000) // 每小时执行一次
public void keepDatabaseConnectionAlive() { public void keepDatabaseConnectionAlive() {
log.info("数据库定时任务执行"); log.info("数据库定时任务执行");
try { try {
jdbcTemplate.execute("SELECT 1"); // 执行一个简单的查询操作 Query countQuery = entityManager.createNativeQuery("SELECT 1");
Object count = countQuery.getSingleResult();
log.info("查询结果:{}",count);
// jdbcTemplate.execute("SELECT 1"); // 执行一个简单的查询操作
} catch (Exception ex) { } catch (Exception ex) {
// 处理异常 // 处理异常
log.error("数据库定时任务执行异常",ex); log.error("数据库定时任务执行异常",ex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment