java Hibernate 和 JDBC 性能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17150085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:09:28  来源:igfitidea点击:

Hibernate and JDBC performance?

javahibernatejdbc

提问by JCS

Currently i am switching my java web application from JDBC to Hibernate, in my current implementation with JDBC i load up static data on initialization of the application into static variables so i don't have to directly access the database every time i need some static data, now switching to hibernate i am thinking of getting rid of these static variables as as far as i have researched hibernate keeps loaded data in a cache.

目前我正在将我的 java web 应用程序从 JDBC 切换到 Hibernate,在我当前使用 JDBC 的实现中,我将应用程序初始化时的静态数据加载到静态变量中,这样我就不必每次需要一些静态数据时直接访问数据库,现在切换到休眠我正在考虑摆脱这些静态变量,因为我已经研究过休眠将加载的数据保存在缓存中。

I am fairly new to hibernate so i am not sure if switching from my current method to hibernate will give any performance improvements. I am going to research further into hibernates caching and run some performance tests to see which method is better but would just like some opinions on what others think regarding performance on both these methods.

我对 hibernate 还很陌生,所以我不确定从我当前的方法切换到 hibernate 是否会带来任何性能改进。我将进一步研究休眠缓存并运行一些性能测试以查看哪种方法更好,但只是想了解其他人对这两种方法的性能的看法。

Thanks.

谢谢。

回答by AurA

JDBC will always give better performanceas compared to Hibernate for most of the database vendors. You can check the comparison made as given in the link below. He concludes that Hibernate is fast when querying tables with less rows else JDBC is way better:
http://phpdao.com/hibernate_vs_jdbc/

对于大多数数据库供应商而言,与 Hibernate 相比,JDBC 将始终提供更好的性能。您可以查看下面链接中给出的比较。他的结论是,当查询行数较少的表时,Hibernate 速度很快,否则 JDBC 会更好:http:
//phpdao.com/hibernate_vs_jdbc/

Another good performance stats can be found in Hibernate forum discussion at https://forum.hibernate.org/viewtopic.php?f=1&t=931708

另一个不错的性能统计数据可以在 Hibernate 论坛讨论中找到,网址https://forum.hibernate.org/viewtopic.php?f=1&t=931708

It states the following order of performance hit taken due to use of Hibernate (please note that this can be improved by tuning Hibernate to one's needs:

它说明了以下由于使用 Hibernate 而造成的性能损失顺序(请注意,这可以通过根据个人需要调整 Hibernate 来改进:

Objects: 8 - Hibernate: 10ms / Direct JDBC: 10ms = Ratio: 1.0 
Objects: 16 - Hibernate: 10ms / Direct JDBC: 0ms = Ratio: Infinity 
Objects: 64 - Hibernate: 20ms / Direct JDBC: 10ms = Ratio: 2.0 
Objects: 256 - Hibernate: 150ms / Direct JDBC: 30ms = Ratio: 5.0 
Objects: 512 - Hibernate: 210ms / Direct JDBC: 40ms = Ratio: 5.25 
Objects: 1024 - Hibernate: 410ms / Direct JDBC: 70ms = Ratio: 5.857143 
Objects: 2048 - Hibernate: 681ms / Direct JDBC: 180ms = Ratio: 3.7833333
Objects: 8 - Hibernate: 10ms / Direct JDBC: 10ms = Ratio: 1.0 
Objects: 16 - Hibernate: 10ms / Direct JDBC: 0ms = Ratio: Infinity 
Objects: 64 - Hibernate: 20ms / Direct JDBC: 10ms = Ratio: 2.0 
Objects: 256 - Hibernate: 150ms / Direct JDBC: 30ms = Ratio: 5.0 
Objects: 512 - Hibernate: 210ms / Direct JDBC: 40ms = Ratio: 5.25 
Objects: 1024 - Hibernate: 410ms / Direct JDBC: 70ms = Ratio: 5.857143 
Objects: 2048 - Hibernate: 681ms / Direct JDBC: 180ms = Ratio: 3.7833333


The choice of Hibernate over JDBC and SQL queries is not because of the performance, but because of reasons mainly object persistenceand database independencein terms of not writing database specific queries. You can read the following PDF guide to get a better view:


选择 Hibernate 而不是 JDBC 和 SQL 查询不是因为性能,而是因为在不编写数据库特定查询方面主要是对象持久性数据库独立性的原因。您可以阅读以下 PDF 指南以获得更好的视图:

回答by Olaf

For your particular situation I would recommend creating your own object that would keep needed static value(s). You can populate it during initialization of your application or when that data is requested for the first time.

对于您的特定情况,我建议您创建自己的对象来保留所需的静态值。您可以在应用程序初始化期间或第一次请求该数据时填充它。

As for the comparative perfromance, your best case is that your Hibernate-based data access layer would perfrom as good as JDBC, or the perfromance degradation would not affect overall user expereince. Hibernate is used to speed up development and enhance maintainability, not to get a perfromance edge over JDBC.

至于比较性能,最好的情况是基于 Hibernate 的数据访问层的性能与 JDBC 一样好,否则性能下降不会影响整体用户体验。Hibernate 用于加速开发和增强可维护性,而不是为了获得优于 JDBC 的性能优势。