本文转自:http://stackoverflow.com/questions/20866023/jmeter-var-putobject-variable-use-in-other-jdbc-request-but-it-is-ignored

jmeter var.putobject variable use in other jdbc request, but it is ignored

Test Plan Thread Group JDBC Connection JSR223 Sampler JDBC Request Result Tree

Put codes in the JSR223 Sampler:

int id = Integer.parseInt( vars.get("party_id") ); vars.putObject("id", new Integer(id));

Then use id in JDBC Request:

    select * from table where column = ? 
    Parameter Value: ${id}
    parameter Type: NUMBER
        

It seems the JDBC request is ignored and no output shown in the result tree.

Please help to look into the issue...

Thanks in advance

share improve this question edited Jan 1 '14 at 6:07

User Defined Variable stores value of String but not Integer. I wonder if there is any error/warning in JMeter console (log).

Cast you integer to String to put it into UDV. enter image description here

Just specify ${UDV} if you wanted it to be an integer. Add quotes ('$UDV') if you want it as varchar in SQL query enter image description here

I also add debug sampler into the test plan, variable "id" has the value shown in the result tree. Observe the issue from the log file "jmeter.threads.JMeterThread: Error while processing sampler 'try integer' : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String". Do you have any idea how to get the integer variable used in the other sampler? –   Jan 2 '14 at 5:23 When you use putObject method just cast your integer into String using toString() method.vars.putObject("id", id.toString()); –   Jan 2 '14 at 6:58 If i use string it works fine for my select statement where i can change the type to varchar. I have one insert statement for one jdbc request required number parameter. When i pass "${id}" not working if cast it to String.–   Jan 2 '14 at 7:51 I'm not sure I've got your option. If you have select query you write it as select * from table where condition = ${id}. Supposing your variable has value 5 (it is string, because user defined variable stores only string) next query will be executed select * from table where condition = 5. If you need varchar in the SQL query you write select * from table where condition = '${id}' and next will be executed select * from table where condition = '5'. JMeter HTTP or JDBC sampler treat ${variable} the next way: just get the text from variable and put it into sampler body. Is it the answer? –   Jan 2 '14 at 8:34 Thanks for the details. Method vars.putObject can store any type of object for user defined variable, for example integer. I wonder if there is some way to get the variable whose type is not string. I have one insert query [insert into table (id number) values (${id})] required integer in one jdbc request using the variable storing integer defined in JSR223 sampler. As you have mentioned, ${id} type is string, any way to convert to integer? –   Jan 2 '14 at 10:30 

Have reproduced your problem, it looks like that JDBC Sampler doesn't accept integers, only Strings.

I'm getting following error in jmeter.log, guess you too:

2014/01/04 11:30:14 ERROR - jmeter.threads.JMeterThread: Error while processing sampler 'JDBC Request' : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

So your cast to integer is absolutely not required. Try changing your JSR223 code to following:

vars.put("id", vars.get("party_id")); 
        

It should work fine.

The bit, which tells MySQL which datatype bind the variable to is ParameterType stanza. You need to specify paratemer type there, not in JSR223

share improve this answer answered  Jan 4 '14 at 8:39 Jmeter 的 JSR223 控件是 代替 BeanShell 的新一代脚本控件,支持多种脚本语言,尤其是其的 Groovy,更是重点推荐使用的脚本语言,本文研究其vars 和 props 两种对象的用法。 二者的区别 vars props 本文使用Jmeter 是 4.0 版本 二者的区别 props 和 vars 主要有两点区别: vars 只能在当前线程内使用,pr.. 在使用Jmeter过程,或多或少都会接触些BeanShell,它会使工具的使用,变得更灵活。Jmeter关于BeanShell的有:1.BeanShell Sampler 取样器:完成Beanshell请求组件定义的脚本功能;2.BeanShell PostProcessor 后置处理器:可通过编程方式获取对应请求的响应结果;3.BeanShell PreProcessor 前置处理器:可通过编程方式在发起请求以前做预处理,比如编码或者加密; 文章目录1、Beanshell2、常用内置变量3、log4、varsvars.get()vars.put()vars.remove()5、props6、ctx7、SampleResult JMeter 可以使用 Beanshell Sampler 编写一些简单的脚本,实现更多功能。 1、Beanshell Beanshell 是一种轻量级的 Java 脚本,纯 Java 编写的,能够动态的执行标... BeanShell是用Java写成的,一个小型的、免费的、可以下载的、嵌入式的Java源代码解释器,具有对象脚本语言特性。本篇只记录一下基本的使用。有以下五个组件: Beanshell Sampler Beanshell PreProcessor Beanshell PostProcessor Beanshell Assertion __Beanshe... beanshell创建新的参数newvarname,赋值为另外一个字符串 vars.put(“newvarname”,String.valueOf(definedvarname)); 引用参数值,任何一个地方均可以 ${newvarname} 在上一篇,我们了解了在beanshell可以利用vars获取和设置新的变量,也知道了如何用log去查看日志,以及两种获取变量值的方法。没看过的可以点这https://mp.csdn.net/mp_blog/creation/editor/120850385 本篇主要讲下两种获取变量值方法间的差异。同样设置一个配置元素,名为name,值为junjun 按如下代码执行,可见打印结果都是junjun。 log.info("vars.get获取的name值:"+var... 变量参数化,如多个数值时一般都会想到使用“CSV Data Set Config”来实现,但实际场景只需要有限的指定数值来进行参数化,调用文件来维护文件也是比较麻烦的。所以在此使用另一种方法,直接在脚本定义使用。 逻辑就是先创建一个JSR223 Samper初始化参数,然后使用Loop Controller进行控制循环 ,这里是实例没有实际的请求,如有实际的请求时,把Loop Controller 下的JSR223 Samper改为请求的前置条件**“JSR223 PreProcesso beanshell是jmeter内嵌的一个java解释器,大概175k,比较小,你可以在这个元件运行自己的脚本,前置处理器,定时器,取样器,断言,监听器都有beanshell元件,但是都大同小异,先来看beanshell前置处理器 reset interpreter:是否每次都重启解释器,可以不用动,但是大并发量的情况下官方建议改为true parameter:其的数据会被自动... JSR223 Samplerjsr223的采样器允许使用jsr223脚本代码来运行一个取样器。 jsr 223测试元素有一个可以显著提高性能的特性(编译)。从这个特性获益: 使用脚本文件而不是内联它们。这将使JMeter编译它们,如果这个特性在ScriptEngine可用,并缓存它们。 若编译属性设置为true,可以使用脚本文本和检查缓存编译的脚本。 在使用这个特性时,确保脚本代码没有直接在