comparison 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
comparison
equal deleted inserted replaced
308:a077bb098eb4 309:86dd32b45d87
1 package de.intevation.artifactdatabase.db; 1 package de.intevation.artifactdatabase.db;
2
3 import java.sql.SQLException;
4 2
5 import javax.sql.DataSource; 3 import javax.sql.DataSource;
6 4
7 import java.io.File; 5 import java.io.File;
8 6
9 import org.apache.commons.dbcp.BasicDataSource; 7 import org.apache.commons.pool.ObjectPool;
8
9 import org.apache.commons.pool.impl.GenericObjectPool;
10
11 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
12 import org.apache.commons.dbcp.PoolableConnectionFactory;
13 import org.apache.commons.dbcp.PoolingDataSource;
10 14
11 import de.intevation.artifacts.common.utils.Config; 15 import de.intevation.artifacts.common.utils.Config;
12 16
13 import org.apache.log4j.Logger; 17 import org.apache.log4j.Logger;
14 18
26 File configDir = Config.getConfigDirectory(); 30 File configDir = Config.getConfigDirectory();
27 File databaseFile = new File(configDir, DEFAULT_DATABASE_FILE); 31 File databaseFile = new File(configDir, DEFAULT_DATABASE_FILE);
28 return "jdbc:h2:" + databaseFile; 32 return "jdbc:h2:" + databaseFile;
29 } 33 }
30 34
31 protected BasicDataSource dataSource; 35 protected DataSource dataSource;
32 36
33 protected String driver; 37 protected String driver;
34 protected String url; 38 protected String url;
35 protected String user; 39 protected String user;
36 protected String password; 40 protected String password;
80 84
81 public void setUrl(String url) { 85 public void setUrl(String url) {
82 this.url = url; 86 this.url = url;
83 } 87 }
84 88
85 protected void addShutdownHook() {
86 Runtime.getRuntime().addShutdownHook(new Thread() {
87 @Override
88 public void run() {
89 if (dataSource != null) {
90 try {
91 dataSource.close();
92 }
93 catch (SQLException sqle) {
94 }
95 dataSource = null;
96 }
97 }
98 });
99 }
100
101 public synchronized DataSource getDataSource() { 89 public synchronized DataSource getDataSource() {
102 if (dataSource == null) { 90 if (dataSource == null) {
103 if (log.isDebugEnabled()) { 91 if (log.isDebugEnabled()) {
104 log.debug("create new datasource:"); 92 log.debug("create new datasource:");
105 log.debug(" driver: " + driver); 93 log.debug(" driver: " + driver);
106 log.debug(" url : " + url); 94 log.debug(" url : " + url);
107 log.debug(" user : " + user); 95 log.debug(" user : " + user);
108 } 96 }
109 dataSource = new BasicDataSource();
110 97
111 dataSource.setDriverClassName(driver); 98 try {
112 dataSource.setUsername(user); 99 Class.forName(driver);
113 dataSource.setPassword(password); 100 }
114 dataSource.setUrl(url); 101 catch (ClassNotFoundException cnfe) {
115 addShutdownHook(); 102 log.error("cannot load driver", cnfe);
103 return null;
104 }
105
106 DriverManagerConnectionFactory dmcf =
107 new DriverManagerConnectionFactory(url, user, password);
108
109 ObjectPool cp = new GenericObjectPool();
110
111 PoolableConnectionFactory pcf = new PoolableConnectionFactory(
112 dmcf, cp, null, null, false, false);
113
114 dataSource = new PoolingDataSource(cp);
116 } 115 }
117 return dataSource; 116 return dataSource;
118 } 117 }
119 } 118 }
120 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 119 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org