diff --git a/fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSource.java b/fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSource.java index 26793c544..13c9ecdd6 100644 --- a/fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSource.java +++ b/fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSource.java @@ -168,6 +168,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat private volatile boolean keepAlive = false; + private SQLException initException = null; + public DruidDataSource() { this(false); } @@ -643,6 +645,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } public void init() throws SQLException { + if (initException != null) { + LOG.error("{dataSource-" + this.getID() + "} init error", initException); + throw initException; + } + if (inited) { return; } @@ -811,10 +818,15 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } } catch (SQLException e) { LOG.error("{dataSource-" + this.getID() + "} init error", e); + initException = e; throw e; } catch (InterruptedException e) { throw new SQLException(e.getMessage(), e); - } finally { + } catch (Throwable e) { + initException = new SQLException(e.getMessage()); + throw e; + } + finally { inited = true; lock.unlock(); diff --git a/fine-druid/src/test/java/com/fr/third/alibaba/druid/pool/DruidDataSourceTest.java b/fine-druid/src/test/java/com/fr/third/alibaba/druid/pool/DruidDataSourceTest.java new file mode 100644 index 000000000..e4e2b33b8 --- /dev/null +++ b/fine-druid/src/test/java/com/fr/third/alibaba/druid/pool/DruidDataSourceTest.java @@ -0,0 +1,40 @@ +package com.fr.third.alibaba.druid.pool; + +import com.fr.invoke.Reflect; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author lidongy + * @version 10.0 + * Created by lidongy on 2020/9/27 + */ +public class DruidDataSourceTest { + + @Test + public void testInitException() { + DruidDataSource dataSource = new DruidDataSource(); + Reflect.on(dataSource).set("maxEvictableIdleTimeMillis", 1); + Reflect.on(dataSource).set("minEvictableIdleTimeMillis", 2); + + + boolean flag1 = false; + boolean flag2 = false; + + try { + dataSource.init(); + } catch (Exception e) { + flag1 = true; + } + + //按照以前错误的逻辑,第二次init时就不会报错 + try { + dataSource.init(); + } catch (Exception e) { + flag2 = true; + } + + Assert.assertTrue(flag1); + Assert.assertTrue(flag2); + } +}