使用 maven 项目执行 jmeter 脚本时在 oracle 和 jtds 驱动程序上出错

Getting error on oracle and jtds driver while executing jmeter script using maven project

我有一个 jmx 脚本,它有 JDBC 连接到 oracle 和 sql 服务器的请求。我正在使用 maven 项目来执行 jmx 脚本,并且我已经在 pom.xml 中声明了对这两个服务器的依赖。但是我继续收到这两个驱动程序的错误。

maven 依赖项:

    <dependency>
        <groupId>net.sourceforge.jtds</groupId>
        <artifactId>jtds</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>12.2.0.1</version>
    </dependency>

Oracle 数据库错误:

[INFO] Cannot load JDBC driver class 'oracle.jdbc.OracleDriver'
[INFO] java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
[INFO]  at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
[INFO]  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
[INFO]  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
[INFO]  at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2197)
[INFO]  at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2104)
[INFO]  at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1563)
[INFO]  at org.apache.jmeter.protocol.jdbc.config.DataSourceElement$DataSourceComponentImpl.getConnection(DataSourceElement.java:334)
[INFO]  at org.apache.jmeter.protocol.jdbc.config.DataSourceElement.getConnection(DataSourceElement.java:193)
[INFO]  at org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler.sample(JDBCSampler.java:79)
[INFO]  at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622)
[INFO]  at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546)
[INFO]  at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486)
[INFO]  at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253)
[INFO]  at java.base/java.lang.Thread.run(Thread.java:834)

错误:(SQL 服务器)

[INFO] Cannot load JDBC driver class 'net.sourceforge.jtds.jdbc.Driver'
[INFO] java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
[INFO]  at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
[INFO]  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
[INFO]  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
[INFO]  at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2197)
[INFO]  at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2104)
[INFO]  at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1563)
[INFO]  at org.apache.jmeter.protocol.jdbc.config.DataSourceElement$DataSourceComponentImpl.getConnection(DataSourceElement.java:334)
[INFO]  at org.apache.jmeter.protocol.jdbc.config.DataSourceElement.getConnection(DataSourceElement.java:193)
[INFO]  at org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler.sample(JDBCSampler.java:79)
[INFO]  at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622)
[INFO]  at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546)
[INFO]  at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486)
[INFO]  at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253)
[INFO]  at java.base/java.lang.Thread.run(Thread.java:834)

这不是通过 JMeter Maven plugin 添加依赖项的正确方法,您需要在 <configuration> 部分下指定外部库,例如:

<configuration>
    <testPlanLibraries>
        <artifact>net.sourceforge.jtds:jtds:1.3.1</artifact>
        <artifact>com.oracle.jdbc:ojdbc8:12.2.0.1</artifact>
    </testPlanLibraries>
</configuration>

完整 pom.xml 以防万一:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>jmeter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>LATEST</version>

                <executions>
                    <!-- Run JMeter tests -->
                    <execution>
                        <id>jmeter-tests</id>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                    <!-- Fail build on errors in test -->
                    <execution>
                        <id>jmeter-check-results</id>
                        <goals>
                            <goal>results</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <testPlanLibraries>
                        <artifact>net.sourceforge.jtds:jtds:1.3.1</artifact>
                        <artifact>com.oracle.jdbc:ojdbc8:12.2.0.1</artifact>
                    </testPlanLibraries>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

更多信息: