使用HazelCast进行休眠缓存:基本配置

以前,我们对JPA缓存,机制以及hibernate提供的内容进行了介绍 。

接下来是一个使用Hazelcast作为二级缓存的休眠项目。

为此,我们将在JPA中使用一个基本的spring boot项目。 Spring Boot使用休眠作为默认的JPA提供程序。
我们的设置将非常接近上一篇文章 。
我们将docker与postgresql一起用于我们的sql数据库。

group 'com.gkatzioura'
version '1.0-SNAPSHOT'buildscript {repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")}
}apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'repositories {mavenCentral()
}dependencies {compile("org.springframework.boot:spring-boot-starter-web")compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'compile group: 'org.postgresql', name:'postgresql', version:'9.4-1206-jdbc42'compile group: 'org.springframework', name: 'spring-jdbc'compile group: 'com.zaxxer', name: 'HikariCP', version: '2.6.0'compile group: 'com.hazelcast', name: 'hazelcast-hibernate5', version: '1.2'compile group: 'com.hazelcast', name: 'hazelcast', version: '3.7.5'testCompile group: 'junit', name: 'junit', version: '4.11'
}

通过仔细检查依赖关系,我们可以看到hikari池,postgresql驱动程序,spring数据jpa,当然还有hazelcast。

我们将通过利用Spring boot的数据库初始化功能来自动化数据库,而不是手动创建数据库。

我们将在resources文件夹下创建一个名为schema.sql的文件。

create schema spring_data_jpa_example;create table spring_data_jpa_example.employee(id  SERIAL PRIMARY KEY,firstname   TEXT    NOT NULL,lastname    TEXT    NOT NULL,   email       TEXT    not null,age         INT     NOT NULL,salary         real,unique(email)
);insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) 
values ('Test','Me','test@me.com',18,3000.23);

为了简单起见,避免进行任何其他配置,我们将把数据源,jpa和缓存的配置放在application.yml文件中。

spring:datasource:continue-on-error: truetype: com.zaxxer.hikari.HikariDataSourceurl: jdbc:postgresql://172.17.0.2:5432/postgresdriver-class-name: org.postgresql.Driverusername: postgrespassword: postgreshikari:idle-timeout: 10000jpa:properties:hibernate:cache:use_second_level_cache: trueuse_query_cache: trueregion:factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactoryshow-sql: true

配置spring.datasource.continue-on-error至关重要,因为一旦应用程序重新启动,就应该再次尝试创建数据库,因此崩溃是不可避免的。

任何休眠特定的属性都驻留在spring.jpa.properties路径中。 我们启用了二级缓存和查询缓存。

同样,我们将show-sql设置为true。 这意味着,一旦查询命中数据库,就应通过控制台对其进行记录。

然后创建我们的员工实体。

