[Java Web] Hibernate Basics Summary (iv)

Source: Internet
Author: User

Performance optimization

    1. When you traverse large data volumes (such as finding message-sensitive words), you need to manually use the Clear method to release the data in the cache to prevent excessive data wasting in the cache.
    2. 1+n problem: Set fetch to lazy to issue SQL statements when needed, or set batchsize to specify an SQL statement to query multiple objects.
    3. The list method takes all the objects out, and the iterate method takes the primary key of all the objects first, and then extracts the objects according to the primary key when needed. In addition iterate will query the level two cache, list will not.
Cache
    1. The cache in hibernate is divided into primary cache and level two cache, which can effectively reduce the number of database queries and improve program performance.
    2. The first-level cache refers to the cache in the session, because the session has a short life cycle, so the performance increase of the primary cache is limited.
    3. The second-level cache refers to the cache in Sessionfactory, which can be present across a session.
    4. Features that are suitable for level two cache data: often accessed, infrequently changed, and limited in number.
    5. How to open the cache: set in Hibernate.cfg.xml:
      <property name= "Cache.use_second_level_cache" >true</property><property name= "Cache.provider_class ">org.hibernate.cache.EhCacheProvider</property>
      Or use @cache annotations:
      @Cache (Usage=cacheconcurrencystrategy.read_write)
    6. The load and iterate methods use a level two cache by default, and list adds data to the level two cache, but does not look for data from the two-level cache.
    7. If you need to use a level two cache for query, you need to open the query cache:
      <property name= "Cache.use_query_cache" >true</property>
    8. Common caching algorithms are the LRU (least recently used), LFU (least frequently used), and FIFO (first-in-one-out).
Transaction concurrency Processing
    1. Possible problems with transactional concurrency are: The first category of missing updates, dirty reads, non-repeatable reads, the second category of missing updates (special cases where non-repeatable reads are not readable), and Phantom reads.
    2. Transaction isolation mechanism for the database: 1:read-uncommitted 2:read-committed 4:repeatable Read 8:serializable (the number represents the corresponding value).
    3. As long as a transactional database is supported, the first type of update loss cannot occur.
    4. Dirty reads, non-repeatable reads, and phantom reads occur read-uncommitted (allow reading of uncommitted data).
    5. Read-committed (Read committed data) does not appear dirty read, and will still appear non-repeatable read and Phantom read.
    6. Repeatable Read (no other transaction in transaction execution could be modified or inserted) is more secure.
    7. Serializable (all transaction order execution) is rarely used.
    8. Hibernate.connection.isolation can set the transaction isolation level, corresponding to 1, 2, 4, 8 (if not set to use the default level of the database).
    9. Pessimistic Lock: Use the Select ... for update, or use Load (class, ID, Lockmode.upgrade).
    10. Optimistic lock: The use of @version annotations increases the number of times a field is modified, and the transaction commits the same number of changes before committing.

[Java Web] Hibernate Basics Summary (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.