From a6bd568df74133c010d4a5a4d8bdc0ac82b9a4d4 Mon Sep 17 00:00:00 2001 From: qiaozhanwei Date: Sun, 29 Sep 2019 20:08:21 +0800 Subject: [PATCH] add ConnectionFactoryTest and ConnectionFactory read datasource from appliction.yml (#931) --- .../dao/config/MybatisPlusConfig.java | 16 +++++ .../dao/config/YmlConfig.java | 26 +++++--- .../dao/datasource/ConnectionFactory.java | 13 ++-- .../dao/datasource/DatabaseConfiguration.java | 63 ------------------- .../dao/mapper/ConnectionFactoryTest.java | 33 ++++++++++ 5 files changed, 73 insertions(+), 78 deletions(-) delete mode 100644 dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/DatabaseConfiguration.java create mode 100644 dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/MybatisPlusConfig.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/MybatisPlusConfig.java index b5dff9c6da..26d1c5b39f 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/MybatisPlusConfig.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/MybatisPlusConfig.java @@ -1,3 +1,19 @@ +/* + * 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.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/YmlConfig.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/YmlConfig.java index 0caf7bc826..a2e0f65651 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/YmlConfig.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/config/YmlConfig.java @@ -1,3 +1,19 @@ +/* + * 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.config; @@ -10,12 +26,9 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; -/** - * Created by qiaozhanwei on 2019/9/17. - */ public class YmlConfig { - private static Map allMap=new HashMap(); + public static Map allMap=new HashMap(); static { Yaml yaml = new Yaml(); InputStream inputStream = YmlConfig.class.getResourceAsStream("/application.yml"); @@ -26,11 +39,6 @@ public class YmlConfig { } } - public static void main(String[] args) { - String ss = allMap.get("spring.datasource.url"); - System.out.println(ss); - } - public static void iteratorYml(Map map,String key){ Iterator iterator = map.entrySet().iterator(); while(iterator.hasNext()){ 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 951827714c..b9e9aecd15 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 @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.dao.datasource; import com.alibaba.druid.pool.DruidDataSource; +import org.apache.dolphinscheduler.dao.config.YmlConfig; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSession; @@ -29,7 +30,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.sql.DataSource; - +import java.util.Map; /** @@ -45,11 +46,11 @@ public class ConnectionFactory { */ public static DruidDataSource getDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); - - druidDataSource.setDriverClassName("com.mysql.jdbc.Driver"); - druidDataSource.setUrl("jdbc:mysql://192.168.220.188:3306/escheduler?useUnicode=true&characterEncoding=UTF-8"); - druidDataSource.setUsername("root"); - druidDataSource.setPassword("root@123"); + Map allMap = YmlConfig.allMap; + druidDataSource.setDriverClassName(allMap.get("spring.datasource.driver-class-name")); + druidDataSource.setUrl(allMap.get("spring.datasource.url")); + druidDataSource.setUsername(allMap.get("spring.datasource.username")); + druidDataSource.setPassword(allMap.get("spring.datasource.password")); druidDataSource.setInitialSize(5); druidDataSource.setMinIdle(5); druidDataSource.setMaxActive(20); diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/DatabaseConfiguration.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/DatabaseConfiguration.java deleted file mode 100644 index bd054be180..0000000000 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/DatabaseConfiguration.java +++ /dev/null @@ -1,63 +0,0 @@ -///* -// * 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.datasource; -// -//import com.alibaba.druid.pool.DruidDataSource; -//import org.apache.ibatis.session.SqlSessionFactory; -//import org.mybatis.spring.SqlSessionFactoryBean; -//import org.mybatis.spring.annotation.MapperScan; -//import org.springframework.context.annotation.Bean; -//import org.springframework.context.annotation.Configuration; -//import org.springframework.context.annotation.Primary; -//import org.springframework.context.annotation.PropertySource; -//import org.springframework.jdbc.datasource.DataSourceTransactionManager; -//import org.springframework.transaction.PlatformTransactionManager; -// -//import java.sql.SQLException; -// -///** -// * data base configuration -// */ -//@Configuration -//@PropertySource({"classpath:application.yml"}) -//@MapperScan(basePackages = "org.apache.dolphinscheduler.dao.mapper", sqlSessionFactoryRef = "SqlSessionFactory") -//public class DatabaseConfiguration { -// -// /** -// * register data source -// */ -// @Primary -// @Bean(name = "DataSource", initMethod = "init", destroyMethod = "close") -// public DruidDataSource dataSource() { -// return ConnectionFactory.getDataSource(); -// } -// -// @Primary -// @Bean(name = "SqlSessionFactory") -// public SqlSessionFactory sqlSessionFactory() throws Exception { -// SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); -// sqlSessionFactoryBean.setDataSource(dataSource()); -// -// return sqlSessionFactoryBean.getObject(); -// } -// -// @Primary -// @Bean(name = "TransactionManager") -// public PlatformTransactionManager transactionManager() throws SQLException { -// return new DataSourceTransactionManager(dataSource()); -// } -//} 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 new file mode 100644 index 0000000000..a674505c26 --- /dev/null +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ConnectionFactoryTest.java @@ -0,0 +1,33 @@ +/* + * 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.mapper; + +import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory; +import org.junit.Assert; +import org.junit.Test; + +import java.sql.Connection; + + +public class ConnectionFactoryTest { + + @Test + public void testConnection()throws Exception{ + Connection connection = ConnectionFactory.getDataSource().getPooledConnection().getConnection(); + Assert.assertEquals(connection != null , true); + } +}