From 71f39fa8751252c9b665213e2f3a4a1827da2a27 Mon Sep 17 00:00:00 2001 From: ruanwenjun <861923274@qq.com> Date: Thu, 6 May 2021 09:41:44 +0800 Subject: [PATCH] [Improvement][Dao] Database add connection check (#5408) (#5409) --- .../dao/datasource/ConnectionFactory.java | 10 ++++++---- .../dao/datasource/SpringConnectionFactory.java | 6 ++++-- 2 files changed, 10 insertions(+), 6 deletions(-) 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 2664273724..25fe5927d3 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 @@ -26,12 +26,14 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; -import org.mybatis.spring.SqlSessionTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + +import java.sql.SQLException; import javax.sql.DataSource; +import org.mybatis.spring.SqlSessionTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * not spring manager connection, only use for init db, and alert module for non-spring application @@ -81,7 +83,7 @@ public class ConnectionFactory extends SpringConnectionFactory { * * @return druid dataSource */ - private DataSource buildDataSource() { + private DataSource buildDataSource() throws SQLException { DruidDataSource druidDataSource = dataSource(); return druidDataSource; diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/SpringConnectionFactory.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/SpringConnectionFactory.java index 5a0e6353d0..a58955da19 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/SpringConnectionFactory.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/SpringConnectionFactory.java @@ -28,6 +28,7 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.type.JdbcType; +import java.sql.SQLException; import java.util.Properties; import org.mybatis.spring.SqlSessionTemplate; @@ -77,7 +78,7 @@ public class SpringConnectionFactory { * @return druid dataSource */ @Bean(destroyMethod = "") - public DruidDataSource dataSource() { + public DruidDataSource dataSource() throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); @@ -104,6 +105,7 @@ public class SpringConnectionFactory { druidDataSource.setValidationQueryTimeout(PropertyUtils.getInt(Constants.SPRING_DATASOURCE_VALIDATION_QUERY_TIMEOUT, 3)); //auto commit druidDataSource.setDefaultAutoCommit(PropertyUtils.getBoolean(Constants.SPRING_DATASOURCE_DEFAULT_AUTO_COMMIT, true)); + druidDataSource.init(); return druidDataSource; } @@ -113,7 +115,7 @@ public class SpringConnectionFactory { * @return DataSourceTransactionManager */ @Bean - public DataSourceTransactionManager transactionManager() { + public DataSourceTransactionManager transactionManager() throws SQLException { return new DataSourceTransactionManager(dataSource()); }