Home Database Mysql Tutorial Hibernate中的Session缓存问题

Hibernate中的Session缓存问题

Jun 07, 2016 pm 04:09 PM
hibernate session cache question

1. Session 缓存: 1) . 在 Session 接口的实现中包含一系列的 Java 集合 , 这些 Java 集合构成了 Session 缓存 . 它 用于存放 Session 关联的对象 ( Session 关联对象的方式有很多种。 例如:session.get ( Class , OID ) 、 session.update () 、 session

1. Session 缓存:
1) . 在 Session 接口的实现中包含一系列的 Java 集合 , 这些 Java 集合构成了 Session 缓存 . 它用于存放 Session 关联的对象( Session 关联对象的方式有很多种。 例如:session.get (Class , OID )、 session.update()、 session.save () ...)。 只要 Session 实例没有结束生命周期 , 且没有清理缓存,则存放在它缓存中的对象也不会结束生命周期。 Session 缓存可减少 Hibernate 应用程序访问数据库的频率。
2) . 操作 Session 缓存的方法(了解一下吧)。
①. 若调用 session.get () 从数据库中加载一个对象,则该对象会被纳入到 Session 缓存中。
News news = (News ) session.get (News.class , 1);
//会向数据库发送 SQL 吗 ? 不会发送 SQL 语句 , 而是从 Session 缓存获取对象的引用(快照) News news2 = (News ) session.get (News.class , 1);
②. Session 的 clear() 方法可以清空 Session 的缓存
News news = (News) session.get(News.class, 1);
//清理 session 缓存 session.clear (); //会向数据库发送 SQL 吗 ? 会 ! 因为 Session 缓存被清空了 ! News news2 = (News ) session.get (News.class , 1);
③. Session 的 flush() 方法: 清理缓存 - 强制使数据库记录和 Session 缓存中对象状态保持一致,可能会发送 SQL 语句 (若数据库记录和 Session 中对象状态不一致,则发送 SQL,否则不发送 SQL)
I. 默认情况下,提交事务时,会先清理缓存,然后再提交事务
II. 若主键生成方式使用的是数据库底层的自增长方式,会在执行 Session 的 save() 方法时,就清理缓存,执行 INSERT 语句,而不是等到提交事务时。 Hibernate 要求和 Session 关联的对象必须有和数据表记录对应的 OID,这就意味着执行 save() 方法后,必须有 OID ,而底层自增长的方式生成主键,必须先执行 INSERT 才能获取主键值
//若使用 MySQL 底层自增的方式生成主键, save() 方法即会引起发送 INSERT 语句 session.save (news ); System.out.println (news.getId ());
III. 使用 HQL(Hibernate Query Language )查询记录时,不经过 Session 缓存!直接查询数据库,且要求查询的结果是最新的! 所以,在进行 HQL 查询之前需要先清理缓存 session.save(news);
//会导致清理缓存 News news2 = (News) session.createQuery ("FROM News n WHERE n.id = ?" ) .setInteger (0 , news.getId ()).uniqueResult ();
IV. commit() 和 flush() 方法的区别: flush 执行一系列 sql 语句,但不提交事务; commit 方法先调用flush() 方法,然后提交事务 . 意味着提交事务对数据库的操作永久保存下来。
④. refresh () 方法: 强制使 Session 缓存中的对象的状态和数据库记录保持一致。所以会强制发送一条 SELECT 语句。 注意,因为 MySQL 的默认的隔离级别为 READ REPTABLE 。所以需要设置事务的隔离级别才能看到实验的效果
!-- 设置 Hibernate 的事务的隔离级别, 设置为读已提交 --> property name ="connection.isolation" >2 property >



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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1238
24
Where are video files stored in browser cache? Where are video files stored in browser cache? Feb 19, 2024 pm 05:09 PM

Which folder does the browser cache the video in? When we use the Internet browser every day, we often watch various online videos, such as watching music videos on YouTube or watching movies on Netflix. These videos will be cached by the browser during the loading process so that they can be loaded quickly when played again in the future. So the question is, in which folder are these cached videos actually stored? Different browsers store cached video folders in different locations. Below we will introduce several common browsers and their

