Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7744357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:44:54+00:00 2026-06-01T09:44:54+00:00

I am building a Java SE application that is powered by Hibernate. Mainly many

  • 0

I am building a Java SE application that is powered by Hibernate. Mainly many Java SE instances would run and there would be many Hibernate Session factories. When one client machine insert an Object to the datasource, other clients won’t see it unless I clear the cache. Is there a better mechanism to use in this case?
Below is my cache clearing method.

public static void clearCache() {
        HibernateHelper.beginTranscation();
        HibernateHelper.getCurrentSession().clear();
        HibernateHelper.getSessionFactory().evictQueries();
        try {
            Map<String, ClassMetadata> classesMetadata = HibernateHelper
                    .getSessionFactory().getAllClassMetadata();
            for (String entityName : classesMetadata.keySet()) {
                HibernateHelper.getSessionFactory().evictEntity(entityName);
            }
        } catch (Exception e) {
        }
        HibernateHelper.commit();
    }

Please note the fact that I am using the Second Level cache (Memcache) and Query cache as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:44:55+00:00Added an answer on June 1, 2026 at 9:44 am

    The way you are using the L2 cache might cause data inconsistency as your application is using multiple session factories and hence it’s distributed and needs distributed cache.

    Here is how your system looks

    enter image description here

    OR

    Alternatively you can have a central memcache server which each of the client (with the help of memcached client ) will use. (One memcached used by all users )

    The basic requirement is that the changes committed by one user/session factory should be visible to the other users/session factory for leveraging the cache. The code sample given by you is not helpful as it will clean the L2 cache every time a transaction commits. This is anyways done by L2 cache or session cache.

    so my suggestions would be

    1. Don’t use L2 cache at all hence no local memcache. You are not leveraging it anyways as you are cleaning the cache after every transaction.
    2. If at all L2 cache is needed, use a distributed cache (which is slightly complicated) OR use a single memcached server for all.

    Please check this link as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a Java application that has parts that require daily update. I want
I'm currently working on a project that is building a java-based desktop application to
I'm building a Java application with a Swing GUI and a task that takes
I'm building an application that needs to open self-signed HTTPS SSL URLs in java.
I have a web application that I am building using IDEA. There are some
I'm building a java application that gets the mac addresses of a user and
I am building a development environment for a Java EE 6 application that i
I am currently building a java-servlet-based web application that should offer its service to
I'm building a Java application that will allow our users to load a list
We are building an application that will have clients installed in many sites. Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.