Skip to content

Commit 81fc0bb

Browse files
committed
Best way to use @transactional
1 parent 419e038 commit 81fc0bb

File tree

7 files changed

+247
-0
lines changed

7 files changed

+247
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
**[Avoid Spring Redundant `save()` Call](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootRedundantSave)**
2+
3+
**Description:** This application is an example when calling `save()` for an entity is redundant (not necessary).
4+
5+
**Key points:**
6+
- at flush time, Hibernate relies on *dirty checking* mechanism to determine the potential modifications in entities
7+
- for each modification, Hibernate automatically triggers the corresponding `UPDATE` statement without the need to explicitly call the `save()` method
8+
- behind the scene, this redundancy (calling `save()` when is not necessarily) doesn't affect the number of triggered queries, but it implies a performance penalty in the underlying Hibernate processes
9+
10+
<a href="https://leanpub.com/java-persistence-performance-illustrated-guide"><p align="center"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Java%20Persistence%20Performance%20Illustrated%20Guide.jpg" height="410" width="350"/></p></a>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>HibernateSpringBootTransactionalInRepository</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>HibernateSpringBootTransactionalInRepository</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.1.4.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-jdbc</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.postgresql</groupId>
42+
<artifactId>postgresql</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.bookstore;
2+
3+
import com.bookstore.service.BookstoreService;
4+
import org.springframework.boot.ApplicationRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@SpringBootApplication
10+
public class MainApplication {
11+
12+
private final BookstoreService bookstoreService;
13+
14+
public MainApplication(BookstoreService bookstoreService) {
15+
this.bookstoreService = bookstoreService;
16+
}
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(MainApplication.class, args);
20+
}
21+
22+
@Bean
23+
public ApplicationRunner init() {
24+
return args -> {
25+
26+
bookstoreService.newAuthor();
27+
System.out.println("=========================================");
28+
System.out.println("=========================================");
29+
System.out.println("=========================================");
30+
bookstoreService.longRunningServiceMethod();
31+
System.out.println("=========================================");
32+
};
33+
}
34+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Author implements Serializable {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@Id
15+
//@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
18+
private int age;
19+
private String name;
20+
private String genre;
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getGenre() {
39+
return genre;
40+
}
41+
42+
public void setGenre(String genre) {
43+
this.genre = genre;
44+
}
45+
46+
public int getAge() {
47+
return age;
48+
}
49+
50+
public void setAge(int age) {
51+
this.age = age;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "Author{" + "id=" + id + ", age=" + age
57+
+ ", name=" + name + ", genre=" + genre + '}';
58+
}
59+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.entity.Author;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Modifying;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.stereotype.Repository;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
@Repository
11+
@Transactional(readOnly = true)
12+
public interface AuthorRepository extends JpaRepository<Author, Long> {
13+
14+
@Query("SELECT a FROM Author a WHERE a.id = ?1")
15+
public Author fetchById(long id);
16+
17+
@Transactional
18+
@Modifying
19+
@Query("DELETE FROM Author a WHERE a.name = ?1")
20+
public int deleteByName(String name);
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.bookstore.service;
2+
3+
import com.bookstore.entity.Author;
4+
import com.bookstore.repository.AuthorRepository;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
@Service
9+
public class BookstoreService {
10+
11+
private final AuthorRepository authorRepository;
12+
13+
public BookstoreService(AuthorRepository authorRepository) {
14+
this.authorRepository = authorRepository;
15+
}
16+
17+
@Transactional
18+
public void newAuthor() {
19+
Author a1 = new Author();
20+
a1.setName("Joana Nimar");
21+
a1.setGenre("History");
22+
a1.setId(4L);
23+
a1.setAge(34);
24+
25+
authorRepository.save(a1);
26+
}
27+
28+
@Transactional
29+
public void longRunningServiceMethod() throws InterruptedException {
30+
31+
System.out.println("Service-method start ...");
32+
System.out.println("Sleeping before triggering SQL to simulate a long running code ...");
33+
Thread.sleep(40000);
34+
35+
Author author = authorRepository.fetchById(4L);
36+
authorRepository.deleteByName(author.getName());
37+
38+
System.out.println("Service-method done ...");
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
2+
spring.datasource.username=postgres
3+
spring.datasource.password=root
4+
5+
spring.jpa.hibernate.ddl-auto=create
6+
spring.jpa.show-sql=true
7+
8+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
9+
spring.jpa.properties.hibernate.format-sql=true
10+
11+
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
12+
13+
spring.jpa.open-in-view=false
14+
15+
logging.level.ROOT=INFO
16+
logging.level.org.hibernate.engine.transaction.internal.TransactionImpl=DEBUG
17+
logging.level.org.springframework.orm.jpa=DEBUG
18+
logging.level.org.springframework.transaction=DEBUG
19+
20+
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
21+
logging.level.com.zaxxer.hikari=TRACE
22+
23+
spring.datasource.hikari.auto-commit=false
24+
spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true

0 commit comments

Comments
 (0)