使用 JDBC 缓慢重复执行 DDL 和 DML 语句
对于针对数据库抽象库的 11 个不同 RBDMS 运行的集成测试,我需要经常重置架构和/或数据。这意味着,我为大多数测试用例运行了大量语句。
仅在测试套件开始时才需要重置架构。例如:
DROP VIEW
DROP TABLE
DROP SEQUENCE
DROP PROCEDURE
...
CREATE TABLE
...
只有写入数据的测试才需要数据重置。例如:
DROP SEQUENCE
DELETE FROM TABLE ...
CREATE SEQUENCE
INSERT INTO TABLE ...
只读测试非常快,因为它们不需要任何重置。直观上,我只是将 SQL 文件读入内存并使用它执行每个语句:
// No bind variables, all hard parses
// Bind variables won't help much, though as the "hard-parse-reduction" isn't very big.
stmt = connection.createStatement();
stmt.execute(sql);
在某些系统上,这非常慢。有没有更好的方法用 JDBC 来做到这一点?或者我必须接受这样一个事实:这可能很慢......?
For integration tests running against 11 different RBDMS for a database abstraction library, I need to frequently reset the schema and/or data. This means, I run plenty of statements for most test-cases.
Schema resets are only needed at the beginning of a test-suite. For example:
DROP VIEW
DROP TABLE
DROP SEQUENCE
DROP PROCEDURE
...
CREATE TABLE
...
Data-resets are only needed for tests that write data. For example:
DROP SEQUENCE
DELETE FROM TABLE ...
CREATE SEQUENCE
INSERT INTO TABLE ...
Read-only tests are quite fast, as they don't need any resetting. Inutitively, I'd just read a SQL file into memory and execute every statement using this:
// No bind variables, all hard parses
// Bind variables won't help much, though as the "hard-parse-reduction" isn't very big.
stmt = connection.createStatement();
stmt.execute(sql);
On some systems, this is quite slow. Is there any better way to do this with JDBC? Or do I have to accept the fact that this can be slow...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
部分想法:为了更快地删除,许多 DBMS 都有“截断表”语句。
另一个部分想法:您可以只复制空数据库的文件,而不是在测试开始时删除并重新安装所有内容。不过,这需要深入了解所有 11 个 DBMS,并且您需要关闭进程。
Partial idea: for faster deletes, many DBMS have a "truncate table" statement.
Another partial idea: instead of deleting and reinstalling everything at the start of the test, you could just copy the files of an empty database. This will require in-depth knowledge of all 11 DBMS though, and you'll need to shutdown the processes.