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 9307125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:19:31+00:00 2026-06-19T00:19:31+00:00

I’m running a RESTful java backend on GlassFish. Attached to that is an HTML5

  • 0

I’m running a RESTful java backend on GlassFish. Attached to that is an HTML5 / JS frontend which I can put in a webapp project (and then include the backend as a dependency), or run on an IIS webserver on a different location. CORS is not an issue. Whatever solves this following problem:

Situation:

  1. User1 logs on and database path is set to ‘db/user1/’
  2. User1 inserts ‘Value 1’ into the database
  3. User2 logs on and database path is set to ‘db/user2/’
  4. User1 tries to delete ‘Value 1’ from database

User1 would not be able to delete Value 1 from db/user1, since the databasepath has been changed to db/user2 and there is no Value 1 in that database.

public class DataAccess{
    private static DataAccess dataaccess;
    private String databasepath;

    public static DataAccess getInstance() {
        if (dataaccess == null) {
            dataaccess = new DataAccess();
        }
    }
}

How can I modify the getInstance() method so that it acts as a singleton, but only inside the thread of that user? I saw something called threadlocal but didn’t fully understand it, is that perhaps a solution?

Any help is most certainly appreciated.

  • 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-19T00:19:32+00:00Added an answer on June 19, 2026 at 12:19 am

    You could use the ThreadLocal class in a factory pattern:

    public class DataAccess{
        private static ThreadLocal<DataAccess> THREAD_LOCAL = new ThreadLocal() {
         @Override
         protected DataAccess initialValue() {
                 return new DataAccess();
         }
        };
        private String databasepath;
    
        public static DataAccess getInstance() {
          return THREAD_LOCAL.get();
        }
    }
    

    This will, however, cause a memory leak. So you need to use a Servlet Filter to set the value at the start of a request and then delete it at the end, some like:

       public void doFilter(ServletRequest request,
          ServletResponse response, FilterChain chain) 
          throws IOException, ServletException {
          DataAccess.set(new DataAccess("SomeValue"));
          try {
            chain.doFilter(request, response);
          } finally {
            DataAcess.remove();
          }
       }
    

    Once you have a class that implements Filter you add it to your web.xml thus:

    <!--Filter for adding in dataccess objects-->
    <filter>
        <filter-name>DataccessFilter</filter-name>
        <filter-class>my.package.DataccessFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>DataccessFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    This page gives an example of a filter and its mappings.
    And your DataAccess would look like:

    public class DataAccess{
        private static ThreadLocal<DataAccess> THREAD_LOCAL = new ThreadLocal();
        private String databasepath;
    
        public DataAcess(final String databasepath) {
          this.databasepath = databasepath;
        }
    
        public static DataAccess getInstance() {
          return THREAD_LOCAL.get();
        }
        public static void set(final DataAccess dataAccess) {
          THREAD_LOCAL.set(dataAccess);
        }
        public static void remove() {
          THREAD_LOCAL.remove();
        }
    }
    

    Be extremely careful with ThreadLocal as it is probably the number one cause of memory leaks in Java. With web servers that have thread pools you can get even more serious bugs if you don’t clean them out properly.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

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.