skip to content

Database Requests

Driving a database directly through a pooled connection and a query sampler, and the caveat that this measures the database and its driver, never the application in front of them.

on this pageshow

explore

questions

6

In Apache JMeter, what must exist before a JDBC Request sampler can run a query?

level: juniorimportance: must knowfreq 71%

answer

  1. Two elements, never one
  2. A config element holds the connection
  3. Both fields must carry one name
  4. Vendor jar sits beside JMeter's utility jars

basics

~20 s

A JDBC Connection Configuration element whose Variable Name for created pool matches the sampler's pool field, plus the vendor's driver jar in JMeter's lib directory. A name mismatch fails the sample with "No pool found named".

solid answer

~50 s

JMeter splits a database request across two elements. The **JDBC Connection Configuration** config element holds `Database URL`, `JDBC Driver class`, `Username`, `Password` and the pool settings, and binds the pool it builds to the name in **Variable Name for created pool**. The **JDBC Request** sampler holds only the SQL, and points at that pool through its own *Variable Name of Pool declared in JDBC Connection Configuration* field; the two strings must be identical, and each configuration element in a plan needs a distinct name. JMeter 6.0.0 ships the Apache Commons DBCP pool but no vendor drivers, so the database's own JDBC jar has to be copied into `lib/` (not `lib/ext/`, which is for JMeter components and plugins). Setting the `CLASSPATH` environment variable does not help: `bin/jmeter` launches the JVM with `java -jar`, and Java ignores `CLASSPATH` and `-cp` in that mode.

code

text · 14 lines
text
apache-jmeter-6.0.0/
  lib/
    postgresql-42.7.4.jar        <- vendor JDBC driver goes here
  lib/ext/
    (JMeter components and plugins only - not drivers)

JDBC Connection Configuration
  Variable Name for created pool : reportingDb
  Database URL                   : jdbc:postgresql://db.internal:5432/reporting
  JDBC Driver class              : org.postgresql.Driver

JDBC Request
  Variable Name of Pool ...      : reportingDb
  SQL Query                      : select region, sum(amount) from orders group by region

go deeper

for a junior

Recall that a database request needs a JDBC Connection Configuration plus a JDBC Request, joined by one identical pool name, and that the vendor driver jar goes in the lib directory.

for a middle

Explain what each element owns: connection and pool settings on the config element, SQL and result handling on the sampler. Know that JMeter reads its classpath once at startup, so a newly copied driver needs a restart.

for a senior

Show how you diagnose the two common failures apart - a missing pool name versus a missing driver class - and how you keep credentials out of the .jmx, given that the Password field is stored unencrypted in the plan.

for a principal

Own the convention: one named pool per database per plan, driver jars provisioned with the injector image rather than hand-copied, and a rule for where connection details come from so plans stay portable between environments.