package com.gkatzioura.hibernate.enitites;import javax.persistence.*;/*** Created by gkatzioura on 2/6/17.*/
@Entity
@Table(name = "employee", schema="spring_data_jpa_example")
public class Employee {@Id@Column(name = "id")@GeneratedValue(strategy = GenerationType.SEQUENCE)private Long id;@Column(name = "firstname")private String firstName;@Column(name = "lastname")private String lastname;@Column(name = "email")private String email;@Column(name = "age")private Integer age;@Column(name = "salary")private Integer salary;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastname() {return lastname;}public void setLastname(String lastname) {this.lastname = lastname;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getSalary() {return salary;}public void setSalary(Integer salary) {this.salary = salary;}
}

一切都已设置。 Spring Boot将检测到该实体并自行创建一个EntityManagerFactory。 接下来是员工的存储库类。

package com.gkatzioura.hibernate.repository;import com.gkatzioura.hibernate.enitites.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;/*** Created by gkatzioura on 2/11/17.*/
public interface EmployeeRepository extends JpaRepository<Employee,Long> {
}

最后一个是控制器

package com.gkatzioura.hibernate.controller;import com.gkatzioura.hibernate.enitites.Employee;
import com.gkatzioura.hibernate.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** Created by gkatzioura on 2/6/17.*/
@RestController
public class EmployeeController {@Autowiredprivate EmployeeRepository employeeRepository;@RequestMapping("/employee")public List<Employee> testIt() {return employeeRepository.findAll();}@RequestMapping("/employee/{employeeId}")public Employee getEmployee(@PathVariable Long employeeId) {return employeeRepository.findOne(employeeId);}}

一旦我们在http:// localhost:8080 / employee / 1发出请求

控制台将显示在数据库发出的查询

Hibernate: select employee0_.id as id1_0_0_, employee0_.age as age2_0_0_, employee0_.email as email3_0_0_, employee0_.firstname as firstnam4_0_0_, employee0_.lastname as lastname5_0_0_, employee0_.salary as salary6_0_0_ from spring_data_jpa_example.employee employee0_ where employee0_.id=?

第二次发出请求时,由于启用了第二个缓存,因此不会在数据库上发出查询。 相反,应从二级缓存中获取实体。

您可以从github下载该项目。

翻译自: https://www.javacodegeeks.com/2017/02/hibernate-caching-hazelcast-basic-configuration.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/350081.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql事务实战_mysql事务隔离级别详解和实战

A事务做了操作 没有提交 对B事务来说 就等于没做 获取的都是之前的数据但是 在A事务中查询的话 查到的都是操作之后的数据没有提交的数据只有自己看得到&#xff0c;并没有update到数据库。查看InnoDB存储引擎 系统级的隔离级别 和 会话级的隔离级别&#xff1a;mysql> sele…

微云 linux_编年史与微云

微云 linux总览 我面临的一个常见问题是&#xff1a; 如果是单个作者&#xff0c;多个读者&#xff0c;如何缩放基于Chronicle的系统。 尽管有解决此问题的方法&#xff0c;但很有可能根本不会有问题。 微云 这是我用来描述单个线程来完成当前由多个服务器完成的工作的术语。 …

H3C 计算子网内可用主机地址数

转载于:https://www.cnblogs.com/fanweisheng/p/11153665.html

mysql中两次排序_MySQL中的两种排序方式: index和filesort

index &#xff1a;通过有序索引顺序扫描直接返回有序数据&#xff0c;不需要额外的排序&#xff0c;操作效率较高。filesort&#xff1a;通过对返回数据进行排序&#xff0c;filesort 并不代表通过磁盘文件排序&#xff0c;而是说明进行了一个排序操作&#xff0c;至于排序操作…

Java命令行界面(第2部分):args4j

在上一篇文章中 &#xff0c;我研究了如何使用Apache Commons CLI在Java应用程序中解析命令行参数。 在本文中&#xff0c;我将使用另一个库args4j进行相同的操作。 args4j采用了一种不同于Commons CLI的方式来指定Java应用程序应期望的命令行参数。 尽管Commons CLI期望代表选…

mysql my.cnf在哪里_my.cnf配置文件在哪

my.cnf配置文件在linux上是位于路径“/etc/my.cnf”下&#xff0c;在window上则位于安装目录的根目录下&#xff1b;可以使用命令“mysql --help”查看关于MYSQL对应配置文件“my.cnf”搜索顺序。一般linux上都放在 /etc/my.cnf ,window 上安装都是默认可能按照上面的路径还是没…

深入学习Mybatis框架(二)- 进阶

1.动态SQL 1.1 什么是动态SQL&#xff1f; 动态SQL就是通过传入的参数不一样,可以组成不同结构的SQL语句。 这种可以根据参数的条件而改变SQL结构的SQL语句,我们称为动态SQL语句。使用动态SQL可以提高代码重用性。 1.2 XML方式的实现 1.2.1 需要使用到的标签 <if> 用于判…

近似线性依靠matlab_不要仅仅依靠单元测试

近似线性依靠matlab当您构建一个复杂的系统时&#xff0c;仅仅测试组件是不够的。 这很关键&#xff0c;但还不够。 想象一下一家汽车厂生产和进口最高质量的零件&#xff0c;但组装好汽车后再也没有启动发动机。 如果您的测试用例套件几乎不包含单元测试&#xff0c;那么您将永…

戏说 .NET GDI+系列学习教程(三、Graphics类的方法的总结)

转载于:https://www.cnblogs.com/WarBlog/p/11157395.html

从关系数据库到Elasticsearch的索引数据– 1

Elasticsearch提供强大的搜索功能&#xff0c;并支持数据的分片和复制。 因此&#xff0c;我们希望将数据库中可用的数据索引到Elasticsearch中。 有多种方法可以将数据索引到Elasticsearch中&#xff1a; 使用Logstash将源设置为DB&#xff0c;将接收器设置为Elasticsearch&…

mysql 求bit 某位为1_mysql按位的索引判断值是否为1

DELIMITER $$DROP FUNCTION IF EXISTS value_of_bit_index_is_true$$/*计算某个数字的某些索引的位的值是否都为1&#xff0c;索引类似1,2,3,4*/CREATE FUNCTION value_of_bit_index_is_true(number INT, idxies VARCHAR(50)) RETURNS INT(11)BEGIN/*将1,2,3,4,5,6这样的字符串…

python math.asin

import mathmath.asin(x) x : -1 到 1 之间的数值。如果 x 是大于 1&#xff0c;会产生一个错误。 #!/usr/bin/pythonimport math print "asin(0.64) : ", math.asin(0.64)print "asin(0) : ", math.asin(0)print "asin(-1) : ", math.asin(-1)p…

消息队列mysql redis那个好_Redis与RabbitMQ作为消息队列的比较

简要介绍RabbitMQRabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种&#xff0c;最初起源于金融系统&#xff0c;用于在分布式系统中存储转发消息&#xff0c;在易用性、扩展性、高可用性等方面表现不俗。消息中间件主要用于组件之间的解耦&#xff0c;消息的发送者无需…

mysql 添加远程连接_为 mysql 添加远程连接账户

1、以管理员身份登录mysqlmysql -u root -p2、选择mysql数据库use mysql3、创建用户并设定密码create user [email protected] identified by ‘123456‘4、使操作生效flush privileges5、使操作生效flush privileges6、用新用户登录mysql -u test -p允许用户从远程访问数据库的…

包装类型与包装类别_包装的重要性

包装类型与包装类别我记得大约15年前开始学习Java的时候。 我读到了很多有关“包装”和“命名空间”的东西&#xff0c;但我完全不了解。 可悲的是&#xff1a;虽然包装的某些方面几乎为行业中的每个人所了解&#xff0c;但其他方面却不是。 因此&#xff0c;让我们看一下哪些软…

isinstance和issubclass

目录 一、isinstance与type二、issubclass一、isinstance与type 在游戏项目中&#xff0c;我们会在每个接口验证客户端传过来的参数类型&#xff0c;如果验证不通过&#xff0c;返回给客户端“参数错误”错误码。 这样做不但便于调试&#xff0c;而且增加健壮性。因为客户端是可…

animation动画不生效_你可能不知道的Animation动画技巧与细节

引言在 web 应用中&#xff0c;前端同学在实现动画效果时往往常用的几种方案&#xff1a;css3 transition / animation - 实现过渡动画setInterval / setTimeout - 通过设置一个间隔时间来不断的改变图像的位置requestAnimationFrame - 通过一个回调函数来改变图像位置&#xf…

eclipse查看jar包源代码

方法一&#xff1a;将jd-gui集成在Eclipse中 https://jingyan.baidu.com/article/b24f6c8275536686bfe5daed.html 下载2个反编译文件&#xff0c;实际操作未解决 https://www.cnblogs.com/jianshuai520/p/9267273.html 反编译器的位置&#xff0c;发生改变 方法二&#xf…

微服务系列:MicroProfile和Apache TomEE

介绍 MicroProfile是一项由知名供应商于2016年9月发起的举措&#xff0c;目的是基于JEE平台构建微服务架构。 任务是针对微服务架构优化Enterprise Java 。 开发人员可以利用这种体系结构&#xff0c;通过Enterprise Java平台以标准化的方式构建和开发微服务应用程序。 API构建…

crash recovery mysql_InnoDB crash recovery 完整过程

mysql Innodb在发生意外宕机&#xff0c;重启之后&#xff0c;要经历哪些过程&#xff0c;以下是详细过程。• Tablespace discoveryTablespace discovery is the process that InnoDB uses to identify tablespaces that require redo log application. See Tablespace Discov…