JMeter:每个线程执行后检查JDBC连接是否打开或关闭。

huangapple go评论56阅读模式
英文:

JMeter : Check JDBC connection open or close after each thread execution

问题

以下是翻译好的部分:

I am trying to check the JDBC connection status after each thread execution whether it is close or open?.
我正在尝试在每个线程执行后检查JDBC连接的状态,看它是否关闭或打开?

In my thread group there are three things
在我的线程组中有三个要素:

  1. JDBC connection configuration
  2. JDBC连接配置
  3. JDBC request (select * from employee)
  4. JDBC请求(从员工表中选择*)
  5. JSR223 PostProcessor
  6. JSR223后置处理器

Script :
脚本:

def connection = org.apache.jmeter.protocol.jdbc.config.DataSourceElement.getConnection('ConnectionString')
log.info('*************Connection closed: '+ connection.isClosed())
上述脚本在每个线程执行后记录连接状态,当循环计数为1时。问题在于,一旦我将循环计数更改为>= 2,它开始抛出以下错误:
上述脚本在每个线程执行后记录连接状态,当循环计数为1时。问题在于,一旦我将循环计数更改为>= 2,它开始抛出以下错误:

Problem in JSR223 script, JSR223 PostProcessor
JSR223脚本,JSR223后置处理器中的问题
javax.script.ScriptException: java.sql.SQLException: Cannot get a connection, pool error Timeout waiting for idle object
javax.script.ScriptException:java.sql.SQLException:无法获取连接,池错误,等待空闲对象的超时

And when I remove the Post processor and increase the loop count it is working fine.
而当我删除后置处理器并增加循环计数时,它运行正常。

Logs :
日志:

英文:

I am trying to check the JDBC connection status after each thread execution whether it is close or open?.

In my thread group there are three things

  1. JDBC connection configuration

  2. JDBC request (select * from employee)

  3. JSR223 PostProcessor

    Script :

    def connection = org.apache.jmeter.protocol.jdbc.config.DataSourceElement.getConnection('ConnectionString')
    log.info('*************Connection closed: '+ connection.isClosed())

Above script is logging the connection status after each thread execution when loop count is 1. Problem here as soon as I change the loop count to >= 2. it started throwing the error below error

Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: java.sql.SQLException: Cannot get a connection, pool error Timeout waiting for idle object

And when I remove the Post processor and increase the loop count it is working fine.

Logs :


2023-02-19 16:15:33,599 INFO o.a.j.t.JMeterThread: Thread started: DB Thread Group 2-1

2023-02-19 16:15:38,054 DEBUG o.a.j.p.j.AbstractJDBCTestElement: executing jdbc:SELECT * FROM EMPLOYEE

2023-02-19 16:15:38,623 INFO o.a.j.e.J.JSR223 PostProcessor: *************Connection closed: false

2023-02-19 16:15:58,637 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: java.sql.SQLException: Cannot get a connection, pool error Timeout waiting for idle object, borrowMaxWaitDuration=PT10S
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.11.jar:3.0.11]
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71) ~

答案1

得分: 2

我能想到两种可能的原因:

  1. 要么是因为您的数据库宕机/过载/无法通过JDBC访问。

  2. 要么是因为您的连接池设置需要调整,即需要增加最大连接数和/或等待时间:

    JMeter:每个线程执行后检查JDBC连接是否打开或关闭。

总的来说,我认为您的方法不正确,根据Java文档

通常无法通过调用此方法来确定与数据库的连接是有效还是无效。典型的客户端可以通过捕获在尝试执行操作时可能抛出的任何异常来确定连接是否无效。

因此,您可能希望增加JMeter、JDBC驱动程序和Java SQL命名空间的调试日志详细程度。

英文:

I can think of 2 possible reasons:

  1. Either your database is down/overloaded/not reachable via JDBC

  2. Or your connection pool settings need to be tweaked, i.e. max number of connections and/or wait time need to be increased:

    JMeter:每个线程执行后检查JDBC连接是否打开或关闭。

In general I don't think your approach is correct, as per JavaDoc:

>This method generally cannot be called to determine whether a connection to a database is valid or invalid. A typical client can determine that a connection is invalid by catching any exceptions that might be thrown when an operation is attempted.

So you might want to increase debug logging verbosity for JMeter, your JDBC driver and Java SQL namespace instead

答案2

得分: 1

在JDBC配置中,你是否在请求连接时使用连接池?

你的测试显示JSR223脚本正在关闭连接,从编码角度来看,这可能是一个好事,但循环的下一次迭代尝试使用关闭的连接执行请求,结果出错。如果你从原始连接切换到连接池,当JSR 223关闭连接时,它将被返回到池中,并保持打开状态以供下一次循环迭代使用。通常,你需要切换到使用DataSource API来实现这一点,但这只是脚本的一个小调整。

英文:

In the JDBC configuration are you using a connection pool to request connections?

What your test shows is that the JSR223 script is closing the connection, which is probably a good thing from a coding perspective, but the next iteration of the loop tries to execute a request with a closed Connection and blammo. If you switch from raw connections to a connection pool when the JSR 223 closes the connection it'll be returned to the pool and remain open for the next iteration of the loop. You'll have to switch to using DataSource API typically for this, but it's a minor tweak to the script.

huangapple
  • 本文由 发表于 2023年2月19日 19:02:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定