未验证 提交 74525c28 编写于 作者: T tswstarplanet 提交者: GitHub

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
上级 42f9385e
......@@ -169,5 +169,11 @@ public class AlertDao extends AbstractBaseDao {
return userAlertGroupMapper.listUserByAlertgroupId(alertgroupId);
}
/**
* for test
* @return
*/
public AlertMapper getAlertMapper() {
return alertMapper;
}
}
......@@ -51,6 +51,7 @@ public class ConnectionFactory extends SpringConnectionFactory {
private ConnectionFactory() {
try {
dataSource = buildDataSource();
sqlSessionFactory = getSqlSessionFactory();
sqlSessionTemplate = getSqlSessionTemplate();
} catch (Exception e) {
......@@ -69,12 +70,18 @@ public class ConnectionFactory extends SpringConnectionFactory {
*/
private SqlSessionTemplate sqlSessionTemplate;
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
/**
* get the data source
*
* @return druid dataSource
*/
public DruidDataSource getDataSource() {
private DataSource buildDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
......@@ -112,10 +119,9 @@ public class ConnectionFactory extends SpringConnectionFactory {
* @throws Exception sqlSessionFactory exception
*/
private SqlSessionFactory getSqlSessionFactory() throws Exception {
DataSource dataSource = getDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Environment environment = new Environment("development", transactionFactory, getDataSource());
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setEnvironment(environment);
......@@ -125,7 +131,7 @@ public class ConnectionFactory extends SpringConnectionFactory {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setDataSource(getDataSource());
sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums");
sqlSessionFactory = sqlSessionFactoryBean.getObject();
......
......@@ -27,6 +27,7 @@ import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.io.*;
import java.sql.Connection;
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_NEW_VERSION_NAME = "t_ds_version";
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();
@Override
......@@ -52,13 +53,8 @@ public abstract class UpgradeDao extends AbstractBaseDao {
* get datasource
* @return DruidDataSource
*/
public static DruidDataSource getDataSource(){
DruidDataSource dataSource = ConnectionFactory.getInstance().getDataSource();
dataSource.setInitialSize(2);
dataSource.setMinIdle(2);
dataSource.setMaxActive(2);
return dataSource;
public static DataSource getDataSource(){
return ConnectionFactory.getInstance().getDataSource();
}
/**
......
......@@ -16,19 +16,42 @@
*/
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.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AlertDaoTest {
private static final Logger logger = LoggerFactory.getLogger(AlertDaoTest.class);
import java.util.Arrays;
import java.util.List;
public class AlertDaoTest {
@Test
public void testGetAlertDao() {
logger.info("testGetAlertDao start");
public void testAlertDao(){
AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
Assert.assertNotNull(alertDao);
logger.info("testGetAlertDao end");
Alert alert = new Alert();
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);
}
}
......@@ -31,7 +31,7 @@ public class ConnectionFactoryTest {
*/
@Test
public void testConnection()throws Exception{
Connection connection = ConnectionFactory.getInstance().getDataSource().getPooledConnection().getConnection();
Connection connection = ConnectionFactory.getInstance().getDataSource().getConnection();
Assert.assertTrue(connection != null);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册