Hibernate learning six Hibernate cache

Source: Internet
Author: User
Tags sessions

Cache :

If hibernate is used in a clustered environment (the cluster has node A, Node B) when requested, a node is sent to a, a record is modified in the database, and then the cache of Node B updates the new data modified by node A in real time.
Hibernate's own first-level cache does not span processes, requires third-party cache support, and Ehcache caches can be implemented

Hibernate cache:
  First-level caching:
In the same session, the first time the Get () method is called, Hibernate retrieves the cache for the Lookup object, finds no, Hibernate sends a SELECT statement to the database to fetch the appropriate object, and then put the object in the cache for the next use, The second time the Get () method is called, Hibernate retrieves the cache for the lookup object, discovers that the lookup object is there, removes it from the cache, does not retrieve it from the database, and does not send the SELECT statement again.
One session cannot take the cache in another session.
level Two cache:
Sessionfactory-level caches can be present across sessions and can be shared by multiple sessions of the session.
(1) Frequently visited

(2) Minor changes

(3) Limited quantity

(4) data that is not very important, allowing occasional concurrent data to occur.
User's rights: The number of users is small, the permissions are not many, not often changed, often be accessed. such as organizational structures.

How does Hibernate find objects apply caching?
When hibernate accesses data objects based on their IDs, the first cache is checked from the session level.

Not found, if a level two cache is configured, then from the level two cache;

If it is not found, then query the database, the results according to the ID into the cache, delete, update, add data, while updating the cache.

The QBC (Query by Criteria) API provides another way to retrieve objects:


hibernate to take some fields?
QBC is the upper-hql package that queries all fields. To use only part of the field, write hql as you would write SQL.
One scenario is to add a constructor to the entity class, and the constructor has only the required fields, so it is not fully checked (String hql= "select New Employee (E.ID,E.NAME_CN) from employee E"; )
The other is to write directly to the SQL and get things assembled into objects. Query query = getsession (). CreateQuery (HQL). Setresulttransformer ((Transformers.aliastobean (Clazz)));
Some fields can be obtained by Detachedcriteria the projection method.

Hibernate Multi-table union query?
HQL: If the relationship is configured, it can be used without a join; The HQL writes the join;
QBC, a multi-table association can be implemented with Createcriteria. Criteria Repa=cri.createcriteria ("Repaward", Criteriaspecification.left_join);
Criteria Works=cri.createcriteria ("Works", Criteriaspecification.left_join);

HIbernate session and connection relationship?

    • Session and connection, is a many-to-one relationship, each session has a corresponding connection, a connection at different times can be used for multiple sessions.
    • Multiple sessions are bound to a connection, and the underlying operations database is synchronized.
    • If a connection is being occupied by a session, open a session, then create a new connection corresponding to it.
    • In the case of a connection pool, when the session is closed, the connection does not have to be closed, it can also query the connection occupied by the application, and if it exceeds the maximum idle time, it is released by the connection pool.
    • If there is a connection pool, the session is not closed after use, the connection is occupied, if it exceeds the connection recovery time, it can also be released by the connection pool recovery.
    • In the case of a non-connection pool, a session occupies a connection, and if it is not closed, the connection cannot be freed.
    • Close is required for each open session.

Is the connection pool multiple ports?

Sockets are the interfaces between the application layer and the transport layer in a computer network. If your application needs to use network functionality, you only need to call the socket API. Each process can use more than one socket, and each socket has an identifier. For TCP sockets, the identifier structure is: source IP, source port, destination IP, and destination port . For a Client server architecture network program, the port of the server process is known, such as MySQL5543, and the client process port is randomly assigned. That is, each database connection object, and the database server communication relies on its own socket, and the port number of the socket is different, although it belongs to a process.

Hibernate learning six Hibernate cache

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.