KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > db > TKDBConnection


1 /*
2  * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/db/TKDBConnection.java,v 1.14 2002/02/16 15:32:33 alex Exp $
3  *
4  */

5 package com.teamkonzept.db;
6
7 import java.sql.*;
8 import java.util.Vector JavaDoc;
9
10 import com.teamkonzept.lib.*;
11
12 import org.apache.log4j.Category;
13
14 /** Klasse zur Verwaltung von Datenstrukturen, die im direkten Zusammenhang mit der
15  * JDBC-Connection stehen. Z.B f¸r die Transaktionsverwaltung verwendete Datenstrukturen.
16  *
17  * @author
18  * @version
19  */

20 public class TKDBConnection
21 {
22     /** Logging Category */
23     private static final Category CAT = Category.getInstance(TKDBConnection.class);
24
25     /* Die Connection zur DB */
26     Connection conn;
27
28     /** Connection manager */
29     TKDBConnectionManager connectionManager;
30
31
32     public TKHashtable prepQueries = null; // of queryID, PreparedQuery for reuse of Prepared Statements
33

34     /* Information ob noch eine als sensitve markierte Query offen ist */
35     private boolean sensitiveQueryOpen;
36
37     /** Transaktionsstatus der Verbindung (aus Performancegründen hier gehalten)
38         JDBC definiert, dass eine Connection initial autoCommit ist
39     */

40     private boolean autoCommit = true;
41
42     /* Enthaelt alle anderen offenen Queries. */
43     private Vector JavaDoc nonSensitiveQueries = new Vector JavaDoc();
44
45     /* Gibt an ob eine Transaktion offen ist. */
46     private boolean transactionIsOpen= false;
47
48
49     public TKDBConnection( Connection conn, TKDBConnectionManager manager){
50         this.conn = conn;
51         this.connectionManager = manager;
52     }
53
54     public boolean isAutoCommit(){
55         return autoCommit;
56     }
57
58     public void setAutoCommit(boolean autoCommit) throws SQLException
59     {
60         this.autoCommit = autoCommit;
61         conn.setAutoCommit(autoCommit);
62     }
63
64     public boolean connectionIsClosed() throws SQLException{
65         if (conn == null){
66             throw new TKSQLError( " No connection in TKDBConnection "); // %%change
67
}
68         return conn.isClosed();
69     }
70
71
72     public TKHashtable getPrepQueries(){
73         if ( prepQueries == null){
74             prepQueries = new TKHashtable();
75         }
76         return prepQueries;
77     }
78
79
80     public Connection getConnection(){
81         if ( conn == null) {
82             throw new TKSQLError( " No connection in TKDBConnection "); // %% change
83
}
84         return conn;
85     }
86
87
88
89     /** Method to store a TKPreparedQuery Object for later reuse.
90     */

91
92   public void storePrepQuery(TKPrepQuery query, Object JavaDoc queryId){
93     prepQueries.put(queryId,query);
94     CAT.debug(" storePrepQuery: stored TKPrepQuery object of class " + query.getClass().getName() + " for reuse ");
95   }
96
97
98
99
100     public void beginTransaction() throws SQLException{
101         if ( transactionIsOpen || !autoCommit){
102             throw new TKSQLError( " Illegal nested transaction "); // change !!!
103
}
104         setAutoCommit(false);
105         transactionIsOpen = true;
106         CAT.debug(" Autocommit set to false ");
107     }
108
109
110
111
112     public void commitTransaction() throws SQLException
113     {
114         if(autoCommit || !transactionIsOpen) {
115             throw new TKSQLError( "Error in transaction-management: explicit commit out of tansaction environment "); // %%change
116
}
117         cleanupTransaction();
118         conn.commit();
119         setAutoCommit(true);
120     }
121
122
123
124     public void rollbackTransaction() throws SQLException
125     {
126         if(!transactionIsOpen) {
127             throw new TKSQLError( "Error in transaction-management: rollback outside of transaction "); // %%change
128
}
129
130         if( !autoCommit ) {
131             conn.rollback();
132             CAT.warn(" TRANSACTION ROLLED BACK ! ");
133             setAutoCommit(true);
134         }
135         else{
136             throw new TKSQLError( " Rollback called during AutoCommit "); // %%change
137
}
138         connectionManager.closeConnection();
139         cleanupTransaction();
140     }
141
142
143     /** Aufraeumen am Ende einer Transaktion:
144       * a) ‹berpr¸ft ob noch eine sensible Query offen ist, wirft dann Exceptio
145       * c) setzt transactionIsAktive auf false.
146       */

147
148     public void cleanupTransaction(){
149         if (sensitiveQueryOpen){
150             throw new TKSQLError( " A sensitive query is still open at the end of a transaction! "); // %%change
151
}
152         transactionIsOpen = false;
153     }
154
155
156
157     /** Meldet query als offene sensitive Query an */
158     public final void registerSensitiveQuery(){
159         if (sensitiveQueryOpen){
160             throw new TKSQLError( " Illegal attempt to execute sensitive queries interleaved! "); // %%change
161
}
162         if (transactionIsOpen == false){
163             throw new TKSQLError( " Illegal attempt to register sensitive query out of transaction environment! "); // %%change
164
}
165         sensitiveQueryOpen = true;
166     }
167
168
169     /** Meldet sensible Query ab */
170     public final void deregisterSensitiveQuery(){
171         if (sensitiveQueryOpen == false){
172             throw new TKSQLError( " Illegal attempt to deregister sensitive query "); // %%change
173
}
174         else{
175             sensitiveQueryOpen = false;
176         }
177     }
178
179
180     /** Registriert query im Vector nonSensitiveQueries, und gibt den Index zur¸ck*/
181     public final int registerNonSensitiveQuery( final TKQuery query ){
182         nonSensitiveQueries.addElement(query);
183         return (nonSensitiveQueries.size() -1);
184     }
185
186     public final TKQuery deregisterNonSensitiveQuery( final int i ){
187         TKQuery query = (TKQuery) nonSensitiveQueries.elementAt(i);
188         nonSensitiveQueries.setElementAt(null,i);
189         return query;
190     }
191
192     public final boolean isTransactionOpen(){
193         return transactionIsOpen;
194     }
195
196
197     /** Schliesst alle noch offenen Queries im Vector nonSensitiveQueries.
198         Die Version mit Parameter erlaubt es die Queries auf unterschiedliche
199         Weise zu schliessen. Das wird z.B. von _closeConnection benoetigt.
200         Wenn connectionClose == false ist, dann f¸hrt eine Exception beim
201         Schliessen einer Query nicht zum Verbindungsabbau. Ausserdem werden
202         dann garantiert alle Queries geschlossen.
203       */

204
205     public final void closeNonsensitiveQueries() throws SQLException{
206         closeNonsensitiveQueries(true);
207     }
208
209     public void closeNonsensitiveQueries( final boolean connectionClose )
210
211                                                             throws SQLException{
212
213     SQLException toThrow = null;
214
215     final int num_queries = nonSensitiveQueries.size();
216           int i;
217         for( i=0; i < num_queries; i++){
218
219             final TKQuery query = (TKQuery) nonSensitiveQueries.elementAt(i);
220             if ( query != null){
221                 if( connectionClose){
222                     query.close();
223                 }
224                 else{
225                     try{// Alle Queries werden geschlossen! Die letzte Exception wird weitergeworfen
226
query.specClose();
227                     }
228                     catch(SQLException toCatch){
229                         toThrow = toCatch;
230                     }
231                 }
232             }
233         }
234         nonSensitiveQueries.removeAllElements();
235         if (toThrow != null){
236             throw toThrow;
237         }
238     }
239
240
241
242 }
243
244
Popular Tags