Aritro Sen
Aritro Sen

Reputation: 357

How to return a value from database in jmeter

I am trying to return a value from MSSQL server database and storing it in a variable inside JSR223 timer script in Jmeter. However I am getting the following error in log -

**WARN o.a.j.t.JSR223Timer: Script did not return a value **

This is the code which i have written in the script -

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();

Can anyone help me in understanding where i might have gone wrong ?

Upvotes: 0

Views: 924

Answers (2)

Dmitri T
Dmitri T

Reputation: 168002

  1. Any reason to use the timer? The warning you're getting is about missing return keyword as the JSR223 Timer is intended to be used for calculation of virtual user think time therefore it should return the value to "sleep" in milliseconds. Given you don't want to introduce the think time it makes sense to go for JSR223 PostProcessor instead.

  2. Don't inline JMeter functions or variables into the scripts as it might cause script misbehavior due to clashing with GString template or the variable will be cached and the same value will be returned for the subsequent calls.

  3. It is recommended to use JMeter's built-in components and avoid scripting where possible, you can use i.e. JDBC PostProcessor in order to extract the interesting value(s) and store it(them) into JMeter Variables. Check out Debugging JDBC Sampler Results in JMeter article to learn how to execute SQL statements and work with results

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58774

You should use vars.get to get variable value

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"));   

Upvotes: 0

Related Questions