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

  • Home
  • SEARCH
  • 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 8253423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:52:15+00:00 2026-06-08T00:52:15+00:00

I read the code sample/documentation about caching in the wiki page . I see

  • 0

I read the code sample/documentation about caching in the wiki page. I see that callback RemovalListener can be used to do tear down etc of evicted cached objects. My question is does the library make sure that the object is not being used by any other thread before calling the provided RemovalListener. Lets consider the code example from the docs:

CacheLoader<Key, DatabaseConnection> loader = 
                                 new CacheLoader<Key, DatabaseConnection> () {
  public DatabaseConnection load(Key key) throws Exception {
    return openConnection(key);
  }
};
RemovalListener<Key, DatabaseConnection> removalListener =
                          new RemovalListener<Key, DatabaseConnection>() {
  public void onRemoval(RemovalNotification<Key, DatabaseConnection> removal) {
    DatabaseConnection conn = removal.getValue();
    conn.close(); // tear down properly
  }
};

return CacheBuilder.newBuilder()
  .expireAfterWrite(2, TimeUnit.MINUTES)
  .removalListener(removalListener)
  .build(loader);

Here the cache is configured to evict elements 2 minutes after creation (I understand that it may not be exact two minutes because eviction would be piggybacked along with user read/write calls etc.) But whatever time be it, will the library check that there is no active reference present to the object being passed to the RemovalListener? Because I may have another thread who fetched the object from the cache long back but may be still using it. In that case I cannot call close() on it from RemovalListener.

Also the documentation of RemovalNotification says that: A notification of the removal of a single entry. The key and/or value may be null if they were already garbage collected.
So according to it conn could be null in the above example. How do we tear down the conn object properly in such case? Also the above code example in such case will throw NullPointerException.

The use case I am trying to address is:

  1. The cache element need to expire after two minutes of creation.
  2. The evicted object needs to be closed, but only afte making sure no one is using them.
  • 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-08T00:52:17+00:00Added an answer on June 8, 2026 at 12:52 am

    Guava contributor here.

    My question is does the library make sure that the object is not being used by any other thread before calling the provided RemovalListener.

    No, that would be impossible for Guava to do generally — and a bad idea anyway! If the cache values were Integers, then because Integer.valueOf reuses Integer objects for integers below 128, you could never expire an entry with a value below 128. That would be bad.

    Also the documentation of RemovalNotification says that: A notification of the removal of a single entry. The key and/or value may be null if they were already garbage collected. So according to it conn could be null in the above example.

    To be clear, that’s only possible if you’re using weakKeys, weakValues, or softValues. (And, as you’ve correctly deduced, you can’t really use any of those if you need to do some teardown on the value.) If you’re only using some other form of expiration, you’ll never get a null key or value.

    In general, I don’t think a GC-based solution is going to work here. You must have a strong reference to the connection to close it properly. (Overriding finalize() might work here, but that’s really a broken thing generally.)

    Instead, my approach would be to cache references to a wrapper of some sort. Something like

     class ConnectionWrapper {
       private Connection connection;
       private int users = 0;
       private boolean expiredFromCache = false;
       public Connection acquire() { users++; return connection; }
       public void release() {
         users--;
         if (users == 0 && expiredFromCache) {
           // The cache expired this connection.
           // We're the only ones still holding on to it.
         }
       }
       synchronized void tearDown() {
         connection.tearDown();
         connection = null; // disable myself
       }
    
     }
    

    and then use a Cache<Key, ConnectionWrapper> with a RemovalListener that looks like…

     new RemovalListener<Key, ConnectionWrapper>() {
       public void onRemoval(RemovalNotification<Key, ConnectionWrapper> notification) {
         ConnectionWrapper wrapper = notification.getValue();
         if (wrapper.users == 0) {
           // do the teardown ourselves; nobody's using it
           wrapper.tearDown();
         } else {
           // it's still in use; mark it as expired from the cache
           wrapper.expiredFromCache = true;
         }
      }
    }
    

    …and then force users to use acquire() and release() appropriately.

    There’s really not going to be any way better than this approach, I think. The only way to detect that there are no other references to the connection is to use GC and weak references, but you can’t tear down a connection without a strong reference to it — which destroys the whole point. You can’t guarantee whether it’s the RemovalListener or the connection user who’ll need to tear down the connection, because what if the user takes more than two minutes to do its thing? I think this is probably the only feasible approach left.

    (Warning: the above code assumes only one thread will be doing things at a time; it’s not synchronized at all, but hopefully if you need it, then this is enough to give you an idea of how it should work.)

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

Sidebar

Related Questions

I read a couple of articles and sample code about how to solve TSP
Below i have shared my code in detail. I read documentation and everything about
If you read about cmpthese in the Perl Benchmark module's documentation, it states that
The goal of the following code sample is to read the contents of $target
I have a Sample Code where I'm trying to read data from an xml
I read this code in a library which is used to display a bitmap
When I read django code sometimes, I see in some templates load url from
I've read all the documentation about hooks , similar questions and a lot of
I just wrote some simple sample code to make sure that I had EclEmma
When my application first runs I'm using some simple code to read in some

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.