1 17 18 package org.apache.james.util.mordred; 19 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.sql.*; 23 import java.util.Map ; 24 25 29 public class PoolConnEntry implements java.sql.Connection { 30 private static final boolean DEEP_DEBUG = false; 31 32 public final static int AVAILABLE = 0; 34 public final static int ACTIVE = 1; 35 36 private JdbcDataSource container; 37 38 private Connection connection; 39 private int status; 40 private long lockTime; 41 private long createDate; 42 private long lastActivity; 43 private int id; 44 private java.lang.Throwable trace; 45 46 51 public PoolConnEntry(JdbcDataSource container, Connection conn, int id) { 52 this.container = container; 53 this.connection = conn; 54 status = AVAILABLE; 55 createDate = System.currentTimeMillis(); 56 lastActivity = System.currentTimeMillis(); 57 this.id = id; 58 } 59 60 63 public synchronized boolean lock() throws SQLException { 64 if (DEEP_DEBUG) { 65 System.out.println("Trying to lock"); 66 } 67 68 if (status != PoolConnEntry.AVAILABLE) { 69 return false; 70 } 71 72 if (DEEP_DEBUG) { 73 System.out.println("Available"); 74 } 75 76 if (false) { 77 if (connection.isClosed()) { 81 throw new SQLException("Connection has been closed."); 82 } 83 84 if (DEEP_DEBUG) { 85 System.out.println("not closed"); 86 } 87 } 88 89 90 status = PoolConnEntry.ACTIVE; 91 lockTime = System.currentTimeMillis(); 92 lastActivity = lockTime; 93 trace = new Throwable (); 94 clearWarnings(); 95 if (DEEP_DEBUG) { 96 System.out.println("Returning"); 97 } 98 return true; 99 } 100 101 104 public synchronized void unlock() { 105 lastActivity = System.currentTimeMillis(); 106 trace = null; 107 status = AVAILABLE; 108 } 109 110 115 public void clearWarnings() { 116 try { 117 SQLWarning currSQLWarning = connection.getWarnings(); 118 while (currSQLWarning != null) { 119 StringBuffer logBuffer = 120 new StringBuffer (256) 121 .append("Warnings on connection ") 122 .append(id) 123 .append(currSQLWarning); 124 container.debug(logBuffer.toString()); 125 currSQLWarning = currSQLWarning.getNextWarning(); 126 } 127 connection.clearWarnings(); 128 } catch (SQLException sqle) { 129 container.debug("Error while clearing exceptions on " + id); 130 } 132 } 133 134 135 140 public long getCreateDate() { 141 return createDate; 142 } 143 144 149 public int getId() { 150 return id; 151 } 152 153 158 public long getLastActivity() { 159 return lastActivity; 160 } 161 162 167 public long getLockTime() { 168 return lockTime; 169 } 170 171 176 public int getStatus() { 177 return status; 178 } 179 180 185 public java.lang.Throwable getTrace() { 186 return trace; 187 } 188 189 192 protected void finalize() { 193 container.debug("Closing connection " + id); 194 try { 195 connection.close(); 196 } catch (SQLException ex) { 197 StringBuffer warnBuffer = 198 new StringBuffer (64) 199 .append("Cannot close connection ") 200 .append(id) 201 .append(" on finalize"); 202 container.warn(warnBuffer.toString()); 203 } 204 if (getTrace() != null) { 206 StringWriter sout = new StringWriter (); 207 trace.printStackTrace(new PrintWriter (sout, true)); 208 container.info(sout.toString()); 209 } 210 } 211 212 public String getString() { 213 StringBuffer poolConnStringBuffer = 214 new StringBuffer (64) 215 .append(getId()) 216 .append(": ") 217 .append(connection.toString()); 218 return poolConnStringBuffer.toString(); 219 } 220 221 222 230 231 public void close() throws SQLException { 232 clearWarnings(); 233 container.releaseConnection(this); 234 } 235 236 241 public boolean isClosed() throws SQLException { 242 return connection.isClosed(); 243 } 244 245 246 public final Statement createStatement() throws SQLException { 247 return connection.createStatement(); 248 } 249 250 public final PreparedStatement prepareStatement(final String sql) throws SQLException { 251 return connection.prepareStatement(sql); 252 } 253 254 public final CallableStatement prepareCall(final String sql) throws SQLException { 255 return connection.prepareCall(sql); 256 } 257 258 public final String nativeSQL(final String sql) throws SQLException { 259 return connection.nativeSQL( sql ); 260 } 261 262 public final void setAutoCommit(final boolean autoCommit) throws SQLException { 263 connection.setAutoCommit( autoCommit ); 264 } 265 266 public final boolean getAutoCommit() throws SQLException { 267 return connection.getAutoCommit(); 268 } 269 270 public final void commit() throws SQLException { 271 connection.commit(); 272 } 273 274 public final void rollback() throws SQLException { 275 connection.rollback(); 276 } 277 278 public final DatabaseMetaData getMetaData() throws SQLException { 279 return connection.getMetaData(); 280 } 281 282 public final void setReadOnly( final boolean readOnly ) throws SQLException { 283 connection.setReadOnly( readOnly ); 284 } 285 286 public final boolean isReadOnly() throws SQLException { 287 return connection.isReadOnly(); 288 } 289 290 public final void setCatalog( final String catalog ) throws SQLException { 291 connection.setCatalog( catalog ); 292 } 293 294 public final String getCatalog() throws SQLException { 295 return connection.getCatalog(); 296 } 297 298 public final void setTransactionIsolation( final int level ) throws SQLException { 299 connection.setTransactionIsolation(level); 300 } 301 302 public final int getTransactionIsolation() throws SQLException { 303 return connection.getTransactionIsolation(); 304 } 305 306 public final SQLWarning getWarnings() throws SQLException { 307 return connection.getWarnings(); 308 } 309 310 public final Statement createStatement( final int resultSetType, 311 final int resultSetConcurrency ) 312 throws SQLException { 313 return connection.createStatement(resultSetType, resultSetConcurrency); 314 } 315 316 public final PreparedStatement prepareStatement( final String sql, 317 final int resultSetType, 318 final int resultSetConcurrency ) 319 throws SQLException { 320 return connection.prepareStatement( sql, resultSetType, resultSetConcurrency); 321 } 322 323 public final CallableStatement prepareCall( final String sql, 324 final int resultSetType, 325 final int resultSetConcurrency ) 326 throws SQLException { 327 return connection.prepareCall( sql, resultSetType, resultSetConcurrency ); 328 } 329 330 public final Map getTypeMap() throws SQLException { 331 return connection.getTypeMap(); 332 } 333 334 public final void setTypeMap( final Map map ) throws SQLException { 335 connection.setTypeMap( map ); 336 } 337 338 422 423 } 424 | Popular Tags |