comparison artifact-database/src/main/java/de/intevation/artifactdatabase/SQLExecutor.java @ 174:25d472a67a9f

Reduce the code repetition and the complexity of the backend. artifacts/trunk@1400 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 04 Mar 2011 16:43:03 +0000
parents
children
comparison
equal deleted inserted replaced
173:940ed629419f 174:25d472a67a9f
1 /*
2 * Copyright (c) 2011 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8 package de.intevation.artifactdatabase;
9
10 import java.sql.Connection;
11 import java.sql.PreparedStatement;
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14
15 import javax.sql.DataSource;
16
17 import org.apache.log4j.Logger;
18
19 public class SQLExecutor {
20
21 private static Logger logger = Logger.getLogger(SQLExecutor.class);
22
23 public Connection conn;
24 public PreparedStatement stmnt;
25 public ResultSet result;
26
27 public SQLExecutor() {
28 }
29
30 public void reset() throws SQLException {
31 if (result != null) {
32 result.close();
33 result = null;
34 }
35 if (stmnt != null) {
36 result = null;
37 stmnt.close();
38 }
39 }
40
41 public PreparedStatement prepareStatement(String query)
42 throws SQLException {
43 return stmnt = conn.prepareStatement(query);
44 }
45
46 public void close() {
47 if (result != null) {
48 try { result.close(); }
49 catch (SQLException sqle) {}
50 }
51 if (stmnt != null) {
52 try { stmnt.close(); }
53 catch (SQLException sqle) {}
54 }
55 if (conn != null) {
56 try { conn.close(); }
57 catch (SQLException sqle) {}
58 }
59 }
60
61 public boolean runWrite() {
62 DataSource dataSource = DBConnection.getDataSource();
63 try {
64 conn = dataSource.getConnection();
65 try {
66 conn.setAutoCommit(false);
67 return doIt();
68 }
69 catch (SQLException sqle) {
70 conn.rollback();
71 throw sqle;
72 }
73 }
74 catch (SQLException sqle) {
75 logger.error(sqle.getLocalizedMessage(), sqle);
76 }
77 finally {
78 close();
79 }
80 return false;
81 }
82
83 public boolean runRead() {
84 DataSource dataSource = DBConnection.getDataSource();
85 try {
86 conn = dataSource.getConnection();
87 return doIt();
88 }
89 catch (SQLException sqle) {
90 logger.error(sqle.getLocalizedMessage(), sqle);
91 }
92 finally {
93 close();
94 }
95 return false;
96 }
97
98 public boolean doIt() throws SQLException {
99 return true;
100 }
101 }
102 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org