1 10 package org.mmbase.module.database; 11 12 import java.sql.*; 13 import java.util.Map ; 14 import java.lang.reflect.Method ; 15 16 import org.mmbase.module.core.MMBase; 17 import org.mmbase.util.logging.Logger; 18 import org.mmbase.util.logging.Logging; 19 20 36 public class MultiConnection extends ConnectionWrapper { 37 public final static int CON_UNUSED = 0; 39 public final static int CON_BUSY = 1; 40 public final static int CON_FINISHED = 2; 41 public final static int CON_FAILED = 3; 42 43 44 public static long queries = 0; 45 private static final Logger log = Logging.getLoggerInstance(MultiConnection.class); 46 47 50 MultiPool parent; 51 54 String lastSql; 55 56 private long startTimeMillis = 0; 57 private int usage = 0; 58 public int state = 0; 59 60 65 public MultiConnection(MultiPool parent,Connection con) { 66 super(con); 67 this.parent = parent; 68 state = CON_UNUSED; 69 } 70 71 74 public String getStateString() { 75 if (state == CON_FINISHED) { 76 return "Finished"; 77 } else if (state==CON_BUSY) { 78 return "Busy"; 79 } else if (state==CON_FAILED) { 80 return "Failed"; 81 } else if (state==CON_UNUSED) { 82 return "Unused"; 83 } 84 return "Unknown"; 85 } 86 87 90 public void setLastSQL(String sql) { 91 lastSql = sql; 92 state = CON_BUSY; 93 } 94 95 98 public String getLastSQL() { 99 return lastSql; 100 } 101 102 105 public Statement createStatement() throws SQLException { 106 MultiStatement s = new MultiStatement(this, con.createStatement()); 107 return s; 108 } 109 110 120 121 protected boolean checkAfterException() throws SQLException { 122 Statement s = null; 123 ResultSet rs = null; 124 try { 125 s = createStatement(); 127 rs = s.executeQuery("SELECT 1 FROM " + MMBase.getMMBase().getBuilder("object").getFullTableName() + " WHERE 1 = 0"); } catch (SQLException isqe) { 129 log.service("Found broken connection, will try to fix it."); 131 parent.replaceConnection(this); 133 return true; 134 } finally { 135 if (s != null) s.close(); 136 if (rs != null) rs.close(); 137 } 138 return false; 139 } 140 141 151 public void setAutoCommit(boolean enableAutoCommit) throws SQLException { 152 try { 153 con.setAutoCommit(enableAutoCommit); 154 } catch (SQLException sqe) { 155 if (checkAfterException()) { 156 con.setAutoCommit(enableAutoCommit); 157 } else { 158 throw sqe; 159 } 160 } 161 } 162 163 166 private String getLogSqlMessage(long time) { 167 StringBuffer mes = new StringBuffer (); 168 mes.append('#'); 169 mes.append(queries); 170 mes.append(" "); 171 if (time < 10) mes.append(' '); 172 if (time < 100) mes.append(' '); 173 if (time < 1000) mes.append(' '); 174 mes.append(time); 175 mes.append(" ms: ").append(getLastSQL()); 176 return mes.toString(); 177 } 178 181 public void close() throws SQLException { 182 long time = System.currentTimeMillis() - getStartTimeMillis(); 183 long maxLifeTime = parent.getMaxLifeTime(); 184 if (time < maxLifeTime / 24) { log.debug(getLogSqlMessage(time)); 186 } else if (time < maxLifeTime / 4) { if (log.isServiceEnabled()) { 188 log.service(getLogSqlMessage(time)); 189 } 190 } else if (time < maxLifeTime / 2) { log.info(getLogSqlMessage(time)); 192 } else { log.warn(getLogSqlMessage(time)); 194 } 195 if (log.isDebugEnabled()) { 196 log.trace("because", new Exception ()); 197 } 198 199 state = CON_FINISHED; 200 if (parent != null) { 205 parent.putBack(this); 206 } else { 207 realclose(); 208 } 209 } 210 211 214 public void realclose() throws SQLException { 215 con.close(); 216 } 217 218 221 public boolean checkSQLError(Exception e) { 222 log.error("JDBC CHECK ERROR=" + e.toString()); 223 return true; 224 } 225 226 229 public void claim() { 230 usage++; 231 queries++; 232 startTimeMillis = System.currentTimeMillis(); 233 } 234 235 238 public void release() { 239 startTimeMillis = 0; 240 } 241 242 245 public int getUsage() { 246 return usage; 247 } 248 249 252 void resetUsage() { 253 usage = 0; 254 } 255 256 259 public int getStartTime() { 260 return (int) (startTimeMillis / 1000); 261 } 262 263 266 public long getStartTimeMillis() { 267 return startTimeMillis; 268 } 269 270 271 274 public String toString() { 275 return "'"+getLastSQL()+"'@"+hashCode(); 276 } 277 280 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 281 return new MultiStatement(this, con.createStatement(resultSetType, resultSetConcurrency)); 282 } 283 284 285 289 public Statement createStatement(int type, int concurrency, int holdability) throws SQLException { 290 return new MultiStatement(this, con.createStatement(type, concurrency, holdability)); 291 } 292 293 299 public Connection getRealConnection() { 300 return con; 301 } 302 } 303 304 305 | Popular Tags |