I was ultimately unable to make it work with JDBC Connection Configuration; however, I found a workaround by using JSR223 Sampler instead, and I think this is actually a better method since it doesn't involve requesting and using access token; all it needs is client ID and client secret.
Within JSR223 Sampler, I chose "Language: java (BeanShell)" and manually write Java code inside, based on this article, section ActiveDirectoryServicePrincipal: https://learn.microsoft.com/en-us/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver16.
My Test Plan now has the following structure:
- Test Plan
- Thread Group
- User Defined Variables
- JSR223 Sampler
- View Results Tree
Note that I needed to add some additional dependencies that MSAL4J relies on (it wasn't obvious from the errors I got so feel like I have to mention it here). I got the following .jar files from Maven repository:
- msal4j-1.13.3.jar
- mssql-jdbc-10.2.0.jre8.jar
- mssql-jdbc_auth-12.2.0x64.dll (this one is a .dll not a .jar file)
- content-type-2.2jar
- oauth2-oidc-sdk-10.13.2.jar
- nimbus-jose-jwt-9.31.jar
The Java code inside my JSR223 Sampler ended up being as follows, where vars.get()
accesses the corresponding variable defined in User Defined Variables:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
public class AADServicePrincipal {
public void executeQuery() {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName(vars.get("server_name")); // Replace with your server name
ds.setDatabaseName(vars.get("database_name")); // Replace with your database
ds.setAuthentication("ActiveDirectoryServicePrincipal");
ds.setUser(vars.get("client_id")); // Replace with your user name
ds.setPassword(vars.get("client_secret")); // Replace with your password
try {
Connection connection = ds.getConnection();
Statement stmt = connection.createStatement();
String query = "SELECT COUNT(*) FROM table1; ";
ResultSet rs = stmt.executeQuery(query);
System.out.println("Performed the query successfully!");
printResult(rs);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception: " + e);
}
}
private void printResult(ResultSet rs) {
ResultSetMetaData rsmd = rs.getMetaData();
int columnsCount = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnsCount; i++) {
if (i > 1) {
System.out.println(", ");
}
String columnValue = rs.getString(i);
System.out.println(columnValue + " " + rsmd.getColumnName(i));
}
System.out.println();
}
}
}
AADServicePrincipal session = new AADServicePrincipal();
session.executeQuery();