1 18 19 package org.apache.jmeter.protocol.jdbc.util; 20 21 import java.sql.Connection ; 22 import java.sql.DriverManager ; 23 import java.sql.SQLException ; 24 25 import org.apache.jorphan.logging.LoggingManager; 26 import org.apache.log.Logger; 27 28 37 public class ConnectionObject implements Runnable 38 { 39 static long accessInterval = 180000; 40 41 private static Logger log = LoggingManager.getLoggerForClass(); 42 43 private final DBKey key; 44 private Connection con; 45 private int useCount; 46 private int maxUsage; 47 private long lastAccessed; 48 private volatile boolean inUse; 49 private volatile boolean inMaintenance; 50 51 private Thread reset; 52 53 59 public ConnectionObject(DBKey k, int maxUsage) throws SQLException 60 { 61 key = k; 62 reset = new Thread (this); 63 useCount = 0; 64 lastAccessed = System.currentTimeMillis(); 65 inMaintenance = true; 66 inUse = false; 67 con = null; 68 this.maxUsage = maxUsage; 69 reset(); 70 } 71 72 77 public boolean getInMaintenance() 78 { 79 return inMaintenance; 80 } 81 82 87 public void setInMaintenance(boolean b) 88 { 89 inMaintenance = b; 90 } 91 92 96 public long getLastAccessed() 97 { 98 return lastAccessed; 99 } 100 101 104 public void close() 105 { 106 if (con != null) 107 { 108 try 109 { 110 con.close(); 111 } 112 catch (SQLException e) 113 { 114 } 115 } 116 con = null; 117 } 118 119 122 public void update() 123 { 124 lastAccessed = System.currentTimeMillis(); 125 } 126 127 131 public boolean getInUse() 132 { 133 return inUse; 134 } 135 136 140 public synchronized Connection grab() 141 { 142 Connection c = null; 143 if (inUse || inMaintenance) 144 { 145 log.debug( 146 "Connection not available because it is " 147 + (inUse ? "in use" : "in maintenance")); 148 } 149 else 150 { 151 if (con != null) 152 { 153 try 154 { 155 if (con.isClosed()) 156 { 157 log.debug( 158 "Connection is closed. " 159 + "Putting it in maintenance."); 160 inMaintenance = true; 161 reset.start(); 163 } 164 else if ( 165 System.currentTimeMillis() - lastAccessed 166 > accessInterval) 167 { 168 log.debug( 169 "Connection is timed out. " 170 + "Putting it in maintenance."); 171 inMaintenance = true; 172 reset.start(); 174 } 175 else 176 { 177 log.debug( 178 "Connection is available." 179 + "Marking it as in use."); 180 inUse = true; 181 c = con; 182 } 183 } 184 catch (SQLException e) 185 { 186 log.warn("Exception checking if connection is closed", e); 187 } 188 } 189 else 190 { 191 log.debug("connection is null. Putting it in maintenance."); 192 inMaintenance = true; 193 reset.start(); 194 } 195 } 196 return c; 197 } 198 199 203 public int getUseCount() 204 { 205 return useCount; 206 } 207 208 211 public void run() 212 { 213 boolean set = true; 214 while (set) 215 { 216 try 217 { 218 reset(); 219 set = false; 220 } 221 catch (SQLException e) 222 { 223 log.error("ConnectionObject: url = " + key.getUrl(), e); 224 } 225 } 226 reset = new Thread (this); 227 } 228 229 233 public void reset() throws SQLException 234 { 235 if (con != null) 236 { 237 try 238 { 239 con.close(); 240 } 241 catch (Exception e) 242 { 243 } 244 con = null; 245 } 246 con = DriverManager.getConnection( 247 key.getUrl(), 248 key.getUsername(), 249 key.getPassword()); 250 251 useCount = 0; 252 lastAccessed = System.currentTimeMillis(); 253 inUse = false; 254 inMaintenance = false; 255 } 256 257 261 public void release() 262 { 263 useCount++; 264 try 265 { 266 if (con != null) 267 { 268 if (System.currentTimeMillis() - lastAccessed > accessInterval) 269 { 270 inMaintenance = true; 271 reset.start(); 272 } 273 else if (useCount >= maxUsage) 274 { 275 inMaintenance = true; 276 reset.start(); 277 } 278 else if (con.isClosed()) 279 { 280 inMaintenance = true; 281 reset.start(); 282 } 283 } 284 else 285 { 286 inMaintenance = true; 287 reset.start(); 288 } 289 } 290 catch (SQLException e) 291 { 292 inMaintenance = true; 293 reset.start(); 294 } 295 inUse = false; 296 lastAccessed = System.currentTimeMillis(); 297 } 298 299 303 public Connection getCon() 304 { 305 return con; 306 } 307 } 308 | Popular Tags |