1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.sql.SQLException ; 12 import java.sql.Statement ; 13 import java.sql.Connection ; 14 import java.util.*; 15 import java.text.DateFormat ; 16 import java.text.SimpleDateFormat ; 17 18 27 abstract class AbstractProxyStatement { 28 29 private static final Log LOG = LogFactory.getLog(ProxyStatement.class); 30 31 private static final DateFormat DATE_FORMAT = new SimpleDateFormat ("dd-MMM-yyyy.HH:mm:ss"); 32 33 private Statement statement; 34 35 private ConnectionPool connectionPool; 36 37 private ProxyConnectionIF proxyConnection; 38 39 private Map parameters; 40 41 private String sqlStatement; 42 43 private StringBuffer sqlLog = new StringBuffer (); 44 45 52 public AbstractProxyStatement(Statement statement, ConnectionPool connectionPool, ProxyConnectionIF proxyConnection, String sqlStatement) { 53 this.statement = statement; 54 this.connectionPool = connectionPool; 55 this.proxyConnection = proxyConnection; 56 this.sqlStatement = sqlStatement; 57 } 58 59 64 protected boolean testException(Throwable t) { 65 if (FatalSqlExceptionHelper.testException(connectionPool.getDefinition(), t)) { 66 try { 69 statement.close(); 70 connectionPool.throwConnection(proxyConnection, "Fatal SQL Exception has been detected"); 71 72 HouseKeeperController.sweepNow(connectionPool.getDefinition().getAlias()); 74 75 LOG.warn("Connection has been thrown away because fatal exception was detected", t); 76 } catch (SQLException e2) { 77 LOG.error("Problem trying to throw away suspect connection", e2); 78 } 79 return true; 80 } else { 81 return false; 82 } 83 } 84 85 89 public Statement getDelegateStatement() { 90 return statement; 91 } 92 93 97 protected ConnectionPool getConnectionPool() { 98 return connectionPool; 99 } 100 101 105 protected Statement getStatement() { 106 return statement; 107 } 108 109 114 public void close() throws SQLException { 115 statement.close(); 116 proxyConnection.registerClosedStatement(statement); 117 } 118 119 protected Connection getConnection() { 120 return ProxyFactory.getWrappedConnection((ProxyConnection) proxyConnection); 121 } 122 123 127 public boolean equals(Object obj) { 128 return (statement.hashCode() == obj.hashCode()); 129 } 130 131 136 protected void putParameter(int index, Object value) { 137 138 if (parameters == null) { 140 parameters = new TreeMap(new Comparator() { 141 public int compare(Object o1, Object o2) { 142 int c = 0; 143 144 if (o1 instanceof Integer && o2 instanceof Integer ) { 145 c = ((Integer ) o1).compareTo(((Integer ) o2)); 146 } 147 148 return c; 149 } 150 }); 151 } 152 153 Object key = new Integer (index); 154 if (value == null) { 155 parameters.put(key, "NULL"); 156 } else if (value instanceof String ) { 157 parameters.put(key, "'" + value + "'"); 158 } else if (value instanceof Number ) { 159 parameters.put(key, value); 160 } else if (value instanceof Boolean ) { 161 parameters.put(key, ((Boolean ) value).toString()); 162 } else if (value instanceof Date) { 163 parameters.put(key, "'" + getDateAsString((Date) value) + "'"); 164 } else { 165 String className = value.getClass().getName(); 166 StringTokenizer st = new StringTokenizer(className, "."); 167 while (st.hasMoreTokens()) { 168 className = st.nextToken(); 169 } 170 parameters.put(key, className); 171 } 172 } 173 174 180 protected void trace(long startTime, Exception exception) throws SQLException { 181 182 if (isTrace()) { 183 if (connectionPool.getLog().isDebugEnabled() && connectionPool.getDefinition().isTrace()) { 185 connectionPool.getLog().debug(sqlLog.toString() + " (" + (System.currentTimeMillis() - startTime) + " milliseconds" 186 + (exception != null ? ", threw a " + exception.getClass().getName() + ": " + exception.getMessage() + ")" : ")")); 187 } 188 connectionPool.onExecute(sqlLog.toString(), (System.currentTimeMillis() - startTime), exception); 190 } 191 192 if (parameters != null) { 194 parameters.clear(); 195 } 196 sqlStatement = null; 197 sqlLog.setLength(0); 198 199 } 200 201 protected void startExecute() { 202 if (isTrace()) { 203 ((ProxyConnection) proxyConnection).addSqlCall(sqlLog.toString()); 204 } 205 } 206 207 212 protected void appendToSqlLog() { 213 if (sqlStatement != null && sqlStatement.length() > 0 && isTrace()) { 214 int parameterIndex = 0; 215 StringTokenizer st = new StringTokenizer(sqlStatement, "?"); 216 while (st.hasMoreTokens()) { 217 if (parameterIndex > 0) { 218 if (parameters != null) { 219 final Object value = parameters.get(new Integer (parameterIndex)); 220 if (value != null) { 221 sqlLog.append(value); 222 } else { 223 sqlLog.append("?"); 224 } 225 } else { 226 sqlLog.append("?"); 227 } 228 } 229 parameterIndex++; 230 sqlLog.append(st.nextToken()); 231 } 232 if (sqlStatement.endsWith("?")) { 233 if (parameterIndex > 0) { 234 if (parameters != null) { 235 final Object value = parameters.get(new Integer (parameterIndex)); 236 if (value != null) { 237 sqlLog.append(value); 238 } else { 239 sqlLog.append("?"); 240 } 241 } else { 242 sqlLog.append("?"); 243 } 244 } 245 } 246 if (sqlStatement != null && !sqlStatement.trim().endsWith(";")) { 247 sqlLog.append("; "); 248 } 249 } 250 if (parameters != null) { 251 parameters.clear(); 252 } 253 } 254 255 protected boolean isTrace() { 256 return getConnectionPool().isConnectionListenedTo() || (getConnectionPool().getDefinition().isTrace()); 257 } 258 259 263 protected void setSqlStatementIfNull(String sqlStatement) { 264 if (this.sqlStatement == null) { 265 this.sqlStatement = sqlStatement; 266 } 267 } 268 269 protected static String getDateAsString(Date date) { 270 return DATE_FORMAT.format(date); 271 } 272 273 } 274 275 276 | Popular Tags |