An article showing how to configure Ehcache in Spring Boot application. It’s covered for both Ehcache 2 and Ehcache 3.

In another article, I have introduced to Spring Cache Abstraction. Following that article, In this article, I am going to show you how to configure Ehcache in the Spring Boot application(similarly, you can do for non-spring application by adding more dependencies).

configure ehcache in spring boot
configure ehcache in spring boot

Before jumping to the code demo section, I want to have a quick introduction to Ehcache, spring-boot-autoconfigure module, and spring-context-support module. Going through these pieces should be great to understand more what I do in the code demo section.

What is Ehcache?

Caching is well-known as the most approach to increase application performance. Caching helps reduce the cost of CPU expensive operations such as Fibonacci calculation. Besides CPU expensive operations, caching also helps reduce IO expensive operations such as reading files from disk. Unfortunately, efficiently applying cache into the application is no simple task.

Providing an abstraction caching layer.

Spring Framework provides cache implementation in the spring-context module with a simple cache store that is ConcurrentHashMapif no cache library found on the classpath. Otherwise, in case we have included supported cache libraries in the classpath,

With the spring cache, It has many built-in annotations to manage cache…

As a java developer, I guest that you heard about the connection pool and might apply it to your system. Therefore in this article, we will learn how to configure C3P0 which is the most popular connection-pool library for java developers.

What is the C3P0 connection pool?

If you are a fan of Start War you might think C3P0 is this guy.

Photo by Lyman Gerona on Unsplash

At my first time hearing about the connection pool and saw C3P0, I have made a mistake about the ZERO and O letter. I did not know and googling c3po and google show me this guy and I was confused. …

Custom domain service was turned back in Medium that helps us to add our domain to point to. I really like this feature because it improves the branding for our publication. So in this article, I will guide you to add a custom domain point to your publication.

Your account must join Medium membership

Medium requires membership for adding custom domain therefore you have to upgrade your account before continuing to the next process.

Check out this page to learn more about Medium membership and subscribe to become a member.

Get your domain

In case you already have your own domain, skip this step.

To buy a domain, we…

Type-safe queries approach helps to verify the correctness of the query statement at compile time.

Have you ever face a problem that you changing the entity field and then the application throw exception at runtime due to the change? If you have been facing that problem I think using type-safe queries might be a good choice.

Problem with JPQL or Native query

When writing query statements we mostly use JPQL or native query and write literally in string.

incorrect query statement in a string-based form

I purposely wrote the wrong query but we got no warning from the compiler because it has no way to detect such errors. …

In this article, we learn how to use Transactional annotation in a spring application to guarantee the consistency of data.

Why we need transactions?

Let start with an example of saving order information. In many cases to store order information, we will have two tables let call Order and OrderLine. In consequently the system must save data for the order first then sort info of order line items. Now have to think about the case we save Order data successfully but the OrderLine failed. For this case, we have to rollback the order info to be not saved in the database to guarantee the consistency of the order information. In such problems, we have to use transactions.

working with transaction in applications
working with transaction in applications
common flow when working with transaction

The benefits of using Transactional annotation

Reduce boilplate code

In java or other languages, the developer…

Let’s learn how to update the document with MongoTemplate with an explanation of how it works.

Follow part one of the CRUD document using MongoTemplate series which shares about inserting documents, in this article, we will learn how to update documents by using MongoTemplate.

Update behavior

MongoTemplate provides a save method that allows updating documents. The diagram below shows how the save method work in basic.

How the mongotemplate save method works
How the mongotemplate save method works
  • At first, if the _id value was not provided the MongoDB will process the saving document as inserting a new document.
  • If the _id provided, MongoDB server will scan the collection and tries to a matching document with provided _id value. If a document matching the provided _id found, it will be…

Spring Data MongoDB provides a class named MongoTemplate which allows developers to perform common operations with the MongoDB server such as adding documents, updating documents, deleting documents, or collections.

This article is the first part of CRUD document using MongoTemplate that shares a detailed explanation of inserting document process while insert document with MongoTemplate.

Insert a new document process

Insert new document process in mongodb
Insert new document process in mongodb

First, we have a new document to be inserted to Mongodb, If the collection of the document does not exist(created) the MongoDB will create the collection first then continue to perform the insert process.

If the inserted document omitted the _id field, the MongoDB driver automatically…

The @Document annotation

This annotation represents a MongoDB collection. It similars to Entity annotation of Java persistence API. We must be put this annotation at the class level. If the collection’s name different from the class name we can specify the name by using the collection property or value property.

Document annotation — Spring Data Mongodb
Document annotation — Spring Data Mongodb

The Field annotation

The Field annotation used to represent a field in a collection. It is similar to the Column annotation in java persistence API but it does not have much property like java.persistence.Column.

Field properties:

  • name → to specify the name of the field stored in MongoDB. …

Centralizing cache or localizing cache.

Many of us have known about the distributed system, right? It is the kind of software architecture that we distribute the application on multiple servers. It allows us to build high scalability and high reliability. The drawback is managing data consistency becomes more complex. And invalidate cache is one of the complex tasks too. Therefore in this article, I would like to share two caching approaches for a distributed system. Centralized Cache or Localized Cache.

Centralizing cache for distributed system

The centralizing cache is mostly done by using cache servers such as Memcached or Redis. …

Thanh Tran

Software Engineer, Blogger and Amateur Investor.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store