JDBC瘦驱动程序:无效的数据包长度[原文如此] [英] JDBC Thin Driver: Invalid Packet Lenght [sic]
问题描述
我在某些Java代码上运行自动批量测试时遇到了一个奇怪的无效数据包长度(这就是拼写错误的方式)错误,我希望有人在遇到此错误之前或者可以指出我在正确的方向。
I have encountered a strange "Invalid Packet Lenght" (that is how the error is spelled) error when I run an automated bulk test on some of my Java code and I hope that someone has either encountered this error before or can point me in the right direction.
通过JUnit单元测试或从GUI测试代码时,我没有遇到此错误。我在自动批量测试中只遇到此错误。关于我的批量测试的一点点:对于某些输入,我的代码将运行很长时间(这是预期的),但是为了将结果加速到更合理的时间范围,我正在创建一个新的线程来运行每个个别测试,以便我可以在一些给定的最大经过时间后停止测试。
I do not encounter this error when testing my code via JUnit unit tests or from the GUI. I only encounter this error on my automated bulk test. A little bit about my bulk test: for some inputs my code will run for a long time (that's to be expected), but in order to speed up the results to a more reasonable time frame I'm creating a new thread to run each individual test so that I can stop the test after some given maximum elapsed time.
注意,测试和实际代码都需要连接到同一个数据库实例才能加载数据。实际代码使用单个连接从数据库中读取(它不是多线程的)。我仍然试图找出测试连接到数据库的最佳方法(因此这个问题)。
Note that both the test and the actual code need to connect to the same database instance to load data. The actual code uses a single connection to read from the database (it is not multi-threaded). I'm still trying to figure out the best way for the test to connect to the database (hence this question).
我的第一个想法是我做了一些不友好的事情在我关闭我的测试线程以尽早退出运行的方式。我正在调用已弃用的
My first thought was that I am doing something unfriendly in the way I close my test thread to quit the run early. I'm calling the deprecated
threadObject.stop();
方法,因为我的实际代码不是多线程的,没有友好方式来杀死线程内置。在几个(~2-3)停止线程后,我的JDBC连接抛出一个Invalid Packet Lenght错误,然后是其他测试的Socket closed异常。
method since my actual code is not multi-threaded an there is no "friendly" way to kill the thread built in. After a few (~2-3) stopped threads, my JDBC connection throws one "Invalid Packet Lenght" error followed by "Socket closed" exceptions for the rest of the tests.
我尝试了所有这些结果相同的结果:
I've tried all of these with the same results:
- 重复使用与
相同的连接实际代码使用 - 创建一个新连接,重用所有
测试的相同第二个连接 - 每次关闭并重新创建测试连接我停止()一个
长时间运行的测试 - 为每个测试创建一个新连接(这一直有效,直到我最大化我的连接数)
- Reuse the same connection that the actual code uses
- Create one new connection an reuse that same second connection for all tests
- Close and recreate the test connection every time I stop() a long-running test
- Create a new connection for each test (this works until I max out my connection count)
我已经确定了两个连接,test和actual,test连接是抛出异常的连接。
I have determined that of the two connections, "test" and "actual", the "test" connection is the one that throws the exception.
配置:
- Eclipse 3.4
- Java Compliance 1.6
- ojdbc14_g.jar JDBC驱动程序
- Oracle 9 DB
- Eclipse 3.4
- Java Compliance 1.6
- ojdbc14_g.jar JDBC Driver
- Oracle 9 DB
我做错了什么?我应该采用不同的方式来处理测试连接吗?我是否需要重新构建我的实际连接才能运行批量测试?导致无效数据包长度错误的原因是什么?
What am I doing wrong? Is there a different way I should handle my "test" connection? Do I need to re-architect my "actual" connection just to run a bulk test? What causes the "Invalid Packet Lenght" error?
推荐答案
我真的不明白你想要用什么来实现测试,所以我会提供一些关于线程的一般建议和我知道的 Connection
:
I don't really understand what are you trying to accomplish with your bulk test, so I will offer some general advice regarding threading and Connection
s that I know of:
-
也许您的一个线程正在使连接处于无效状态。线程停止时连接会发生什么?一般来说,你应该有一个
wait()
的地方,一个InterruptedException
而不是在中期停止线程-operation。无论是否发生异常,都应在finally
块中关闭连接
。
Perhaps one of your threads is leaving a connection in invalid state. What happens to connection when thread is stopped? In general terms, you should have a place where you
wait()
for anInterruptedException
instead of stopping the thread in mid-operation. AConnection
should be closed in afinally
block, regardless of whether an exception occurred or not.
如果您希望JDBC操作超时,请使用 Statement.setQueryTimeout()。
If you want a timeout for a JDBC operation, use Statement.setQueryTimeout().
考虑使用连接池,例如 C3P0 。在代码中设置非常简单,请查看这个特别是。它将负责大部分连接设置/拆卸,并为您的代码提供有效的,随时可用的连接。
Consider using a connection pool, like C3P0. It's really easy to set up in code, have a look at this in particular. It will take care of most of the connection set-up/tear-down and provide your code with valid, ready-to-use connection.
考虑使用Java自己的 java.util。并发包,而不是推出自己的执行策略。看看 Executors 和 ExecutorService 课程 - 它们提供了在单独的线程中执行任务的方法,并设置了超时。
Consider using Java's own java.util.concurrent package, rather than rolling out your own execution strategy. Have a look at Executors and ExecutorService classes - they provide means of executing a task in separate thread, and setting up a timeout.
希望其中一些有用。
这篇关于JDBC瘦驱动程序:无效的数据包长度[原文如此]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!