diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/basic/BasicDataSourceCreator.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/basic/BasicDataSourceCreator.java index b5f80bc0..47775b3d 100644 --- a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/basic/BasicDataSourceCreator.java +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/basic/BasicDataSourceCreator.java @@ -87,7 +87,7 @@ public DataSource createDataSource(DataSourceProperty dataSourceProperty) { return (DataSource) buildMethod.invoke(o6); } catch (Exception e) { throw new ErrorCreateDataSourceException( - "dynamic-datasource create basic database named " + dataSourceProperty.getPoolName() + " error"); + "dynamic-datasource create datasource named [" + dataSourceProperty.getPoolName() + "] error", e); } } diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/beecp/BeeCpDataSourceCreator.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/beecp/BeeCpDataSourceCreator.java index d823bd37..14563413 100644 --- a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/beecp/BeeCpDataSourceCreator.java +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/beecp/BeeCpDataSourceCreator.java @@ -20,6 +20,7 @@ import com.baomidou.dynamic.datasource.creator.DataSourceCreator; import com.baomidou.dynamic.datasource.creator.DataSourceProperty; import com.baomidou.dynamic.datasource.enums.DdConstants; +import com.baomidou.dynamic.datasource.exception.ErrorCreateDataSourceException; import com.baomidou.dynamic.datasource.toolkit.ConfigMergeCreator; import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import lombok.AllArgsConstructor; @@ -66,7 +67,12 @@ public DataSource createDataSource(DataSourceProperty dataSourceProperty) { config.setDriverClassName(driverClassName); } if (Boolean.FALSE.equals(dataSourceProperty.getLazy())) { - return new BeeDataSource(config); + try { + return new BeeDataSource(config); + } catch (Exception e) { + throw new ErrorCreateDataSourceException( + "dynamic-datasource create datasource named [" + dataSourceProperty.getPoolName() + "] error", e); + } } BeeDataSource beeDataSource = new BeeDataSource(); try { diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/dbcp/Dbcp2DataSourceCreator.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/dbcp/Dbcp2DataSourceCreator.java index 206e558f..f6876781 100644 --- a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/dbcp/Dbcp2DataSourceCreator.java +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/dbcp/Dbcp2DataSourceCreator.java @@ -18,6 +18,7 @@ import com.baomidou.dynamic.datasource.creator.DataSourceCreator; import com.baomidou.dynamic.datasource.creator.DataSourceProperty; import com.baomidou.dynamic.datasource.enums.DdConstants; +import com.baomidou.dynamic.datasource.exception.ErrorCreateDataSourceException; import com.baomidou.dynamic.datasource.toolkit.ConfigMergeCreator; import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import lombok.AllArgsConstructor; @@ -53,7 +54,13 @@ public DataSource createDataSource(DataSourceProperty dataSourceProperty) { dataSource.setDriverClassName(driverClassName); } if (Boolean.FALSE.equals(dataSourceProperty.getLazy())) { - dataSource.start(); + try { + dataSource.start(); + }catch (Exception e) { + + throw new ErrorCreateDataSourceException( + "dynamic-datasource create Dbcp2 database named " + dataSourceProperty.getPoolName() + " error", e); + } } return dataSource; } diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/druid/DruidDataSourceCreator.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/druid/DruidDataSourceCreator.java index 81f8a26a..0b94dd88 100644 --- a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/druid/DruidDataSourceCreator.java +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/druid/DruidDataSourceCreator.java @@ -123,7 +123,7 @@ public DataSource createDataSource(DataSourceProperty dataSourceProperty) { try { configMethod.invoke(dataSource, properties); } catch (Exception ignore) { - + // Druid only prints logs when copying property errors } //连接参数单独设置 dataSource.setConnectProperties(config.getConnectionProperties()); @@ -135,8 +135,9 @@ public DataSource createDataSource(DataSourceProperty dataSourceProperty) { if (Boolean.FALSE.equals(dataSourceProperty.getLazy())) { try { dataSource.init(); - } catch (SQLException e) { - throw new ErrorCreateDataSourceException("druid create error", e); + } catch (Exception e) { + throw new ErrorCreateDataSourceException( + "dynamic-datasource create datasource named [" + dataSourceProperty.getPoolName() + "] error", e); } } return dataSource; diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/hikaricp/HikariDataSourceCreator.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/hikaricp/HikariDataSourceCreator.java index fa37c347..b906f064 100644 --- a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/hikaricp/HikariDataSourceCreator.java +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/hikaricp/HikariDataSourceCreator.java @@ -18,6 +18,7 @@ import com.baomidou.dynamic.datasource.creator.DataSourceCreator; import com.baomidou.dynamic.datasource.creator.DataSourceProperty; import com.baomidou.dynamic.datasource.enums.DdConstants; +import com.baomidou.dynamic.datasource.exception.ErrorCreateDataSourceException; import com.baomidou.dynamic.datasource.toolkit.ConfigMergeCreator; import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import com.zaxxer.hikari.HikariConfig; @@ -82,14 +83,19 @@ public DataSource createDataSource(DataSourceProperty dataSourceProperty) { config.setDriverClassName(driverClassName); } if (Boolean.FALSE.equals(dataSourceProperty.getLazy())) { - return new HikariDataSource(config); + try { + return new HikariDataSource(config); + }catch (Exception e){ + throw new ErrorCreateDataSourceException( + "dynamic-datasource create datasource named [" + dataSourceProperty.getPoolName() + "] error", e); + } } config.validate(); HikariDataSource dataSource = new HikariDataSource(); try { configCopyMethod.invoke(config, dataSource); } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException("HikariConfig failed to copy to HikariDataSource", e); + throw new ErrorCreateDataSourceException("HikariConfig failed to copy to HikariDataSource", e); } return dataSource; }