View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
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 }