Browse Source

Refactor ConnectionFactory to static inner holder Singleton (#2204)

* refactor ConnectionFactory to static inner holder Singleton

* remove redundant import

* make getSqlSessionFactory() method private

* fix sonar issue

* remove @Ignore of MailUtilsTest

* add MailUtilsTest to pom.xml to test

* fix MailUtilsTest path error in pom.xml

* modify test method name

* add assert and logger to unit test

* add log

* add log

* add AlertDaoTest

* move AlertDaoTest to new module

* modify test in pom.xml

* remove unnecessary log
pull/2/head
tswstarplanet 5 years ago committed by GitHub
parent
commit
3c5227ac0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/MailUtilsTest.java
  2. 4
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
  3. 59
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
  4. 2
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java
  5. 34
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java
  6. 2
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
  7. 2
      pom.xml

8
dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/MailUtilsTest.java

@ -23,7 +23,7 @@ import org.apache.dolphinscheduler.dao.AlertDao;
import org.apache.dolphinscheduler.dao.DaoFactory; import org.apache.dolphinscheduler.dao.DaoFactory;
import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.junit.Ignore; 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;
@ -33,7 +33,6 @@ import java.util.*;
/** /**
*/ */
@Ignore
public class MailUtilsTest { public class MailUtilsTest {
private static final Logger logger = LoggerFactory.getLogger(MailUtilsTest.class); private static final Logger logger = LoggerFactory.getLogger(MailUtilsTest.class);
@Test @Test
@ -138,8 +137,10 @@ public class MailUtilsTest {
* Table * Table
*/ */
@Test @Test
public void addAlertTable(){ public void testAddAlertTable(){
logger.info("testAddAlertTable");
AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class); AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
Assert.assertNotNull(alertDao);
Alert alert = new Alert(); Alert alert = new Alert();
alert.setTitle("Mysql Exception"); alert.setTitle("Mysql Exception");
alert.setShowType(ShowType.TABLE); alert.setShowType(ShowType.TABLE);
@ -149,6 +150,7 @@ public class MailUtilsTest {
alert.setAlertType(AlertType.EMAIL); alert.setAlertType(AlertType.EMAIL);
alert.setAlertGroupId(1); alert.setAlertGroupId(1);
alertDao.addAlert(alert); alertDao.addAlert(alert);
logger.info("" +alert);
} }
@Test @Test

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

@ -50,8 +50,8 @@ public class AlertDao extends AbstractBaseDao {
@Override @Override
protected void init() { protected void init() {
alertMapper = ConnectionFactory.getMapper(AlertMapper.class); alertMapper = ConnectionFactory.getInstance().getMapper(AlertMapper.class);
userAlertGroupMapper = ConnectionFactory.getMapper(UserAlertGroupMapper.class); userAlertGroupMapper = ConnectionFactory.getInstance().getMapper(UserAlertGroupMapper.class);
} }
/** /**

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

@ -37,26 +37,44 @@ import javax.sql.DataSource;
* not spring manager connection, only use for init db, and alert module for non-spring application * not spring manager connection, only use for init db, and alert module for non-spring application
* data source connection factory * data source connection factory
*/ */
public class ConnectionFactory extends SpringConnectionFactory{ public class ConnectionFactory extends SpringConnectionFactory {
private static final Logger logger = LoggerFactory.getLogger(ConnectionFactory.class); private static final Logger logger = LoggerFactory.getLogger(ConnectionFactory.class);
private static class ConnectionFactoryHolder {
private static final ConnectionFactory connectionFactory = new ConnectionFactory();
}
public static ConnectionFactory getInstance() {
return ConnectionFactoryHolder.connectionFactory;
}
private ConnectionFactory() {
try {
sqlSessionFactory = getSqlSessionFactory();
sqlSessionTemplate = getSqlSessionTemplate();
} catch (Exception e) {
logger.error("Initializing ConnectionFactory error", e);
throw new RuntimeException(e);
}
}
/** /**
* sql session factory * sql session factory
*/ */
private static SqlSessionFactory sqlSessionFactory; private SqlSessionFactory sqlSessionFactory;
/** /**
* sql session template * sql session template
*/ */
private static SqlSessionTemplate sqlSessionTemplate; private SqlSessionTemplate sqlSessionTemplate;
/** /**
* get the data source * get the data source
*
* @return druid dataSource * @return druid dataSource
*/ */
public static DruidDataSource getDataSource() { public DruidDataSource getDataSource() {
DruidDataSource druidDataSource = new DruidDataSource(); DruidDataSource druidDataSource = new DruidDataSource();
@ -89,13 +107,11 @@ public class ConnectionFactory extends SpringConnectionFactory{
/** /**
* * get sql session factory * * get sql session factory
*
* @return sqlSessionFactory * @return sqlSessionFactory
* @throws Exception sqlSessionFactory exception * @throws Exception sqlSessionFactory exception
*/ */
public static SqlSessionFactory getSqlSessionFactory() throws Exception { private SqlSessionFactory getSqlSessionFactory() throws Exception {
if (sqlSessionFactory == null) {
synchronized (ConnectionFactory.class) {
if (sqlSessionFactory == null) {
DataSource dataSource = getDataSource(); DataSource dataSource = getDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory(); TransactionFactory transactionFactory = new JdbcTransactionFactory();
@ -113,41 +129,32 @@ public class ConnectionFactory extends SpringConnectionFactory{
sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums"); sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums");
sqlSessionFactory = sqlSessionFactoryBean.getObject(); sqlSessionFactory = sqlSessionFactoryBean.getObject();
}
}
}
return sqlSessionFactory; return sqlSessionFactory;
}
private SqlSessionTemplate getSqlSessionTemplate() {
sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
return sqlSessionTemplate;
} }
/** /**
* get sql session * get sql session
*
* @return sqlSession * @return sqlSession
*/ */
public static SqlSession getSqlSession() { public SqlSession getSqlSession() {
if (sqlSessionTemplate == null) {
synchronized (ConnectionFactory.class) {
if (sqlSessionTemplate == null) {
try {
sqlSessionTemplate = new SqlSessionTemplate(getSqlSessionFactory());
return sqlSessionTemplate;
} catch (Exception e) {
logger.error("getSqlSession error", e);
throw new RuntimeException(e);
}
}
}
}
return sqlSessionTemplate; return sqlSessionTemplate;
} }
/** /**
* get mapper * get mapper
*
* @param type target class * @param type target class
* @param <T> generic * @param <T> generic
* @return target object * @return target object
*/ */
public static <T> T getMapper(Class<T> type) { public <T> T getMapper(Class<T> type) {
try { try {
return getSqlSession().getMapper(type); return getSqlSession().getMapper(type);
} catch (Exception e) { } catch (Exception e) {

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

@ -53,7 +53,7 @@ public abstract class UpgradeDao extends AbstractBaseDao {
* @return DruidDataSource * @return DruidDataSource
*/ */
public static DruidDataSource getDataSource(){ public static DruidDataSource getDataSource(){
DruidDataSource dataSource = ConnectionFactory.getDataSource(); DruidDataSource dataSource = ConnectionFactory.getInstance().getDataSource();
dataSource.setInitialSize(2); dataSource.setInitialSize(2);
dataSource.setMinIdle(2); dataSource.setMinIdle(2);
dataSource.setMaxActive(2); dataSource.setMaxActive(2);

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

@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.dao;
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);
@Test
public void testGetAlertDao() {
logger.info("testGetAlertDao start");
AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
Assert.assertNotNull(alertDao);
logger.info("testGetAlertDao end");
}
}

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.getDataSource().getPooledConnection().getConnection(); Connection connection = ConnectionFactory.getInstance().getDataSource().getPooledConnection().getConnection();
Assert.assertTrue(connection != null); Assert.assertTrue(connection != null);
} }
} }

2
pom.xml

@ -740,6 +740,8 @@
<include>**/server/worker/task/sqoop/SqoopTaskTest.java</include> <include>**/server/worker/task/sqoop/SqoopTaskTest.java</include>
<include>**/server/utils/DataxUtilsTest.java</include> <include>**/server/utils/DataxUtilsTest.java</include>
<include>**/service/zk/DefaultEnsembleProviderTest.java</include> <include>**/service/zk/DefaultEnsembleProviderTest.java</include>
<include>**/alert/utils/MailUtilsTest.java</include>
<include>**/dao/AlertDaoTest.java</include>
</includes> </includes>
<!-- <skip>true</skip> --> <!-- <skip>true</skip> -->
</configuration> </configuration>

Loading…
Cancel
Save