From 43fa15f6686d41ea9e8fe06f486cbc636ac7c00d Mon Sep 17 00:00:00 2001 From: sandzhang Date: Fri, 24 Aug 2012 12:22:21 +0800 Subject: [PATCH] fix init check for mysql --- .../alibaba/druid/pool/DruidDataSource.java | 4 +- .../druid/bvt/pool/ConfigErrorTestMysql.java | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/ConfigErrorTestMysql.java diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java index bd2457336d..452313f6bd 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java @@ -526,8 +526,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat this.getConnectProperties().setProperty("oracle.jdbc.FreeMemoryOnEnterImplicitCache", "true"); } } else if (JdbcUtils.MYSQL.equals(this.dbType)) { - if (LOG.isWarnEnabled()) { - LOG.warn("Your dbType is mysql, recommend set testWhileIdle is true and validationQuery shoud not be null"); + if (!this.isTestWhileIdle()) { + if (LOG.isWarnEnabled()) LOG.warn("Your dbType is mysql, recommend set testWhileIdle is true"); } } } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/ConfigErrorTestMysql.java b/src/test/java/com/alibaba/druid/bvt/pool/ConfigErrorTestMysql.java new file mode 100644 index 0000000000..4439dc4cd9 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/ConfigErrorTestMysql.java @@ -0,0 +1,54 @@ +package com.alibaba.druid.bvt.pool; + +import java.lang.reflect.Field; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.support.logging.Log; + +public class ConfigErrorTestMysql extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mysql:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setInitialSize(0); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_success() throws Exception { + dataSource.setTestWhileIdle(true); + + Field field = DruidDataSource.class.getDeclaredField("LOG"); + dataSource.setValidationQuery("select 1"); + field.setAccessible(true); + Log LOG = (Log) field.get(null); + + LOG.resetStat(); + + Assert.assertEquals(0, LOG.getWarnCount()); + dataSource.init(); + Assert.assertEquals(0, LOG.getWarnCount()); + } + + public void test_warn() throws Exception { + dataSource.setTestWhileIdle(false); + + Field field = DruidDataSource.class.getDeclaredField("LOG"); + field.setAccessible(true); + Log LOG = (Log) field.get(null); + + LOG.resetStat(); + + Assert.assertEquals(0, LOG.getWarnCount()); + dataSource.init(); + Assert.assertEquals(1, LOG.getWarnCount()); + } +}