From aec3c93c498fe95d9e1f3cc716cac38af216a49c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E8=BF=9B=E4=B8=BE?= Date: Tue, 2 Feb 2016 23:53:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AF=B9simple-jndi?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C=E5=B0=B1=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E5=9C=A8kettle=E7=9A=84simple-jndi=E4=B8=AD=E9=85=8D=E7=BD=AEd?= =?UTF-8?q?ruid=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E=E6=8E=A5=E6=B1=A0?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../druid/pool/DruidDataSourceConverter.java | 70 +++++++++++++++++++ .../pool/DruidDataSourceConverterTest.java | 43 ++++++++++++ .../druid/pool/simplejndi/jdbc.properties | 16 +++++ 4 files changed, 135 insertions(+) create mode 100644 src/main/java/com/alibaba/druid/pool/DruidDataSourceConverter.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceConverterTest.java create mode 100644 src/test/resources/com/alibaba/druid/pool/simplejndi/jdbc.properties diff --git a/pom.xml b/pom.xml index 33533a1173..97ce137a2a 100644 --- a/pom.xml +++ b/pom.xml @@ -317,6 +317,12 @@ + + simple-jndi + simple-jndi + 0.11.4.1 + provided + javax.transaction jta diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSourceConverter.java b/src/main/java/com/alibaba/druid/pool/DruidDataSourceConverter.java new file mode 100644 index 0000000000..6d799e64ac --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSourceConverter.java @@ -0,0 +1,70 @@ +/* + * Copyright 1999-2101 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.druid.pool; + +import java.sql.SQLException; +import java.util.Properties; + +import org.osjava.sj.loader.convert.Converter; + +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; + +/** + * 支持simple-jndi
+ *

配置示例:

+ *
+pgDruidTest/converter=com.alibaba.druid.pool.DruidDataSourceConverter
+pgDruidTest/type=javax.sql.DataSource
+pgDruidTest/driverClassName=org.postgresql.Driver
+pgDruidTest/url=jdbc:postgresql://127.0.0.1:5432/kettleRep
+pgDruidTest/username=postgres
+pgDruidTest/password=postgres
+pgDruidTest/maxActive=50
+pgDruidTest/minIdle=10
+pgDruidTest/initialSize=5
+pgDruidTest/validationQuery=SELECT 1
+pgDruidTest/maxWait=10000
+pgDruidTest/removeabandoned=true
+pgDruidTest/removeabandonedtimeout=60
+pgDruidTest/logabandoned=false
+pgDruidTest/filters=stat,config,log4j
+pgDruidTest/connectionProperties=druid.log.stmt.executableSql=true
+   
+ * date: 2016年1月31日 下午12:54:10
+ * @author jinjuma@yeah.net + */ +public class DruidDataSourceConverter implements Converter { + + private final static Log LOG = LogFactory.getLog(DruidDataSourceConverter.class); + /** + * + * @see org.osjava.sj.loader.convert.Converter#convert(java.util.Properties, java.lang.String) + */ + @Override + public Object convert(Properties properties, String type) { + try { + DruidDataSource dataSource = new DruidDataSource(); + DruidDataSourceFactory.config(dataSource, properties); + return dataSource; + } catch (SQLException e) { + LOG.error("properties:"+properties, e); + } + return null; + } + +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceConverterTest.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceConverterTest.java new file mode 100644 index 0000000000..89e3a4a49a --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceConverterTest.java @@ -0,0 +1,43 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; +import java.util.Properties; + +import junit.framework.TestCase; + +import org.junit.Assert; +import org.osjava.sj.SimpleContext; + +import com.alibaba.druid.pool.DruidDataSource; + +public class DruidDataSourceConverterTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + String root = DruidDataSourceConverterTest.class + .getResource("/com/alibaba/druid/pool/simplejndi/").toString(); + if (root.startsWith("file:/")) + root = root.substring("file://".length() - 1); + Properties props = new Properties(); + props.put("org.osjava.sj.root", root); + props.put("java.naming.factory.initial", + "org.osjava.sj.SimpleContextFactory"); + props.put("org.osjava.sj.delimiter", "/"); + javax.naming.Context ctx = new SimpleContext(props); + dataSource = (DruidDataSource) ctx.lookup("jdbc/druidTest"); + dataSource.init(); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_conn() throws Exception { + Assert.assertEquals(true, dataSource.isInited()); + Connection conn = dataSource.getConnection(); + Assert.assertEquals(1, dataSource.getActiveCount()); + conn.close(); + Assert.assertEquals(0, dataSource.getActiveCount()); + } +} diff --git a/src/test/resources/com/alibaba/druid/pool/simplejndi/jdbc.properties b/src/test/resources/com/alibaba/druid/pool/simplejndi/jdbc.properties new file mode 100644 index 0000000000..6f3e896c91 --- /dev/null +++ b/src/test/resources/com/alibaba/druid/pool/simplejndi/jdbc.properties @@ -0,0 +1,16 @@ +druidTest/converter=com.alibaba.druid.pool.DruidDataSourceConverter +druidTest/type=javax.sql.DataSource +druidTest/driverClassName=com.alibaba.druid.mock.MockDriver +druidTest/url=jdbc:fake:dragoon_v25masterdb +druidTest/username=dragoon25 +druidTest/password=dragoon25 +druidTest/maxActive=50 +druidTest/minIdle=10 +druidTest/initialSize=5 +druidTest/validationQuery=SELECT 1 +druidTest/maxWait=10000 +druidTest/removeabandoned=true +druidTest/removeabandonedtimeout=60 +druidTest/logabandoned=false +druidTest/filters=stat,config,log4j +druidTest/connectionProperties=druid.log.stmt.executableSql=true;druid.stat.logSlowSql=true From ccf611bca0dbdb4e2d9f24d0bb9b68e3251ae0f2 Mon Sep 17 00:00:00 2001 From: FuZhenn Date: Wed, 9 Mar 2016 16:01:37 +0800 Subject: [PATCH 2/2] Add exception handling when closing statement --- .../alibaba/druid/pool/DruidPooledConnection.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java b/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java index b208ae32be..cbe3ae58ea 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java @@ -174,8 +174,16 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql. stmt.setClosed(true); // soft set close } else { - stmt.closeInternal(); - holder.getDataSource().incrementClosedPreparedStatementCount(); + try { + //Connection behind the statement may be in invalid state, which will throw a SQLException. + //In this case, the exception is desired to be properly handled to remove the unusable connection from the pool. + stmt.closeInternal(); + } catch (SQLException ex) { + this.handleException(ex); + throw ex; + } finally { + holder.getDataSource().incrementClosedPreparedStatementCount(); + } } }