A database request in Apache JMeter 6.0.0 is never one element. The connection lives in a config element and the SQL lives in a sampler, and both have to be in place before a single row moves. ## The two elements **JDBC Connection Configuration** is a Configuration Element. It carries every connection detail — `Database URL`, `JDBC Driver class`, `Username`, `Password`, `Connection Properties` — plus the pool tuning fields (`Max Number of Connections`, `Max Wait (ms)`, `Time Between Eviction Runs (ms)`, `Auto Commit`, `Transaction Isolation`, `Pool Prepared Statements`, `Preinit Pool`, `Init SQL statements separated by new line`, `Test While Idle`, `Soft Min Evictable Idle Time(ms)`, `Validation Query`). At test start it stores a pool handle under the name typed into **Variable Name for created pool**; the Apache Commons DBCP `BasicDataSource` behind it is built there and then when `Max Number of Connections` is positive, and lazily, per thread, at the shipped `0`. **JDBC Request** is a Sampler. It carries `Query Type`, `SQL Query`, `Parameter values`, `Parameter types`, `Variable Names`, `Result Variable Name`, `Query timeout (s)`, `Limit ResultSet` and `Handle ResultSet` — and one pool reference, labelled *Variable Name of Pool declared in JDBC Connection Configuration*. It has no URL, no driver and no credentials of its own. The join between them is the pool name, matched as plain text. Several samplers may name the same pool; one plan may hold several configuration elements, but each needs its own unique name — if two share one, JMeter logs `JDBC data source already defined for: <name>` and only one of them binds. ## Where the driver jar goes JMeter finds jars in two directories automatically: - `lib/` — utility jars, which is where JDBC drivers belong; - `lib/ext/` — JMeter's own components and plugins, which is *not* where a driver belongs. If you would rather not copy the jar into the installation, the `user.classpath` and `plugin_dependency_paths` properties in `jmeter.properties` add further search paths, and the Test Plan element itself has a panel that appends jars or directories to the JMeter loader's search path. Two traps sit here. JMeter only picks up `.jar` files, never `.zip`. And exporting `CLASSPATH` does nothing at all, because `bin/jmeter` starts the JVM with `java -jar`, and the `java` command silently ignores both `CLASSPATH` and `-cp` when `-jar` is used. ## What the failures look like | Symptom | Cause | |---|---| | Sample fails, response data `No pool found named: 'reportingDb', ensure Variable Name matches...` | The sampler's pool field and the configuration's Variable Name differ | | Sample fails with a `ClassNotFoundException` for the driver class | The vendor jar is missing from `lib/` (or is a `.zip`, or was added only to `CLASSPATH`) | | Only one of two configuration elements works | Both were given the same Variable Name; JMeter logged `JDBC data source already defined for:` | | Sample fails carrying an SQLState and a vendor error code | The connection was obtained but the database rejected the statement or the credentials | The last row is worth internalising: when the driver throws, the JDBC Request sampler puts the driver's SQLState plus its vendor error code into the sample's response code and the exception text into the response message, so a JTL row from a failed database sample carries the database's own diagnosis rather than an HTTP-style code. ## A first-run checklist 1. Copy the vendor driver jar into `lib/` and restart JMeter — the classpath is read at startup. 2. Add a JDBC Connection Configuration and give **Variable Name for created pool** a name you will not reuse. 3. Fill `Database URL` and `JDBC Driver class`; JMeter offers a preconfigured list of driver class names, populated from the `jdbc.config.jdbc.driver.class` property. 4. Add a JDBC Request under the same thread group and paste the *same* pool name into its pool field. 5. Type the SQL with **no trailing semi-colon** — the manual is explicit about that. 6. Run one thread once and read the response data before you scale anything up. One ordering detail catches people later: the configuration element resolves its fields once, at test start, rather than per iteration. Whether it also *builds* a pool then depends on `Max Number of Connections` — a positive value builds the shared pool at test start; the shipped `0` stores a placeholder and each thread's own single-connection pool is built lazily, on that thread's first JDBC Request. And the config element's `Password` field is stored unencrypted in the `.jmx`, which the manual says outright — worth knowing before a plan with real credentials lands in a repository.

  • Where does JMeter look for jars, and why does exporting CLASSPATH not help?
    JMeter loads jars from `lib/` (utility jars, including JDBC drivers) and `lib/ext/` (JMeter components and plugins), plus anything named by the `user.classpath` or `plugin_dependency_paths` properties. `bin/jmeter` starts the JVM with `java -jar`, and the java command silently ignores `CLASSPATH` and `-cp` when `-jar` is used. Only `.jar` files are picked up, never `.zip`.
  • What happens if two JDBC Connection Configuration elements are given the same Variable Name?
    Only one of them binds. At test start JMeter logs `JDBC data source already defined for: <name>` at error level and the second element's pool is never stored, so every sampler naming that pool silently uses whichever configuration got there first. The manual states plainly that each name must be different.

saying these in an interview costs you the question

  • Claims a JDBC Request sampler can hold the database URL itself
  • Puts the vendor driver jar in lib/ext with the plugins
  • Expects the CLASSPATH environment variable to be honoured
  • Assumes JMeter bundles MySQL or PostgreSQL drivers
  • Gives two connection configurations the same pool name
open as a page

In JMeter's JDBC Connection Configuration, what does Max Number of Connections set to 0 mean?

level: middleimportance: must knowfreq 63%

basics

~20 s

Zero turns off sharing. Each JMeter thread lazily builds its own DBCP pool holding a single connection, so no thread ever waits on another. Any positive number builds one pool of that size shared by every thread.

open as a page

In JMeter's JDBC Request, what variables does a Variable Names value of A,,C create?

level: middleimportance: should knowfreq 49%

basics

~20 s

One numbered variable per row for each named column, plus a row count. For a two-row, three-column select: A_1 and A_2 from column one, C_1 and C_2 from column three, and A_# and C_# both holding 2. The empty entry skips column two.

open as a page

A JMeter JDBC Request runs a reporting query returning 400,000 rows. What does the sampler build?

level: seniorimportance: should knowfreq 44%

basics

~20 s

It walks the whole result set and concatenates every row into one tab-separated string as the sample's response data, whether or not Variable Names is filled. Limit ResultSet is the field that caps how many rows it reads.

open as a page

A JMeter plan drives a reporting query straight at the database. How would you choose Max Number of Connections?

level: principalimportance: should knowfreq 41%

basics

~20 s

Decide first what the pool is meant to represent. Leave it at 0 and the thread count alone sets the connections the database sees; pin a positive number and JMeter's own pool becomes the constraint, with waits and, past Max Wait, failed samples.

open as a page

In JMeter's JDBC Request, what do the Commit, Rollback and AutoCommit(false) query types do?

level: seniorimportance: nice to knowfreq 31%

basics

~10 s

They ignore the SQL Query field completely and only change the connection's state. You place them as separate JDBC Request samplers around other samplers to wrap several statements in one transaction.

open as a page