我试图从MSSQL服务器数据库返回一个值,并将其存储在Jmeter中JSR223计时器脚本内的变量中。但是我在日志中收到以下错误-
** WARN o.a.j.t.JSR223计时器:脚本未返回值 **
这是我在脚本中编写的代码-
try {
def promoName = ${promotionName}; //extracted the value using JSONExtractor
log.info("Promotion Name is " + promoName); //not displaying in the log
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:sqlserver://ServerIP;databaseName="";integratedSecurity=true","<userId>","<password>");
String query ="select query";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
vars.put("promotionInstanceId", rs.getString(1)); //The query returns an instanceId which i need to store in a variable and use it later
log.info("Promotion Instance is " + ${promotionInstanceId});
}
conn.close();
}
catch (Exception e) {
e.printStackTrace();
有人可以帮助我了解我可能在哪里出错了吗?
答案 0 :(得分:1)
是否有使用计时器的理由?您收到的警告是有关丢失return keyword的警告,因为JSR223计时器旨在用于虚拟用户think time的计算,因此它应将值以毫秒为单位返回“ sleep”。鉴于您不想介绍思考时间,因此选择JSR223 PostProcessor是有意义的。
Don't inline JMeter functions or variables into the scripts,因为与GString template冲突可能会导致脚本行为不正常,否则该变量将被缓存,并且为后续调用返回相同的值。
建议使用JMeter的内置组件并在可能的情况下避免编写脚本,可以使用JDBC PostProcessor来提取有趣的值并将其存储到JMeter变量中。查阅Debugging JDBC Sampler Results in JMeter文章,了解如何执行SQL语句和处理结果
答案 1 :(得分:0)
您应该使用vars.get
来获取变量值
vars.put("promotionInstanceId", rs.getString(1)); //The query returns an instanceId which i need to store in a variable and use it later
log.info("Promotion Instance is " + vars.get("promotionInstanceId"));