Skip to content

Commit

Permalink
Refactor ConnectionFactory to static inner holder Singleton (apache#2204
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
tswstarplanet authored Mar 18, 2020
1 parent 880e995 commit 3c5227a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.dolphinscheduler.dao.DaoFactory;
import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.dao.entity.User;
import org.junit.Ignore;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,7 +33,6 @@

/**
*/
@Ignore
public class MailUtilsTest {
private static final Logger logger = LoggerFactory.getLogger(MailUtilsTest.class);
@Test
Expand Down Expand Up @@ -138,8 +137,10 @@ public void addAlertText(){
* Table
*/
@Test
public void addAlertTable(){
public void testAddAlertTable(){
logger.info("testAddAlertTable");
AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
Assert.assertNotNull(alertDao);
Alert alert = new Alert();
alert.setTitle("Mysql Exception");
alert.setShowType(ShowType.TABLE);
Expand All @@ -149,6 +150,7 @@ public void addAlertTable(){
alert.setAlertType(AlertType.EMAIL);
alert.setAlertGroupId(1);
alertDao.addAlert(alert);
logger.info("" +alert);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public class AlertDao extends AbstractBaseDao {

@Override
protected void init() {
alertMapper = ConnectionFactory.getMapper(AlertMapper.class);
userAlertGroupMapper = ConnectionFactory.getMapper(UserAlertGroupMapper.class);
alertMapper = ConnectionFactory.getInstance().getMapper(AlertMapper.class);
userAlertGroupMapper = ConnectionFactory.getInstance().getMapper(UserAlertGroupMapper.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,47 @@


/**
* 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
*/
public class ConnectionFactory extends SpringConnectionFactory{
public class ConnectionFactory extends SpringConnectionFactory {

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
*/
private static SqlSessionFactory sqlSessionFactory;
private SqlSessionFactory sqlSessionFactory;

/**
* sql session template
*/
private static SqlSessionTemplate sqlSessionTemplate;
private SqlSessionTemplate sqlSessionTemplate;

/**
* get the data source
*
* @return druid dataSource
*/
public static DruidDataSource getDataSource() {
public DruidDataSource getDataSource() {

DruidDataSource druidDataSource = new DruidDataSource();

Expand Down Expand Up @@ -89,65 +107,54 @@ public static DruidDataSource getDataSource() {

/**
* * get sql session factory
*
* @return sqlSessionFactory
* @throws Exception sqlSessionFactory exception
*/
public static SqlSessionFactory getSqlSessionFactory() throws Exception {
if (sqlSessionFactory == null) {
synchronized (ConnectionFactory.class) {
if (sqlSessionFactory == null) {
DataSource dataSource = getDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();

Environment environment = new Environment("development", transactionFactory, dataSource);

MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setEnvironment(environment);
configuration.setLazyLoadingEnabled(true);
configuration.addMappers("org.apache.dolphinscheduler.dao.mapper");
configuration.addInterceptor(new PaginationInterceptor());

MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setDataSource(dataSource);

sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums");
sqlSessionFactory = sqlSessionFactoryBean.getObject();
}
}
}
private SqlSessionFactory getSqlSessionFactory() throws Exception {
DataSource dataSource = getDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();

Environment environment = new Environment("development", transactionFactory, dataSource);

MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setEnvironment(environment);
configuration.setLazyLoadingEnabled(true);
configuration.addMappers("org.apache.dolphinscheduler.dao.mapper");
configuration.addInterceptor(new PaginationInterceptor());

MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setDataSource(dataSource);

sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums");
sqlSessionFactory = sqlSessionFactoryBean.getObject();

return sqlSessionFactory;
}

private SqlSessionTemplate getSqlSessionTemplate() {
sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
return sqlSessionTemplate;
}

/**
* get sql session
*
* @return sqlSession
*/
public static 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);
}
}
}
}
public SqlSession getSqlSession() {
return sqlSessionTemplate;
}

/**
* get mapper
*
* @param type target class
* @param <T> generic
* @param <T> generic
* @return target object
*/
public static <T> T getMapper(Class<T> type) {
public <T> T getMapper(Class<T> type) {
try {
return getSqlSession().getMapper(type);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected void init() {
* @return DruidDataSource
*/
public static DruidDataSource getDataSource(){
DruidDataSource dataSource = ConnectionFactory.getDataSource();
DruidDataSource dataSource = ConnectionFactory.getInstance().getDataSource();
dataSource.setInitialSize(2);
dataSource.setMinIdle(2);
dataSource.setMaxActive(2);
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ConnectionFactoryTest {
*/
@Test
public void testConnection()throws Exception{
Connection connection = ConnectionFactory.getDataSource().getPooledConnection().getConnection();
Connection connection = ConnectionFactory.getInstance().getDataSource().getPooledConnection().getConnection();
Assert.assertTrue(connection != null);
}
}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,8 @@
<include>**/server/worker/task/sqoop/SqoopTaskTest.java</include>
<include>**/server/utils/DataxUtilsTest.java</include>
<include>**/service/zk/DefaultEnsembleProviderTest.java</include>
<include>**/alert/utils/MailUtilsTest.java</include>
<include>**/dao/AlertDaoTest.java</include>
</includes>
<!-- <skip>true</skip> -->
</configuration>
Expand Down

0 comments on commit 3c5227a

Please sign in to comment.