comparison artifact-database/src/main/java/de/intevation/artifactdatabase/DBConnection.java @ 10:e8626caac353

* Made Artifact life cycle symmetric: setup/endOfLife. * Implement defaults for Artifact and ArtifactFactory. * Added connection pooling from apache commons dbcp * Made sql schema of artifact database more compatible. * Improve example config. * Made artifactdb start with 'mvn exec:exec' * minor fixes. artifacts/trunk@25 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 06 Sep 2009 12:00:56 +0000
parents
children af07d004d320
comparison
equal deleted inserted replaced
9:a5a279a0ee35 10:e8626caac353
1 package de.intevation.artifactdatabase;
2
3 import javax.sql.DataSource;
4
5 import java.sql.SQLException;
6
7 import org.apache.commons.dbcp.BasicDataSource;
8
9 /**
10 * @author Sascha L. Teichmann
11 */
12 public class DBConnection
13 {
14 public static final String DB_DRIVER =
15 "/artifacts-database/database/driver/text()";
16 public static final String DB_URL =
17 "/artifacts-database/database/url/text()";
18 public static final String DB_USER =
19 "/artifacts-database/database/driver/user/text()";
20 public static final String DB_PASSWORD =
21 "/artifacts-database/database/driver/password()";
22
23 public static final String DEFAULT_DRIVER =
24 "org.h2.Driver";
25
26 public static final String DEFAULT_URL =
27 "jdbc:h2:artifacts.db";
28
29 public static final String DEFAULT_USER = "";
30 public static final String DEFAULT_PASSWORD = "";
31
32 private DBConnection() {
33 }
34
35 private static BasicDataSource dataSource;
36
37 private static final void addShutdownHook() {
38 Runtime.getRuntime().addShutdownHook(new Thread() {
39 public void run() {
40 if (dataSource != null) {
41 try {
42 dataSource.close();
43 }
44 catch (SQLException sqle) {
45 }
46 dataSource = null;
47 }
48 }
49 });
50 }
51
52 public static synchronized DataSource getDataSource() {
53 if (dataSource == null) {
54 dataSource = new BasicDataSource();
55
56 String driver = Config.getStringXPath(
57 DB_DRIVER, DEFAULT_DRIVER);
58
59 String url = Config.getStringXPath(
60 DB_URL, DEFAULT_URL);
61
62 String user = Config.getStringXPath(
63 DB_USER, DEFAULT_USER);
64
65 String password = Config.getStringXPath(
66 DB_PASSWORD, DEFAULT_PASSWORD);
67
68 dataSource.setDriverClassName(driver);
69 dataSource.setUsername(user);
70 dataSource.setPassword(password);
71 dataSource.setUrl(url);
72 addShutdownHook();
73 }
74
75 return dataSource;
76 }
77 }
78 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org