This is an automated email from the ASF dual-hosted git repository.
lidongdai pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git
The following commit(s) were added to refs/heads/dev by this push:
new 3c5227a Refactor ConnectionFactory to static inner holder Singleton
(#2204)
3c5227a is described below
commit 3c5227ac0fdcd899e27ca0e8937a4ffa2528b4ba
Author: tswstarplanet <[email protected]>
AuthorDate: Wed Mar 18 23:48:19 2020 +0800
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
---
.../alert/utils/MailUtilsTest.java | 8 +-
.../org/apache/dolphinscheduler/dao/AlertDao.java | 4 +-
.../dao/datasource/ConnectionFactory.java | 97 ++++++++++++----------
.../dolphinscheduler/dao/upgrade/UpgradeDao.java | 2 +-
...onnectionFactoryTest.java => AlertDaoTest.java} | 25 +++---
.../dao/mapper/ConnectionFactoryTest.java | 2 +-
pom.xml | 2 +
7 files changed, 74 insertions(+), 66 deletions(-)
diff --git
a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/MailUtilsTest.java
b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/MailUtilsTest.java
index 612de3e..1820a1e 100644
---
a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/MailUtilsTest.java
+++
b/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.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;
@@ -33,7 +33,6 @@ import java.util.*;
/**
*/
-@Ignore
public class MailUtilsTest {
private static final Logger logger =
LoggerFactory.getLogger(MailUtilsTest.class);
@Test
@@ -138,8 +137,10 @@ public class MailUtilsTest {
* 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);
@@ -149,6 +150,7 @@ public class MailUtilsTest {
alert.setAlertType(AlertType.EMAIL);
alert.setAlertGroupId(1);
alertDao.addAlert(alert);
+ logger.info("" +alert);
}
@Test
diff --git
a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
index 1a8c09e..2ba93d7 100644
---
a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
+++
b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
@@ -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);
}
/**
diff --git
a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
index a3bc6a0..199fed0 100644
---
a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
+++
b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/ConnectionFactory.java
@@ -34,29 +34,47 @@ 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
*/
-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();
@@ -89,65 +107,54 @@ public class ConnectionFactory extends
SpringConnectionFactory{
/**
* * 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) {
diff --git
a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java
b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java
index aed9303..384c360 100644
---
a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java
+++
b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/UpgradeDao.java
@@ -53,7 +53,7 @@ public abstract class UpgradeDao extends AbstractBaseDao {
* @return DruidDataSource
*/
public static DruidDataSource getDataSource(){
- DruidDataSource dataSource = ConnectionFactory.getDataSource();
+ DruidDataSource dataSource =
ConnectionFactory.getInstance().getDataSource();
dataSource.setInitialSize(2);
dataSource.setMinIdle(2);
dataSource.setMaxActive(2);
diff --git
a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java
similarity index 64%
copy from
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
copy to
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java
index 5ba2936..b4f197a 100644
---
a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
+++
b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/AlertDaoTest.java
@@ -14,24 +14,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.dolphinscheduler.dao.mapper;
+package org.apache.dolphinscheduler.dao;
-import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
import org.junit.Assert;
import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-import java.sql.Connection;
+public class AlertDaoTest {
+ private static final Logger logger =
LoggerFactory.getLogger(AlertDaoTest.class);
-
-public class ConnectionFactoryTest {
-
- /**
- * test connection
- * @throws Exception if error throws Exception
- */
@Test
- public void testConnection()throws Exception{
- Connection connection =
ConnectionFactory.getDataSource().getPooledConnection().getConnection();
- Assert.assertTrue(connection != null);
+ public void testGetAlertDao() {
+ logger.info("testGetAlertDao start");
+ AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
+ Assert.assertNotNull(alertDao);
+ logger.info("testGetAlertDao end");
}
-}
\ No newline at end of file
+}
diff --git
a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
index 5ba2936..f413944 100644
---
a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
+++
b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java
@@ -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);
}
}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 8910723..412a28e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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>