Skip to content

Commit

Permalink
Upgraded to Boot 2.2.1, this fixed the javax.net.ssl.SSLException thr…
Browse files Browse the repository at this point in the history
…own when application was stopped because the mysql driver had a bug. Also added graceful stopping of the application. And a CommandLineRunner to control application from command line and avoid duplicated declaration of beans.

Added dropping tables  when app is closed, so it can be run as many times as necessary.
  • Loading branch information
iuliana committed Nov 27, 2019
1 parent 1a008f6 commit 59f548a
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 58 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ build.xml
manifest.mf
nbactions.xml
nb-configuration.xml
out/
*.iml
.idea
8 changes: 2 additions & 6 deletions HibernateSpringBootAudit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java.version>12</java.version>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
Expand All @@ -31,10 +31,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
package com.bookstore;

import com.bookstore.auditor.AuditorAwareImpl;
import com.bookstore.service.BookstoreService;
import org.springframework.boot.ApplicationRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
public class MainApplication {

private final BookstoreService bookstoreService;

public MainApplication(BookstoreService bookstoreService) {
this.bookstoreService = bookstoreService;
}

public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
private static final Logger logger = LoggerFactory.getLogger(MainApplication.class);

@Bean
public AuditorAware<String> auditorAware() {
return new AuditorAwareImpl();
public static void main(String... args) throws Exception {
var ctx = SpringApplication.run(MainApplication.class, args);

logger.info("Started ...");
System.in.read();
ctx.close();
}

@Bean
public ApplicationRunner init() {
public CommandLineRunner scheduleRunner(BookstoreService bookstoreService) {
return args -> {
System.out.println("Register new author ...");
bookstoreService.registerAuthor();

Thread.sleep(5000);

System.out.println("Update an author ...");
bookstoreService.updateAuthor();
bookstoreService.updateAuthor();

Thread.sleep(5000);
System.out.println("Update books of an author ...");
bookstoreService.updateBooks();
bookstoreService.updateBooks();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import java.util.Optional;
import java.util.Random;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

@Component("auditorAware")
public class AuditorAwareImpl implements AuditorAware<String> {

@Override
public Optional<String> getCurrentAuditor() {
// use Spring Security to retrive the currently logged-in user(s)
return Optional.of(Arrays.asList("mark1990", "adrianm", "dan555")
.get(new Random().nextInt(3)));
// use Spring Security to retrieve the currently logged-in user(s)
return Optional.of(Arrays.asList("mark1990", "adrianm", "dan555").get(new Random().nextInt(3)));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bookstore.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand All @@ -9,7 +8,7 @@
import javax.persistence.OneToMany;

@Entity
public class Author extends BaseEntity<String> implements Serializable {
public class Author extends BaseEntity<String> {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bookstore.entity;

import java.io.Serializable;
import java.time.LocalDateTime;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
Expand All @@ -14,7 +15,7 @@

@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
public abstract class BaseEntity<U> {
public abstract class BaseEntity<U> implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.bookstore.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Book extends BaseEntity<String> implements Serializable {
public class Book extends BaseEntity<String> {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
@Repository
public interface AuthorRepository extends JpaRepository<Author, Long> {

public Author findByName(String name);
Author findByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ public BookstoreService(AuthorRepository authorRepository) {
@Transactional
public void registerAuthor() {

Author a1 = new Author();
var a1 = new Author();
a1.setName("Quartis Young");
a1.setGenre("Anthology");
a1.setAge(34);

Author a2 = new Author();
var a2 = new Author();
a2.setName("Mark Janel");
a2.setGenre("Anthology");
a2.setAge(23);

Book b1 = new Book();
var b1 = new Book();
b1.setIsbn("001");
b1.setTitle("The Beatles Anthology");

Book b2 = new Book();
var b2 = new Book();
b2.setIsbn("002");
b2.setTitle("A People's Anthology");

Book b3 = new Book();
var b3 = new Book();
b3.setIsbn("003");
b3.setTitle("Anthology Myths");

Expand All @@ -52,14 +52,13 @@ public void registerAuthor() {

@Transactional
public void updateAuthor() {
Author author = authorRepository.findByName("Mark Janel");

var author = authorRepository.findByName("Mark Janel");
author.setAge(45);
}

@Transactional
public void updateBooks() {
Author author = authorRepository.findByName("Quartis Young");
var author = authorRepository.findByName("Quartis Young");
List<Book> books = author.getBooks();

for (Book book : books) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
public class UserService {

public String getCurrentUserName() {
// use Spring Security to retrive the current user
return Arrays.asList("mark1990", "adrianm", "dan555")
.get(new Random().nextInt(3));
// use Spring Security to retrieve the current user
return Arrays.asList("mark1990", "adrianm", "dan555").get(new Random().nextInt(3));
}
}
12 changes: 0 additions & 12 deletions HibernateSpringBootAudit/src/main/resources/application.properties

This file was deleted.

23 changes: 23 additions & 0 deletions HibernateSpringBootAudit/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/bookstoredb?createDatabaseIfNotExist=true&useLegacyDatetimeCode=false
username: root
password: root

jpa:
hibernate.ddl-auto: create-drop
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
jdbc:
time_zone: UTC
open-in-view: false

logging:
pattern:
console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
level:
root: INFO
org.springframework: DEBUG
com.apress.cems.reactive: DEBUG

0 comments on commit 59f548a

Please sign in to comment.