How to view and refresh dns cache in Linux How to view and refresh dns cache in Linux Mar 07, 2024 am 08:43 AM

DNS (DomainNameSystem) is a system used on the Internet to convert domain names into corresponding IP addresses. In Linux systems, DNS caching is a mechanism that stores the mapping relationship between domain names and IP addresses locally, which can increase the speed of domain name resolution and reduce the burden on the DNS server. DNS caching allows the system to quickly retrieve the IP address when subsequently accessing the same domain name without having to issue a query request to the DNS server each time, thereby improving network performance and efficiency. This article will discuss with you how to view and refresh the DNS cache on Linux, as well as related details and sample code. Importance of DNS Caching In Linux systems, DNS caching plays a key role. its existence

Will HTML files be cached? Will HTML files be cached? Feb 19, 2024 pm 01:51 PM

Title: Caching mechanism and code examples of HTML files Introduction: When writing web pages, we often encounter browser cache problems. This article will introduce the caching mechanism of HTML files in detail and provide some specific code examples to help readers better understand and apply this mechanism. 1. Browser caching principle In the browser, whenever a web page is accessed, the browser will first check whether there is a copy of the web page in the cache. If there is, the web page content is obtained directly from the cache. This is the basic principle of browser caching. Benefits of browser caching mechanism

The relationship between CPU, memory and cache is explained in detail! The relationship between CPU, memory and cache is explained in detail! Mar 07, 2024 am 08:30 AM

There is a close interaction between the CPU (central processing unit), memory (random access memory), and cache, which together form a critical component of a computer system. The coordination between them ensures the normal operation and efficient performance of the computer. As the brain of the computer, the CPU is responsible for executing various instructions and data processing; the memory is used to temporarily store data and programs, providing fast read and write access speeds; and the cache plays a buffering role, speeding up data access speed and improving The computer's CPU is the core component of the computer and is responsible for executing various instructions, arithmetic operations, and logical operations. It is called the "brain" of the computer and plays an important role in processing data and performing tasks. Memory is an important storage device in a computer.

Advanced Usage of PHP APCu: Unlocking the Hidden Power Advanced Usage of PHP APCu: Unlocking the Hidden Power Mar 01, 2024 pm 09:10 PM

PHPAPCu (replacement of php cache) is an opcode cache and data cache module that accelerates PHP applications. Understanding its advanced features is crucial to utilizing its full potential. 1. Batch operation: APCu provides a batch operation method that can process a large number of key-value pairs at the same time. This is useful for large-scale cache clearing or updates. //Get cache keys in batches $values=apcu_fetch(["key1","key2","key3"]); //Clear cache keys in batches apcu_delete(["key1","key2","key3"]);2 .Set cache expiration time: APCu allows you to set an expiration time for cache items so that they automatically expire after a specified time.

Spring Boot performance optimization tips: create applications as fast as the wind Spring Boot performance optimization tips: create applications as fast as the wind Feb 25, 2024 pm 01:01 PM

SpringBoot is a popular Java framework known for its ease of use and rapid development. However, as the complexity of the application increases, performance issues can become a bottleneck. In order to help you create a springBoot application as fast as the wind, this article will share some practical performance optimization tips. Optimize startup time Application startup time is one of the key factors of user experience. SpringBoot provides several ways to optimize startup time, such as using caching, reducing log output, and optimizing classpath scanning. You can do this by setting spring.main.lazy-initialization in the application.properties file

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

How to save video files from browser cache to local How to save video files from browser cache to local Feb 23, 2024 pm 06:45 PM

How to Export Browser Cache Videos With the rapid development of the Internet, videos have become an indispensable part of people's daily lives. When browsing the web, we often encounter video content that we want to save or share, but sometimes we cannot find the source of the video files because they may only exist in the browser's cache. So, how do you export videos from your browser cache? This article will introduce you to several common methods. First, we need to clarify a concept, namely browser cache. The browser cache is used by the browser to improve user experience.

See all articles