Mercurial > dive4elements > framework
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DBConnection.java Sun Sep 06 12:00:56 2009 +0000 @@ -0,0 +1,78 @@ +package de.intevation.artifactdatabase; + +import javax.sql.DataSource; + +import java.sql.SQLException; + +import org.apache.commons.dbcp.BasicDataSource; + +/** + * @author Sascha L. Teichmann + */ +public class DBConnection +{ + public static final String DB_DRIVER = + "/artifacts-database/database/driver/text()"; + public static final String DB_URL = + "/artifacts-database/database/url/text()"; + public static final String DB_USER = + "/artifacts-database/database/driver/user/text()"; + public static final String DB_PASSWORD = + "/artifacts-database/database/driver/password()"; + + public static final String DEFAULT_DRIVER = + "org.h2.Driver"; + + public static final String DEFAULT_URL = + "jdbc:h2:artifacts.db"; + + public static final String DEFAULT_USER = ""; + public static final String DEFAULT_PASSWORD = ""; + + private DBConnection() { + } + + private static BasicDataSource dataSource; + + private static final void addShutdownHook() { + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + if (dataSource != null) { + try { + dataSource.close(); + } + catch (SQLException sqle) { + } + dataSource = null; + } + } + }); + } + + public static synchronized DataSource getDataSource() { + if (dataSource == null) { + dataSource = new BasicDataSource(); + + String driver = Config.getStringXPath( + DB_DRIVER, DEFAULT_DRIVER); + + String url = Config.getStringXPath( + DB_URL, DEFAULT_URL); + + String user = Config.getStringXPath( + DB_USER, DEFAULT_USER); + + String password = Config.getStringXPath( + DB_PASSWORD, DEFAULT_PASSWORD); + + dataSource.setDriverClassName(driver); + dataSource.setUsername(user); + dataSource.setPassword(password); + dataSource.setUrl(url); + addShutdownHook(); + } + + return dataSource; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: