From 448c3a8171e9c2f5f2e47173ae43a0e99bc8d3f4 Mon Sep 17 00:00:00 2001 From: lidongy <1879087903@qq.com> Date: Sun, 27 Sep 2020 15:52:13 +0800 Subject: [PATCH] =?UTF-8?q?DEC-15199=20=E5=90=8C=E6=A0=B7=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=88=9B=E5=BB=BA=E7=9A=84datasource=EF=BC=8C?= =?UTF-8?q?=E9=A6=96=E6=AC=A1init=E6=8A=A5=E9=94=99=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1init=E4=B8=8D=E4=BC=9A=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/druid/pool/DruidDataSource.java | 8 ++++ .../druid/pool/DruidDataSourceTest.java | 40 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 fine-druid/src/test/java/com/fr/third/alibaba/druid/pool/DruidDataSourceTest.java 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..c1c3b1237 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,6 +818,7 @@ 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); 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); + } +}