1 5 package com.teamkonzept.db; 6 7 import java.sql.*; 8 import java.util.Vector ; 9 10 import com.teamkonzept.lib.*; 11 12 import org.apache.log4j.Category; 13 14 20 public class TKDBConnection 21 { 22 23 private static final Category CAT = Category.getInstance(TKDBConnection.class); 24 25 26 Connection conn; 27 28 29 TKDBConnectionManager connectionManager; 30 31 32 public TKHashtable prepQueries = null; 34 35 private boolean sensitiveQueryOpen; 36 37 40 private boolean autoCommit = true; 41 42 43 private Vector nonSensitiveQueries = new Vector (); 44 45 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 "); } 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 "); } 84 return conn; 85 } 86 87 88 89 91 92 public void storePrepQuery(TKPrepQuery query, Object 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 "); } 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 "); } 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 "); } 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 "); } 138 connectionManager.closeConnection(); 139 cleanupTransaction(); 140 } 141 142 143 147 148 public void cleanupTransaction(){ 149 if (sensitiveQueryOpen){ 150 throw new TKSQLError( " A sensitive query is still open at the end of a transaction! "); } 152 transactionIsOpen = false; 153 } 154 155 156 157 158 public final void registerSensitiveQuery(){ 159 if (sensitiveQueryOpen){ 160 throw new TKSQLError( " Illegal attempt to execute sensitive queries interleaved! "); } 162 if (transactionIsOpen == false){ 163 throw new TKSQLError( " Illegal attempt to register sensitive query out of transaction environment! "); } 165 sensitiveQueryOpen = true; 166 } 167 168 169 170 public final void deregisterSensitiveQuery(){ 171 if (sensitiveQueryOpen == false){ 172 throw new TKSQLError( " Illegal attempt to deregister sensitive query "); } 174 else{ 175 sensitiveQueryOpen = false; 176 } 177 } 178 179 180 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 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{ 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 |