H2 Java插入ignore - 允许异常 [英] H2 Java Insert ignore - allow exception
问题描述
我正在考虑的是只需运行插入并让重复键异常发生。但是,我不希望这样填写我的日志文件。 DB调用发生在导入的类中,我无法更改。所以我的问题是:
- 这是一个合理的事情吗?我不是为了让错误发生,但这似乎是最好的方式在这种情况下(不应该发生这么多)。
- 我如何保持这个例外,击中我的日志文件?如果没有办法阻止堆栈中的异常,可以重定向输出的堆栈跟踪的输出吗?
谢谢。
一个解决方案是使用:
插入测试
从双
中选择1,'Hello'不存在(select * from test where id = 1)
这应该适用于所有数据库(除了双重部分;您可能需要创建一个自己的虚拟表与一行)。
要禁用日志记录例外,请将; trace_level_file = 0
附加到数据库URL:
jdbc:h2:〜/ test; trace_level_file = 0
或运行SQL语句:
set trace_level_file 0
I am working on a java plugin interfacing with an H2 database. What I really want is an "Insert Ignore" statement; however, I'm aware that H2 doesn't support this. I am also aware of Merge, but this is really not what I want, if the record exists I don't want to change it.
What I am considering is to just run the insert and let the duplicate key exception happen. However, I don't want this to fill my log file. The DB call happens in an imported class that I can't change. So my questions are:
- Is this a reasonable thing to do? I'm not one for letting errors happen, but this seems like the best way in this case (it should not happen all that much).
- How can I keep this exception from hitting my log file? If there isn't a way to block exceptions down the stack, can I redirect the output of the stack trace that is output?
Thanks.
One solution is to use:
insert into test
select 1, 'Hello' from dual
where not exists(select * from test where id = 1)
This should work for all databases (except for the dual part; you may need to create your own dummy table with one row).
To disable logging exceptions, append ;trace_level_file=0
to the database URL:
jdbc:h2:~/test;trace_level_file=0
or run the SQL statement:
set trace_level_file 0
这篇关于H2 Java插入ignore - 允许异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!