Home Database Mysql Tutorial Hibernate性能优化

Hibernate性能优化

Jun 07, 2016 pm 02:50 PM
hibernate optimization Original performance

本文为博主原创,允许转载,但请声明原文地址:http://www.coselding.cn/blog/8/8-141.html (1)大量分页查询时,及时session.clear(),可能导致内存溢出; 大集合遍历:如寻找敏感词汇; 注:内存泄漏,Java本身不会内存泄漏,但是Java调用的底层C会内存泄

本文为博主原创,允许转载,但请声明原文地址:http://www.coselding.cn/blog/8/8-141.html

(1)大量分页查询时,及时session.clear(),可能导致内存溢出;
大集合遍历:如寻找敏感词汇;
 注:内存泄漏,Java本身不会内存泄漏,但是Java调用的底层C会内存泄漏。
(2)1+N问题:(典型面试题)
 一个实体A关联着另一个实体B,查询所有的实体A时,原本一条sql语句就可完成,结果后面却多了N条查询各个A关联的实体B的查询语句的问题,影响性能。
 解决办法:A. 实体B设置为LAZY;
           B.BatchSize多次单条查询合并成少次的多条查询,减少sql次数;
           C.join fetch:设置外连接,把后面要查询的也一起查出来,减少查询次数。
(3)BatchSize多次单条查询合并成少次的多条查询,减少sql次数。
(4)List的Iterator:
   A.List:Query查询到的List是直接一条sql查询所有结果;
   B.Iterator:只查询表的最小主键,接下来每访问一条才发送一条sql语句查询具体的记录,但是Iterator使用了Hibernate的缓存,同一个session查询两次,底层sql只发送一次而已,它会自己去缓存中查询结果是否已经有了,减少sql查询次数。
(5)一级缓存、二级缓存、查询缓存(面试题)
   A.一级缓存:session中的缓存,查询过一次之后的同一个session下再次查询会从缓存中查询;
   B.二级缓存:SessionFactory级别的缓存,可以跨越session,Hibernate没有实现,需要使用第三方二级缓存实现,默认EHCache,需要EHCache和Logging的jar;
     a.打开二级缓存:Hibernate主配置文件设置打开缓存并指定二级缓存实现类;
        @Cache注解:标注需要缓存到二级缓存的实体Entity;
        Ehcache.xml:EHCache二级缓存的配置文件,可设置多个缓存配置,根据
        name选择设置的属性配置;
     b.适用二级缓存:经常访问、改动不大、数量有限;
     c.load、iterator默认使用二级缓存,list有往二级缓存加数据但是查询时不使用;
     d.query若要使用二级缓存,需要打开查询缓存,
        true
        setCachable(true);指明Query打开二级缓存;
     e.缓存算法:LRU最近最少使用、FIFO(先进先出)、LFU(最近最不常使用)
   C.查询缓存:特殊的二级缓存,专门用于查询操作;
(6)二级缓存使用步骤:
   A.导入ehcache的jar和commons-logging的jar;
   B.类路径加入ehcache的配置文件,里面配置缓存配置;
   C.hibernate的全局配置文件配置开启二级缓存;
   D.标签设置要开启二级缓存的实体类;
   E.:usage设置缓存策略,region设置要使用的EHCache配置选项,include设置是否缓存该实体所关联的关联实体。
(7)事务并发处理(面试):
事务基础知识见JDBC笔记;
  A. 悲观锁:解决不可重复读,load添加第三个参数设置;
悲观地认为别人总想着影响自己,直接加锁,防止别人的影响;
  B. 乐观锁:解决多人同时修改,程序中实现;
乐观地认为别人不会来影响自己,加一个记录版本号的属性,@Version,事务开始时检查版本号,到真正要修改时再检查版本号,若版本号不变,则正确执行,版本号变了说明被影响了,报错;
(8)一定要设置连接池!!!
(9)一定注意数据库的全表查询是禁止的,关联关系的多方直接不延迟也是禁止的,Hibernate会把整个表加入内存和一级缓存,很容易导致内存溢出。

本文为博主原创,允许转载,但请声明原文地址:http://www.coselding.cn/blog/8/8-141.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

C++ program optimization: time complexity reduction techniques C++ program optimization: time complexity reduction techniques Jun 01, 2024 am 11:19 AM

Time complexity measures the execution time of an algorithm relative to the size of the input. Tips for reducing the time complexity of C++ programs include: choosing appropriate containers (such as vector, list) to optimize data storage and management. Utilize efficient algorithms such as quick sort to reduce computation time. Eliminate multiple operations to reduce double counting. Use conditional branches to avoid unnecessary calculations. Optimize linear search by using faster algorithms such as binary search.

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

How to use benchmarks to evaluate the performance of Java functions? How to use benchmarks to evaluate the performance of Java functions? Apr 19, 2024 pm 10:18 PM

A way to benchmark the performance of Java functions is to use the Java Microbenchmark Suite (JMH). Specific steps include: Adding JMH dependencies to the project. Create a new Java class and annotate it with @State to represent the benchmark method. Write the benchmark method in the class and annotate it with @Benchmark. Run the benchmark using the JMH command line tool.

What is the performance impact of converting PHP arrays to objects? What is the performance impact of converting PHP arrays to objects? Apr 30, 2024 am 08:39 AM

In PHP, the conversion of arrays to objects will have an impact on performance, mainly affected by factors such as array size, complexity, object class, etc. To optimize performance, consider using custom iterators, avoiding unnecessary conversions, batch converting arrays, and other techniques.

Performance comparison of C++ with other languages Performance comparison of C++ with other languages Jun 01, 2024 pm 10:04 PM

When developing high-performance applications, C++ outperforms other languages, especially in micro-benchmarks. In macro benchmarks, the convenience and optimization mechanisms of other languages ​​such as Java and C# may perform better. In practical cases, C++ performs well in image processing, numerical calculations and game development, and its direct control of memory management and hardware access brings obvious performance advantages.

How good is the performance of random number generators in Golang? How good is the performance of random number generators in Golang? Jun 01, 2024 pm 09:15 PM

The best way to generate random numbers in Go depends on the level of security required by your application. Low security: Use the math/rand package to generate pseudo-random numbers, suitable for most applications. High security: Use the crypto/rand package to generate cryptographically secure random bytes, suitable for applications that require stronger randomness.

See all articles