0

I have built an E-Commerce platform that sells food products using Java Servlets and JSP running on Apache Tomcat 8.0 I have deployed the following 3 web apps on the Tomcat server that connect to the same database server (MySQL).

  1. There is a web app for the suppliers of the products to upload their product details including product images.
  2. There is another web app for admins to view the products, edit them and approve them.
  3. There is another web app where the customers view the approved products and perform the shopping processes like adding to cart, checking out, payment, etc.

Everything was working well for sometime now, but as we started modifying the apps to meet up with our customer's needs, the apps are now misbehaving mysteriously.

When admins edit a product price and saves it, it saves successfully without any errors, but the changes doesn't seem to take effect on the customers side, we will update product images but it doesn't take effect as well. Changes do not seem to be taking effect. The most annoying part is that, these changes have been confirmed that it has changed in the MySQL database when we view the database.

The old price and images of products still display even when they have been changed in the database!

When I insert System.out.println("whatever statement here"); in one of the Servlets, that particular Servlet shows the current content for a while and later stops updating if we change any information of the product.

With suspicion of server-side cache in mind, I have checked for solutions in this post, this post and this post but it appears all I have to do is this:

// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");

My problem is that I have so many Servlets, so going around each servlet in each web app to insert the code above is a nightmare I am not ready to face.

Please is there any way I can set something on each web app to disable server-side caching on all servlets and JSPs? Even if the solution requires disabling it on the entire Tomcat server, I will welcome it because this caching really sucks and the admins are really going crazy when product updates are not taking effect.

Please note: I have restarted the apps on Tomcat manager, several times and I have also restarted the Tomcat server itself but no solution. I am also using JPA code to fetch these products from database (I am also suspecting JPA but I am not sure). Here is one of my JPA data access methods:

    public static Product getProductById(int productId)
    {
        EntityManager em = DBConfiguration.getEntityManager();
        try
        {
            Product product = em.find(Product.class, productId);
            if(product != null)
                return product;
            else
                throw new IllegalArgumentException("Product with ID " + productId + " does not exist!");
        }
        finally
        {
            em.close();
        }
    }

If you need more codes for clarification please kindly let me know. Thanks.

Jevison7x
  • 709
  • 2
  • 14
  • 31
  • Tomcat doesn't have any built-in server side cache. All of those cache control mechanism are for **client** caching. Why do you suspect server side caching? *The old price and images of products still display even when they have been changed in the database!* That would be further evidence of **client** caching. **Why** do you think it's a server side issue? – Elliott Frisch May 22 '20 at 20:16
  • @ElliottFrisch I am pretty sure it's not client-side caching because I have checked it on several different browsers including browsers that haven't visited the site before. – Jevison7x May 22 '20 at 20:56
  • I have used private browser, incognito tab, etc and it's still showing old price and images and even old content of the products. – Jevison7x May 22 '20 at 20:57
  • Sounds like an issue in your application instead of a tomcat issue. Is it possible you are using a per server cache for JPA but using an environment with multiple application servers? If you aren't using a shared cache you could see old data depending on what server you are connected to. – Deadron May 22 '20 at 21:04
  • @Deadron I don't really understand what you're saying, can you please throw more light? All the apps are running on the same Tomcat server but different domains, different web apps but one central database for all the apps. – Jevison7x May 22 '20 at 21:22
  • 1
    JPA and its implementations have the idea of a Second-Level Cache which is a cache used to speed up data access. However, when data is changed it needs to be properly notified that the existing cached data is invalid. It does this automatically if you are doing all your changes though JPA. However everyone needs to use the same cache or they may be left with stale data. – Deadron May 22 '20 at 21:26
  • @Deadron - Hmmmmm... You may be right, the code that updates the product is not using JPA, it was using JDBC, we just started using JPA recently... I will work on the code and change all codes that update the product to implement with JPA and test the effects. Thanks for the hint. – Jevison7x May 22 '20 at 21:36

0 Answers0