1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package pl.kernelpanic.dbmonster.connection;
48
49 import java.sql.Connection;
50 import java.sql.PreparedStatement;
51 import java.sql.ResultSet;
52 import java.sql.Statement;
53 import java.sql.SQLException;
54
55 /***
56 * This class provides an unified interface for JDBC connections.
57 * It wraps standard JDBC calls.
58 *
59 * @author Piotr Maj <piotr.maj@kernelpanic.pl>
60 *
61 * @version $Id: Transaction.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
62 */
63 public class Transaction {
64
65 /***
66 * The connection.
67 */
68 private Connection conn = null;
69
70 /***
71 * Statement.
72 */
73 private Statement stmt = null;
74
75 /***
76 * Prepared statement.
77 */
78 private PreparedStatement pstmt = null;
79
80 /***
81 * A result set.
82 */
83 private ResultSet rs = null;
84
85 /***
86 * The connection provider.
87 */
88 private ConnectionProvider connProvider = null;
89
90 /***
91 * Constructs a new connection provider.
92 *
93 * @param cp a connection provider
94 */
95 public Transaction(final ConnectionProvider cp) {
96 connProvider = cp;
97 }
98
99 /***
100 * Starts the JDBC transaction.
101 *
102 * @return a connection
103 *
104 * @throws SQLException if connection cannot be established
105 */
106 public final Connection begin() throws SQLException {
107 conn = connProvider.getConnection();
108 return conn;
109 }
110
111 /***
112 * Commits the transaction.
113 *
114 * @throws SQLException if connection cannot be established
115 */
116 public final void commit() throws SQLException {
117 if (rs != null) {
118 rs.close();
119 }
120
121 if (stmt != null) {
122 stmt.close();
123 }
124
125 if (!conn.getAutoCommit()) {
126 conn.commit();
127 }
128 }
129
130 /***
131 * Aborts the connection.
132 */
133 public final void abort() {
134 try {
135 conn.rollback();
136 } catch (Exception e) {
137
138 }
139 }
140
141 /***
142 * Closes the connection.
143 */
144 public final void close() {
145 if (rs != null) {
146 try {
147 rs.close();
148 } catch (SQLException e) {
149
150 } finally {
151 rs = null;
152 }
153 }
154
155 if (stmt != null) {
156 try {
157 stmt.close();
158 } catch (SQLException e) {
159
160 } finally {
161 stmt = null;
162 }
163 }
164
165 if (pstmt != null) {
166 try {
167 pstmt.close();
168 } catch (SQLException e) {
169
170 } finally {
171 pstmt = null;
172 }
173 }
174
175 if (conn != null) {
176 try {
177 conn.close();
178 } catch (SQLException e) {
179
180 } finally {
181 conn = null;
182 }
183 }
184 }
185
186 /***
187 * Executes query.
188 *
189 * @param query the query string
190 *
191 * @return result set
192 *
193 * @throws SQLException if somethig goes wrong
194 */
195 public final ResultSet executeQuery(final String query)
196 throws SQLException {
197
198 stmt = conn.createStatement();
199 rs = stmt.executeQuery(query);
200 return rs;
201 }
202
203 /***
204 * Prepares statement.
205 *
206 * @param query the query
207 *
208 * @return a statement
209 *
210 * @throws SQLException if query cannot be prepared
211 */
212 public final PreparedStatement prepareStatement(final String query)
213 throws SQLException {
214 pstmt = conn.prepareStatement(query);
215 return pstmt;
216 }
217
218 /***
219 * Executes a prepared statement. Used only for updates.
220 *
221 * @throws SQLException if query could not be executed.
222 */
223 public final void execute() throws SQLException {
224 if (pstmt != null) {
225 pstmt.execute();
226 }
227 }
228 }