From e19833fca4476ba375e544f5987f9a02bbbe7e8d Mon Sep 17 00:00:00 2001 From: Tboy Date: Mon, 2 Dec 2019 20:01:55 +0800 Subject: [PATCH] using spring context datasource (#1317) --- .../api/service/MonitorService.java | 6 ++- .../dolphinscheduler/dao/MonitorDBDao.java | 51 ++++++------------- 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java index a98806f04b..68f3f12f6c 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java @@ -25,6 +25,7 @@ import org.apache.dolphinscheduler.common.model.Server; import org.apache.dolphinscheduler.dao.entity.MonitorRecord; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.ZookeeperRecord; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -38,6 +39,9 @@ import java.util.Map; @Service public class MonitorService extends BaseService{ + + @Autowired + private MonitorDBDao monitorDBDao; /** * query database state * @@ -47,7 +51,7 @@ public class MonitorService extends BaseService{ public Map queryDatabaseState(User loginUser) { Map result = new HashMap<>(5); - List monitorRecordList = MonitorDBDao.queryDatabaseState(); + List monitorRecordList = monitorDBDao.queryDatabaseState(); result.put(Constants.DATA_LIST, monitorRecordList); putMsg(result, Status.SUCCESS); diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java index 288fe6154d..51f60666d1 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java @@ -17,65 +17,47 @@ package org.apache.dolphinscheduler.dao; import com.alibaba.druid.pool.DruidDataSource; -import org.apache.dolphinscheduler.common.Constants; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import org.apache.dolphinscheduler.common.enums.DbType; -import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory; import org.apache.dolphinscheduler.dao.entity.MonitorRecord; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.dolphinscheduler.dao.utils.MysqlPerformance; import org.apache.dolphinscheduler.dao.utils.PostgrePerformance; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import java.sql.*; -import java.util.ArrayList; -import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; /** * database state dao */ +@Component public class MonitorDBDao { private static Logger logger = LoggerFactory.getLogger(MonitorDBDao.class); + public static final String VARIABLE_NAME = "variable_name"; - /** - * load conf - */ - private static Configuration conf; + @Autowired + private DruidDataSource dataSource; - static { - try { - conf = new PropertiesConfiguration(Constants.APPLICATION_PROPERTIES); - }catch (ConfigurationException e){ - logger.error("load configuration excetpion",e); - System.exit(1); - } - } /** * get current db performance * @return MonitorRecord */ - public static MonitorRecord getCurrentDbPerformance(){ + public MonitorRecord getCurrentDbPerformance(){ MonitorRecord monitorRecord = null; Connection conn = null; - DruidDataSource dataSource = null; try{ - dataSource = ConnectionFactory.getDataSource(); - dataSource.setInitialSize(2); - dataSource.setMinIdle(2); - dataSource.setMaxActive(2); conn = dataSource.getConnection(); - if(conn == null){ - return monitorRecord; - } - if(conf.getString(Constants.SPRING_DATASOURCE_DRIVER_CLASS_NAME).contains(DbType.MYSQL.toString().toLowerCase())) { + String driverClassName = dataSource.getDriverClassName(); + if(driverClassName.contains(DbType.MYSQL.toString().toLowerCase())){ return new MysqlPerformance().getMonitorRecord(conn); - } else if(conf.getString(Constants.SPRING_DATASOURCE_DRIVER_CLASS_NAME).contains(DbType.POSTGRESQL.toString().toLowerCase())){ + } else if(driverClassName.contains(DbType.POSTGRESQL.toString().toLowerCase())){ return new PostgrePerformance().getMonitorRecord(conn); } }catch (Exception e) { @@ -85,9 +67,6 @@ public class MonitorDBDao { if (conn != null) { conn.close(); } - if(dataSource != null){ - dataSource.close(); - } } catch (SQLException e) { logger.error("SQLException ", e); } @@ -99,7 +78,7 @@ public class MonitorDBDao { * query database state * @return MonitorRecord list */ - public static List queryDatabaseState() { + public List queryDatabaseState() { List list = new ArrayList<>(1); MonitorRecord monitorRecord = getCurrentDbPerformance();