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).
- There is a web app for the suppliers of the products to upload their product details including product images.
- There is another web app for admins to view the products, edit them and approve them.
- 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.