diff artifact-database/src/main/java/de/intevation/artifactdatabase/db/DBConnection.java @ 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 6d239c58c636
line wrap: on
line diff
--- 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