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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:33:20+00:00 2026-05-26T04:33:20+00:00

I have a singleton class in my play app. This singleton class is a

  • 0

I have a singleton class in my play app. This singleton class is a long process which will generate reports from DB which consumes huge amount of memory. When i run my application in dev mode this singleton functionality is executing several times. I want this functionality to run only once. What should I do for that?

My code is:

public class DataGridManagerImpl extends ComponentContainer implements DataGridManager {

private static DataGridManager instance = null;

    private DataGridManagerImpl(){
        load();
    }}

@Override
    public void load() {
//Myreports function
}

public static DataGridManager getInstance(){

          if (instance == null){
             instance = new DataGridServiceManagerImpl();
          }

        return instance;
    }
}

In my controller file inside a template function

DataGridManager dataGridMgr = DataGridManagerImpl.getInstance();

If i access the page it is executing the load reports function again.

  • 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-05-26T04:33:20+00:00Added an answer on May 26, 2026 at 4:33 am

    Without code explaining how did you create your class it’s hard to answer. From what I understand what you want is to run a process only once.

    Problably the best approach is to use a Scheduled Job. This will trigger the process at a certain time, and Play ensures that only 1 instance of this process is running at the same time, even if the schedule would indicate another instance has to run. Let’s say you have a process scheduled every hour and the process takes 3 hours. The initial process will be the only one running for 3 hours until it finishes.

    Now, I would assume you want your process to be recurring as it generate reports. If not, if you only want to run it once, then you may want to use an asynchronous bootstrap job instead. This would run just once, at the beginning of the application.

    EDIT on update: during development the @OnApplicationStart may execute several times, as Play may automatically reload the application when you do certain code changes. This is part of the dev process (the same that an @OnApplicationStart job won’t start in Dev until the server gets a request).

    As it’s a job that you only want to run once, you may try to skip it in dev mode using the check:

    if(Play.mode == Play.Mode.DEV)

    If you need to run it at least once, add a dev-only url that you can access during dev to start the process.

    Now, on your update you also mention that you are calling that code in a controller, and that every time the controller is acessed the method is called. That’s expected. Singleton doesn’t mean that it will run only once, but that there is only 1 object in the system. If in your controller you launch the calculation, that will happen everytime you access the controller.

    SECOND EDIT (on comments): Arasu, the other issue is that you are calling the method load() when you construct the object. A singleton doesn’t garantee that the object will only be constructed once. It garantees that, once constructed, only 1 object will exist. But it may happen that the object is removed by GC, in this case as per your code if you construct it again then you’ll call load() and redo the processing.

    The best solution is to not call “load” on constructor, but to force the user (you) to call it after retrieving the instance. An alternative is to set some flag at the beginning of load that detects if the code has been run. Be aware that Play is stateless, so that flag will need to be stored in the database.

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

Sidebar

Related Questions

I have some class which uses boost singleton. It calls some function from own
I have a singleton class for global access to config information. This singleton class
I have a singleton class, and I will compile it as a library static(lib)
I have a singleton class called SingletonController1. This SingletonController1 instantiates a bunch of others
I have a singleton class that inherits from sprite so that it can access
i have simple singleton that sets pdo object from factory class , in the
I have a singleton parent class extended by a child class which is not
I have a singleton class with this code: manager.h @interface Manager : NSObject {
I have a Singleton class which is highly dependent on the XML file it
I have a Singleton Class SharedDataObject which has a another class object myClass. MyClass

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.