1 18 19 package org.apache.jmeter.protocol.jdbc.sampler; 20 21 import java.io.IOException ; 22 import java.io.NotActiveException ; 23 import java.io.ObjectInputStream ; 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.SQLException ; 28 import java.sql.Statement ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import org.apache.jmeter.config.ConfigTestElement; 33 import org.apache.jmeter.engine.event.LoopIterationEvent; 34 import org.apache.jmeter.protocol.jdbc.util.ConnectionPoolException; 35 import org.apache.jmeter.protocol.jdbc.util.DBConnectionManager; 36 import org.apache.jmeter.protocol.jdbc.util.DBKey; 37 import org.apache.jmeter.samplers.AbstractSampler; 38 import org.apache.jmeter.samplers.Entry; 39 import org.apache.jmeter.samplers.SampleResult; 40 import org.apache.jmeter.testelement.TestListener; 41 import org.apache.jmeter.testelement.property.JMeterProperty; 42 import org.apache.jmeter.testelement.property.PropertyIterator; 43 import org.apache.jorphan.collections.Data; 44 import org.apache.jorphan.logging.LoggingManager; 45 import org.apache.log.Logger; 46 47 54 public class JDBCSampler extends AbstractSampler implements TestListener 55 { 56 private static Logger log = LoggingManager.getLoggerForClass(); 57 58 public static final String URL = "JDBCSampler.url"; 59 public static final String DRIVER = "JDBCSampler.driver"; 60 public static final String QUERY = "JDBCSampler.query"; 61 62 public static final String JDBCSAMPLER_PROPERTY_PREFIX = "JDBCSampler."; 63 public static final String CONNECTION_POOL_IMPL = 64 JDBCSAMPLER_PROPERTY_PREFIX + "connPoolClass"; 65 66 67 68 private transient DBConnectionManager manager = 69 DBConnectionManager.getManager(); 70 private transient DBKey dbkey = null; 71 72 75 public JDBCSampler() 76 { 77 } 78 79 private void readObject(ObjectInputStream is) 81 throws NotActiveException , IOException , ClassNotFoundException 82 { 83 is.defaultReadObject(); 84 manager = DBConnectionManager.getManager(); 85 } 86 87 public SampleResult sample(Entry e) 88 { 89 DBKey key = null; 90 91 SampleResult res = new SampleResult(); 92 res.setSampleLabel(getName()); 93 res.setSamplerData(this.toString()); 94 95 96 res.sampleStart(); 97 Connection conn = null; 98 Statement stmt = null; 99 100 try 101 { 102 key = getKey(); 103 104 conn = manager.getConnection(key); 107 stmt = conn.createStatement(); 108 109 if (stmt.execute(getQuery())) 111 { 112 ResultSet rs = null; 113 try 114 { 115 rs = stmt.getResultSet(); 116 Data data = getDataFromResultSet(rs); 117 res.setResponseData(data.toString().getBytes()); 118 } 119 finally 120 { 121 if (rs != null) 122 { 123 try 124 { 125 rs.close(); 126 } 127 catch (SQLException exc) 128 { 129 log.warn("Error closing ResultSet", exc); 130 } 131 } 132 } 133 } 134 else 135 { 136 int updateCount = stmt.getUpdateCount(); 137 String results = updateCount + " updates"; 138 res.setResponseData(results.getBytes()); 139 } 140 141 res.setDataType(SampleResult.TEXT); 142 res.setSuccessful(true); 143 } 144 catch (Exception ex) 145 { 146 log.error("Error in JDBC sampling", ex); 147 res.setResponseMessage(ex.toString()); 148 res.setResponseData(new byte[0]); 149 res.setSuccessful(false); 150 } 151 finally 152 { 153 if (stmt != null) 154 { 155 try 156 { 157 stmt.close(); 158 } 159 catch (SQLException err) 160 { 161 stmt = null; 162 } 163 } 164 165 if (conn != null) 166 { 167 manager.releaseConnection(key, conn); 168 } 169 } 170 171 res.sampleEnd(); 172 return res; 173 } 174 175 private synchronized DBKey getKey() throws ConnectionPoolException 176 { 177 if (dbkey == null) 178 { 179 dbkey = 186 manager.getKey( 187 getUrl(), 188 getUsername(), 189 getPassword(), 190 getDriver(), 191 getJDBCProperties()); 192 } 193 194 return dbkey; 195 } 196 197 private Map getJDBCProperties() 198 { 199 Map props = new HashMap (); 200 201 PropertyIterator iter = propertyIterator(); 202 while (iter.hasNext()) 203 { 204 JMeterProperty prop = iter.next(); 205 if (prop.getName().startsWith(JDBCSAMPLER_PROPERTY_PREFIX)) 206 { 207 props.put(prop.getName(), prop); 208 } 209 } 210 return props; 211 } 212 213 220 private Data getDataFromResultSet(ResultSet rs) throws SQLException 221 { 222 ResultSetMetaData meta = rs.getMetaData(); 223 Data data = new Data (); 224 225 int numColumns = meta.getColumnCount(); 226 String [] dbCols = new String [numColumns]; 227 for (int i = 0; i < numColumns; i++) 228 { 229 dbCols[i] = meta.getColumnName(i + 1); 230 data.addHeader(dbCols[i]); 231 } 232 233 while (rs.next()) 234 { 235 data.next(); 236 for (int i = 0; i < numColumns; i++) 237 { 238 Object o = rs.getObject(i + 1); 239 if (o instanceof byte[]) 240 { 241 o = new String ((byte[]) o); 242 } 243 data.addColumnValue(dbCols[i], o); 244 } 245 } 246 return data; 247 } 248 249 250 public String getDriver() 251 { 252 return getPropertyAsString(DRIVER); 253 } 254 255 public String getUrl() 256 { 257 return getPropertyAsString(URL); 258 } 259 260 public String getUsername() 261 { 262 return getPropertyAsString(ConfigTestElement.USERNAME); 263 } 264 265 public String getPassword() 266 { 267 return getPropertyAsString(ConfigTestElement.PASSWORD); 268 } 269 270 public String getQuery() 271 { 272 return this.getPropertyAsString(QUERY); 273 } 274 275 276 public String toString() 277 { 278 return getUrl() + ", user: " + getUsername() + "\n" + getQuery(); 279 } 280 281 282 public void testStarted(String host) 283 { 284 testStarted(); 285 } 286 287 public synchronized void testStarted() 288 { 289 293 } 306 307 public void testEnded(String host) 308 { 309 testEnded(); 310 } 311 312 public synchronized void testEnded() 313 { 314 log.debug("testEndded(), thread: "+Thread.currentThread().getName()); 315 manager.shutdown(); 316 dbkey = null; 317 } 318 319 public void testIterationStart(LoopIterationEvent event) 320 { 321 } 322 } 323 | Popular Tags |