Search code examples
javajdbcjmeterbeanshell

Dynamically change JDBC config in Jmeter


I need to make sure,whether table has data or not ,based on the environment,say for example i have two two database,one is development another one is production like given below.

Production 
host1
dbuser1
dbpassword1
dburl
Tablename : studentinfo

Development
dbuser2
dbpassword2
dburl2
Tablename : studentinfo

FYI : studentinfo has same structure in both environment .

In Jmeter ,User Defined Variable( UDV), I have configured two set of database information. Using BeanShell Processor I have trying to change the database connectivity information, Is there any way to change the Db Config dynamically ?

Given below is my Jmeter UDV

env : prod
prod_db_url: dburl
prod_db_user:usr
prod_db_password:password

dev_db_url: dburl
dev_db_user:usr
dev_db_password:password

In My Beanshell Preprocessor

String env=vars.get("env");
if(env.equlas("prod")){
   // Load the Prod db into vars
} else if (env.equals("dev")){
   // Load the Dev db into vars
}

Here ,I am setting the values in vars, and trying to get the information from DB Configuration variables. but i am not able get values the in DB config.

Can anyone explain ? what went wrong or what is the approach to get connection?


Solution

  • You do not need a Beanshell PreProcessor for this.

    For the below UDV,

    env : prod
    prod_db_url: dburl
    prod_db_user:usr
    prod_db_password:password
    
    dev_db_url: dburl
    dev_db_user:usr
    dev_db_password:password
    

    Just by changing the value of env, you can access all the variables values by using

    ${__V(${env}_db_url)} // return prod or dev db url depends on the value of env.


    Another nice solution:

    Can you have the same variables and store them in 2 different property files?

    prod.proeprties

    db_url=dburl
    db_user=usr
    db_password=password
    

    dev.properties

    db_url=dburl
    db_user=usr
    db_password=password
    

    You can use the JMeter - Property File Reader.

    Property reader file path would be /path/to/${env}.properties Access all the variables using ${__P(db_url)}, ${__P(db_user)}