Method from org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler Detail: |
public static void close(Connection c) {
try {
if (c != null) {
c.close();
}
} catch (SQLException e) {
log.warn("Error closing Connection", e);
}
}
|
public static void close(Statement s) {
try {
if (s != null) {
s.close();
}
} catch (SQLException e) {
log.warn("Error closing Statement " + s.toString(), e);
}
}
|
public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
log.warn("Error closing ResultSet", e);
}
}
|
public String getDataSource() {
return dataSource;
}
|
public String getQuery() {
return query;
}
|
public String getQueryArguments() {
return queryArguments;
}
|
public String getQueryArgumentsTypes() {
return queryArgumentsTypes;
}
|
public String getQueryType() {
return queryType;
}
|
public String getVariableNames() {
return variableNames;
}
|
public SampleResult sample(Entry e) {
log.debug("sampling jdbc");
SampleResult res = new SampleResult();
res.setSampleLabel(getName());
res.setSamplerData(toString());
res.setDataType(SampleResult.TEXT);
res.setContentType("text/plain"); // $NON-NLS-1$
res.setDataEncoding(ENCODING);
// Assume we will be successful
res.setSuccessful(true);
res.setResponseMessageOK();
res.setResponseCodeOK();
res.sampleStart();
Connection conn = null;
Statement stmt = null;
try {
try {
conn = DataSourceElement.getConnection(getDataSource());
} finally {
res.latencyEnd(); // use latency to measure connection time
}
res.setResponseHeaders(conn.toString());
// Based on query return value, get results
String _queryType = getQueryType();
if (SELECT.equals(_queryType)) {
stmt = conn.createStatement();
ResultSet rs = null;
try {
rs = stmt.executeQuery(getQuery());
res.setResponseData(getStringFromResultSet(rs).getBytes(ENCODING));
} finally {
close(rs);
}
} else if (CALLABLE.equals(_queryType)) {
CallableStatement cstmt = getCallableStatement(conn);
int out[]=setArguments(cstmt);
// A CallableStatement can return more than 1 ResultSets
// plus a number of update counts.
boolean hasResultSet = cstmt.execute();
String sb = resultSetsToString(cstmt,hasResultSet, out);
res.setResponseData(sb.getBytes(ENCODING));
} else if (UPDATE.equals(_queryType)) {
stmt = conn.createStatement();
stmt.executeUpdate(getQuery());
int updateCount = stmt.getUpdateCount();
String results = updateCount + " updates";
res.setResponseData(results.getBytes(ENCODING));
} else if (PREPARED_SELECT.equals(_queryType)) {
PreparedStatement pstmt = getPreparedStatement(conn);
setArguments(pstmt);
pstmt.executeQuery();
String sb = resultSetsToString(pstmt,true,null);
res.setResponseData(sb.getBytes(ENCODING));
} else if (PREPARED_UPDATE.equals(_queryType)) {
PreparedStatement pstmt = getPreparedStatement(conn);
setArguments(pstmt);
pstmt.executeUpdate();
String sb = resultSetsToString(pstmt,false,null);
res.setResponseData(sb.getBytes(ENCODING));
} else if (ROLLBACK.equals(_queryType)){
conn.rollback();
res.setResponseData(ROLLBACK.getBytes(ENCODING));
} else if (COMMIT.equals(_queryType)){
conn.commit();
res.setResponseData(COMMIT.getBytes(ENCODING));
} else if (AUTOCOMMIT_FALSE.equals(_queryType)){
conn.setAutoCommit(false);
res.setResponseData(AUTOCOMMIT_FALSE.getBytes(ENCODING));
} else if (AUTOCOMMIT_TRUE.equals(_queryType)){
conn.setAutoCommit(true);
res.setResponseData(AUTOCOMMIT_TRUE.getBytes(ENCODING));
} else { // User provided incorrect query type
String results="Unexpected query type: "+_queryType;
res.setResponseMessage(results);
res.setSuccessful(false);
}
} catch (SQLException ex) {
final String errCode = Integer.toString(ex.getErrorCode());
res.setResponseMessage(ex.toString());
res.setResponseCode(ex.getSQLState()+ " " +errCode);
res.setSuccessful(false);
} catch (UnsupportedEncodingException ex) {
res.setResponseMessage(ex.toString());
res.setResponseCode("000"); // TODO - is this correct?
res.setSuccessful(false);
} catch (IOException ex) {
res.setResponseMessage(ex.toString());
res.setResponseCode("000"); // TODO - is this correct?
res.setSuccessful(false);
} finally {
close(stmt);
close(conn);
}
// TODO: process warnings? Set Code and Message to success?
res.sampleEnd();
return res;
}
|
public void setDataSource(String dataSource) {
this.dataSource = dataSource;
}
|
public void setQuery(String query) {
this.query = query;
}
|
public void setQueryArguments(String queryArguments) {
this.queryArguments = queryArguments;
}
|
public void setQueryArgumentsTypes(String queryArgumentsType) {
this.queryArgumentsTypes = queryArgumentsType;
}
|
public void setQueryType(String queryType) {
this.queryType = queryType;
}
|
public void setVariableNames(String variableNames) {
this.variableNames = variableNames;
}
|
public String toString() {
StrBuilder sb = new StrBuilder(80);
sb.append("["); // $NON-NLS-1$
sb.append(getQueryType());
sb.append("] "); // $NON-NLS-1$
sb.append(getQuery());
sb.append("\n");
sb.append(getQueryArguments());
sb.append("\n");
sb.append(getQueryArgumentsTypes());
return sb.toString();
}
|