annotate flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java @ 4772:f939e1e6cfa4

Sorted imports.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 05 Jan 2013 12:20:28 +0100
parents ee0c60757a94
children f0b3ad10f67f
rev   line source
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.db;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import de.intevation.utils.XML;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4
4772
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4086
diff changeset
5 import java.sql.Connection;
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4086
diff changeset
6 import java.sql.DatabaseMetaData;
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4086
diff changeset
7 import java.sql.DriverManager;
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4086
diff changeset
8 import java.sql.SQLException;
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4086
diff changeset
9
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 import java.util.HashMap;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import javax.xml.xpath.XPathConstants;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
4772
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4086
diff changeset
14 import org.apache.log4j.Logger;
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
4772
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4086
diff changeset
16 import org.w3c.dom.Document;
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 public class ConnectionBuilder
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 {
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 private static Logger log = Logger.getLogger(ConnectionBuilder.class);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 public static final String XPATH_DRIVER = "/sync/side[@name=$type]/db/driver/text()";
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 public static final String XPATH_USER = "/sync/side[@name=$type]/db/user/text()";
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 public static final String XPATH_PASSWORD = "/sync/side[@name=$type]/db/password/text()";
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 public static final String XPATH_URL = "/sync/side[@name=$type]/db/url/text()";
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 protected String type;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 protected String driver;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 protected String user;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 protected String password;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 protected String url;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 public ConnectionBuilder(String type, Document document) {
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 this.type = type;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 extractCredentials(document);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 protected void extractCredentials(Document document) {
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 HashMap<String, String> map = new HashMap<String, String>();
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 map.put("type", type);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 driver = (String)XML.xpath(
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 document, XPATH_DRIVER, XPathConstants.STRING, null, map);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 user = (String)XML.xpath(
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 document, XPATH_USER, XPathConstants.STRING, null, map);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 password = (String)XML.xpath(
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 document, XPATH_PASSWORD, XPathConstants.STRING, null, map);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 url = (String)XML.xpath(
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 document, XPATH_URL, XPathConstants.STRING, null, map);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 if (log.isDebugEnabled()) {
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 log.debug("driver: " + driver);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 log.debug("user: " + user);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 log.debug("password: *******");
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 log.debug("url: " + url);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 public Connection getConnection() throws SQLException {
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 if (driver != null && driver.length() > 0) {
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 try {
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 Class.forName(driver);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 catch (ClassNotFoundException cnfe) {
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 throw new SQLException(cnfe);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
4086
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
70 Connection connection =
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
71 DriverManager.getConnection(url, user, password);
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
72
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
73 connection.setAutoCommit(false);
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
74
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
75 DatabaseMetaData metaData = connection.getMetaData();
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
76
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
77 if (metaData.supportsTransactionIsolationLevel(
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
78 Connection.TRANSACTION_READ_UNCOMMITTED)) {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
79 connection.setTransactionIsolation(
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
80 Connection.TRANSACTION_READ_UNCOMMITTED);
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
81 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
82
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
83 return connection;
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 }
4073
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
85
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
86 public ConnectedStatements getConnectedStatements() throws SQLException {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
87 return new ConnectedStatements(
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
88 getConnection(),
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
89 new Statements(type, driver != null ? driver : "")
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
90 .getStatements());
4073
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
91 }
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org