Description
Book SynopsisUse the popular Spring Data project for data access and persistence using various Java-based APIs such as JDBC, JPA, MongoDB, and more.
This book shows how to easily incorporate data persistence and accessibility into your microservices, cloud-native applications, and monolithic enterprise applications. It also teaches you how to perform unit and performance testing of a component that accesses a database. And it walks you through an example of each type of SQL and NoSQL database covered.
After reading this book, you''ll be able to create an application that interacts with one or multiple types of databases, and conduct unit and performance testing to analyze possible problems. Source code is available on GitHub.
What You''ll Learn
- Become familiar with the Spring Data project and its modules for data access and persistence
- Explore various SQL and NoSQL persistence types
- Uncover the persistence and domain models, and handle transaction management for SQL
- Migrate database changes and versioning for SQL
- Dive into NoSQL persistence with Redis, MongoDB, Neo4j, and Cassandra
- Handle reactive database programming and access with R2DBC and MongoDB
- Conduct unit, integration, and performance testing, and more
Who This Book Is For
Experienced Java software application developers; programmers with experience using the Spring framework or the Spring Boot micro framework
Table of ContentsPart I - IntroductionChapter 1: Architecture of the ApplicationsChapter 2: Spring Basics and BeyondChapter 3: Spring Data and Persistence Types
Part II - SQL PersistenceChapter 4: Persistence and Domain ModelChapter 5: Transaction ManagementChapter 6: Versioning or Migrate the Changes of the Database
Part III - NO-SQL PersistenceChapter 7: Redis key/value DatabaseChapter 8: MongoDB Document DatabaseChapter 9: Neo4j Graph DatabaseChapter 10: Cassandra wide-column DatabaseChapter 11: Reactive access w/R2DBC and MongoDBChapter 12: Unit/Integration TestingChapter 13: Performance TestingChapter 14: Best Practices
Part I - IntroductionThis part or section contains all the introduction about the basics of the Spring and the architecture of theapplication to use the persistence.Chapter 1: Architecture of the applicationsChapter Goal: In this chapter, the readers will see the different ways of structuring one application and thebest practices to organize all the things related to persistence like the use of DAO (repositories on Spring).• Small history of the methods of persistence (Plain query using the class of Java, ORM)• Different types of architectureso Layerso Hexagonal or onion• Persistence design patterso DAO (Repositories in Spring)o Data Transfer Object (DTO)Chapter 2: Spring basics and beyondChapter Goal: In this chapter, the readers will see the different ways of structuring one application and thebest practices to organize all the things related to persistence like the use of DAO (repositories on Spring).• Spring’s Architecture • Dependency Injection and Inversion of Control• Basic Application SetupChapter 3: Spring Data and different types of persistenceChapter Goal: This chapter will provide a full explanation about Spring Data, how it works and what this librarydoes behind the scenes.• How the Spring Data works• How the Repositories workso Using interfaceso Defining a custom implementationPart II - SQL persistenceThis part or section contains the information about different aspects of the persistence of databases whichhave a rigid schema. Also, the readers will see different strategies of deploying the changes on the schemas.Chapter 4: Persistence and domain modelChapter Goal: In this chapter, the readers will learn the basics about persistence and how it works behind thescenes. Also, the readers will see how to create validations in the schema like the size of the column and thedifferent types of relationship between entities.• JPA configuration using annotationso Entity, Ido Types of relationshipso Pre-update, pre-persist• Ways to define the querieso Using specificationso Define SQL• How validate the schema• Types of InherenceChapter 5: Transaction managementChapter Goal: In this chapter, the readers will learn the basics of the transactions and some concepts of ACID.• Definition of ACID• Isolation Levels• Transactional levelsChapter 6: Versioning or migrate the changes of the databaseChapter Goal: In this chapter the readers will see different tools or strategies to include the changes of thedatabases, e.g use Liquibase/Flyway, running the scripts manually, or using the auto-update of the Spring.Also, this chapter will include some mechanism to move the data from one column to another using featureflags.• Mechanism to migrate the changes• Tools to versioning the changeso Liquibaseo Flyway• Using Feature Flags to new featureso What is a Feature flag?o Benefits of use this approacho Common librariesPart III - NO-SQL persistenceIn this section the idea is to cover one example of each type of the databases NO-SQL like key/value,document, graph, and wide-column database. The idea is not to cover all more than one example of a type ofdatabase because most of them have certain operations similar.Chapter 7: Redis key/value databaseChapter Goal: In this chapter, the readers will see how to run a database and save the information using aspecific key. Also, this chapter will show the readers to create a serializer to persist data that is complex andsome best practices like persist the information in async mode.The last point is how to configure the TTL in the information that the readers persist in the database.• What is Redis and which are the benefits?• Connecting with multiples Redis• Persist synchronous or asynchronous• Object Mapping and ConversionChapter 8: MongoDB Document databaseChapter Goal: In this chapter, the readers will see how to run a mongo database and how to persist theinformation with the definition of the entities using the different operations that are permitted on MongoDB.• What is a document store?• Setting up a Mongo• Access using repositories• Manage transactions in a programmatic wayChapter 9: Neo4j Graph databaseChapter Goal: In this chapter, the readers will see how to run a database and how to create different types ofqueries. Also the reader will see the different aspects of the persistence of the information and the use ofreactive approach.• Modeling the problem as a Grapho Cases of usero Benefits• Persisting the domain• Manage transactionsChapter 10: Cassandra wide-column databaseChapter Goal: In this chapter, the readers will see how to configure the database on Spring and thedeclaration of the entities that need to be used to persist the information. Also, the different ways topersist or modify the information on Cassandra.The last point is how to configure the TTL in the information that the readers persist in the database.• What is Cassandra and how works?• Configuration for Cassandra• Access using repositories• Defining a TTLPart IV – Advanced, testing and best practicesThis part covers some aspects of any type of database to create different types of tests and validate theperformance of the queries. Also, this section covers some best practices to reduce the possible problems ormistakes in the applications that access a database.Chapter 11: Reactive accessChapter Goal: This chapter needs to cover how you can access and obtain the information in a reactive way.• What is reactive access?• Modifying queries to be reactiveo R2DBCo MongoDBChapter 12: Unit/Integration testingChapter Goal: This chapter needs to cover more in detail how you can write unit tests without using anexistent database but using the same motor of the database, to do this the reader will use Test Cointainerswith Junit 5 which is the standard to write unit tests.• Unit Testing with Mocks• Integration Testing with a Databaseo What is Test containers?o Test Containers vs embeddedo How you can use it?o Possible problems in the pipelineChapter 13: Performance testingChapter Goal: In this chapter the reader will use some tools like Gatling or QuickPerform to see how tocreate a performance test and analyze if the queries have some issue related with the use ofCPU/memory.• How check or analyze the performance of the queries?• Analyzing the complexity of queries• Performance test of an endpoint that access to a DBChapter 14: Best practicesChapter Goal: In this chapter the reader will know some strategies to improve the performance of thedatabase including some mechanism of cache to reduce the number of times that anyone accesses toobtain information. • Access to the information◦ Master-slave • Using cache to reduce the accessed to DB • Compress the information • Lazy Loading Issues • Pagination and ways to reduce the response