changeset 309:86dd32b45d87

Use real db connection pooling for artifact database. artifacts/trunk@2429 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 31 Jul 2011 18:43:54 +0000
parents a077bb098eb4
children 63122b9dee1d
files ChangeLog artifact-database/pom.xml artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifact-database/src/main/java/de/intevation/artifactdatabase/db/DBConnection.java
diffstat 4 files changed, 36 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Jul 31 17:28:07 2011 +0000
+++ b/ChangeLog	Sun Jul 31 18:43:54 2011 +0000
@@ -1,3 +1,13 @@
+2011-07-31	Sascha L. Teichmann	<teichmann@intevation.de>
+
+	* artifact-database/pom.xml: Bumped Apache DBCP up to 1.4
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/db/DBConnection.java:
+	  Use real pooling to void races. Maybe it needs more configuration options!?
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java: Removed
+	  superfluous imports.
+
 2011-07-31	Sascha L. Teichmann	<teichmann@intevation.de>
 
 	* artifact-database/src/main/resources/sql/org-h2-driver.properties,
--- a/artifact-database/pom.xml	Sun Jul 31 17:28:07 2011 +0000
+++ b/artifact-database/pom.xml	Sun Jul 31 18:43:54 2011 +0000
@@ -82,7 +82,7 @@
     <dependency>
       <groupId>commons-dbcp</groupId>
       <artifactId>commons-dbcp</artifactId>
-      <version>1.2.2</version>
+      <version>1.4</version>
     </dependency>
     <dependency>
       <groupId>log4j</groupId>
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Sun Jul 31 17:28:07 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Sun Jul 31 18:43:54 2011 +0000
@@ -30,8 +30,6 @@
 
 import java.util.ArrayList;
 import java.util.Date;
-import java.util.Map;
-import java.util.LinkedHashMap;
 import java.util.HashMap;
 
 import org.apache.log4j.Logger;
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/db/DBConnection.java	Sun Jul 31 17:28:07 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/db/DBConnection.java	Sun Jul 31 18:43:54 2011 +0000
@@ -1,12 +1,16 @@
 package de.intevation.artifactdatabase.db;
 
-import java.sql.SQLException;
-
 import javax.sql.DataSource;
 
 import java.io.File;
 
-import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.commons.pool.ObjectPool;
+
+import org.apache.commons.pool.impl.GenericObjectPool;
+
+import org.apache.commons.dbcp.DriverManagerConnectionFactory;
+import org.apache.commons.dbcp.PoolableConnectionFactory;
+import org.apache.commons.dbcp.PoolingDataSource;
 
 import de.intevation.artifacts.common.utils.Config;
 
@@ -28,7 +32,7 @@
         return "jdbc:h2:" + databaseFile;
     }
 
-    protected BasicDataSource dataSource;
+    protected DataSource dataSource;
 
     protected String driver;
     protected String url;
@@ -82,22 +86,6 @@
         this.url = url;
     }
 
-    protected void addShutdownHook() {
-        Runtime.getRuntime().addShutdownHook(new Thread() {
-            @Override
-            public void run() {
-                if (dataSource != null) {
-                    try {
-                        dataSource.close();
-                    }
-                    catch (SQLException sqle) {
-                    }
-                    dataSource = null;
-                }
-            }
-        });
-    }
-
     public synchronized DataSource getDataSource() {
         if (dataSource == null) {
             if (log.isDebugEnabled()) {
@@ -106,13 +94,24 @@
                 log.debug(" url   : " + url);
                 log.debug(" user  : " + user);
             }
-            dataSource = new BasicDataSource();
 
-            dataSource.setDriverClassName(driver);
-            dataSource.setUsername(user);
-            dataSource.setPassword(password);
-            dataSource.setUrl(url);
-            addShutdownHook();
+            try {
+                Class.forName(driver);
+            }
+            catch (ClassNotFoundException cnfe) {
+                log.error("cannot load driver", cnfe);
+                return null;
+            }
+
+            DriverManagerConnectionFactory dmcf =
+                new DriverManagerConnectionFactory(url, user, password);
+
+            ObjectPool cp = new GenericObjectPool();
+
+            PoolableConnectionFactory pcf = new PoolableConnectionFactory(
+                dmcf, cp, null, null, false, false);
+
+            dataSource = new PoolingDataSource(cp);
         }
         return dataSource;
     }

http://dive4elements.wald.intevation.org