Browse Source

Decouple DruidDataSource in ConnectionFactory and cache DataSource instance (#2232)

* Decouple DruidDataSource in ConnectionFactory and cache DataSource
instance

* init dataSource first

* fix code smell

* add unit test

* add unit test

* add unit test

* add unit test

* fix unit test

* delete useless logger
pull/3/MERGE
tswstarplanet 5 years ago committed by gaojun2048
parent
commit
0c5ff32797
  1. 8
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
  2. 14
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
  3. 12
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java
  4. 35
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java
  5. 2
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java

8
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java

@ -169,5 +169,11 @@ public class AlertDao extends AbstractBaseDao {
return userAlertGroupMapper.listUserByAlertgroupId(alertgroupId); return userAlertGroupMapper.listUserByAlertgroupId(alertgroupId);
} }
/**
* for test
* @return
*/
public AlertMapper getAlertMapper() {
return alertMapper;
}
} }

14
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java

@ -51,6 +51,7 @@ public class ConnectionFactory extends SpringConnectionFactory {
private ConnectionFactory() { private ConnectionFactory() {
try { try {
dataSource = buildDataSource();
sqlSessionFactory = getSqlSessionFactory(); sqlSessionFactory = getSqlSessionFactory();
sqlSessionTemplate = getSqlSessionTemplate(); sqlSessionTemplate = getSqlSessionTemplate();
} catch (Exception e) { } catch (Exception e) {
@ -69,12 +70,18 @@ public class ConnectionFactory extends SpringConnectionFactory {
*/ */
private SqlSessionTemplate sqlSessionTemplate; private SqlSessionTemplate sqlSessionTemplate;
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
/** /**
* get the data source * get the data source
* *
* @return druid dataSource * @return druid dataSource
*/ */
public DruidDataSource getDataSource() { private DataSource buildDataSource() {
DruidDataSource druidDataSource = new DruidDataSource(); DruidDataSource druidDataSource = new DruidDataSource();
@ -112,10 +119,9 @@ public class ConnectionFactory extends SpringConnectionFactory {
* @throws Exception sqlSessionFactory exception * @throws Exception sqlSessionFactory exception
*/ */
private SqlSessionFactory getSqlSessionFactory() throws Exception { private SqlSessionFactory getSqlSessionFactory() throws Exception {
DataSource dataSource = getDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory(); TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource); Environment environment = new Environment("development", transactionFactory, getDataSource());
MybatisConfiguration configuration = new MybatisConfiguration(); MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setEnvironment(environment); configuration.setEnvironment(environment);
@ -125,7 +131,7 @@ public class ConnectionFactory extends SpringConnectionFactory {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setConfiguration(configuration); sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setDataSource(getDataSource());
sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums"); sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums");
sqlSessionFactory = sqlSessionFactoryBean.getObject(); sqlSessionFactory = sqlSessionFactoryBean.getObject();

12
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java

@ -27,6 +27,7 @@ import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.io.*; import java.io.*;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -40,7 +41,7 @@ public abstract class UpgradeDao extends AbstractBaseDao {
private static final String T_VERSION_NAME = "t_escheduler_version"; private static final String T_VERSION_NAME = "t_escheduler_version";
private static final String T_NEW_VERSION_NAME = "t_ds_version"; private static final String T_NEW_VERSION_NAME = "t_ds_version";
private static final String rootDir = System.getProperty("user.dir"); private static final String rootDir = System.getProperty("user.dir");
protected static final DruidDataSource dataSource = getDataSource(); protected static final DataSource dataSource = getDataSource();
private static final DbType dbType = getCurrentDbType(); private static final DbType dbType = getCurrentDbType();
@Override @Override
@ -52,13 +53,8 @@ public abstract class UpgradeDao extends AbstractBaseDao {
* get datasource * get datasource
* @return DruidDataSource * @return DruidDataSource
*/ */
public static DruidDataSource getDataSource(){ public static DataSource getDataSource(){
DruidDataSource dataSource = ConnectionFactory.getInstance().getDataSource(); return ConnectionFactory.getInstance().getDataSource();
dataSource.setInitialSize(2);
dataSource.setMinIdle(2);
dataSource.setMaxActive(2);
return dataSource;
} }
/** /**

35
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java

@ -16,19 +16,42 @@
*/ */
package org.apache.dolphinscheduler.dao; package org.apache.dolphinscheduler.dao;
import org.apache.dolphinscheduler.common.enums.AlertStatus;
import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.enums.ShowType;
import org.apache.dolphinscheduler.dao.entity.Alert;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class AlertDaoTest { import java.util.Arrays;
private static final Logger logger = LoggerFactory.getLogger(AlertDaoTest.class); import java.util.List;
public class AlertDaoTest {
@Test @Test
public void testGetAlertDao() { public void testAlertDao(){
logger.info("testGetAlertDao start");
AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class); AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
Assert.assertNotNull(alertDao); Alert alert = new Alert();
logger.info("testGetAlertDao end"); alert.setTitle("Mysql Exception");
alert.setShowType(ShowType.TEXT);
alert.setContent("[\"alarm time:2018-02-05\", \"service name:MYSQL_ALTER\", \"alarm name:MYSQL_ALTER_DUMP\", " +
"\"get the alarm exception.!,interface error,exception information:timed out\", \"request address:http://blog.csdn.net/dreamInTheWorld/article/details/78539286\"]");
alert.setAlertType(AlertType.EMAIL);
alert.setAlertGroupId(1);
alert.setAlertStatus(AlertStatus.WAIT_EXECUTION);
alertDao.addAlert(alert);
List<Alert> alerts = alertDao.listWaitExecutionAlert();
Assert.assertNotNull(alerts);
Assert.assertNotEquals(0, alerts.size());
int id = alerts.get(0).getId();
AlertStatus alertStatus = alerts.get(0).getAlertStatus();
alertDao.updateAlert(AlertStatus.EXECUTION_SUCCESS, "", id);
alerts = alertDao.listWaitExecutionAlert();
Assert.assertEquals(0, alerts.size());
alertDao.getAlertMapper().deleteById(id);
} }
} }

2
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java

@ -31,7 +31,7 @@ public class ConnectionFactoryTest {
*/ */
@Test @Test
public void testConnection()throws Exception{ public void testConnection()throws Exception{
Connection connection = ConnectionFactory.getInstance().getDataSource().getPooledConnection().getConnection(); Connection connection = ConnectionFactory.getInstance().getDataSource().getConnection();
Assert.assertTrue(connection != null); Assert.assertTrue(connection != null);
} }
} }
Loading…
Cancel
Save