mrog
mrog

Reputation: 2114

Using a JMeter JDBC connection in Java code

I'm working on a Java Request class for use in JMeter. The class needs to have access to the same database that's being used by the rest of the test plan. Is there a way to access the test plan's JDBC Connection from Java code?

Upvotes: 2

Views: 2857

Answers (1)

pmpm
pmpm

Reputation: 705

Suppose you have the following JDBC Connection Configuration configuration:

enter image description here

You can access connection this way using a JSR223 Sampler + Groovy+Cache Key (whatever unique String you want):

enter image description here

If you want to copy paste, Sampler contains this:

import java.sql.Connection;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
Connection conn = DataSourceElement.getConnection("jdbcConfig");
// Ensure you close conn

Upvotes: 5

Related Questions