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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:57:32+00:00 2026-05-26T14:57:32+00:00

I like to use a Singleton as a globally accessible cache for certain database

  • 0

I like to use a Singleton as a globally accessible cache for certain database values. This is my definition of the singleton with sample variables gi1, and gDBAdapter:

public class GlobalVars {
    private Integer gi1;
    private WebiDBAdapter gDBAdapter;

    private static GlobalVars instance = null;
       protected GlobalVars() {
          // Exists only to defeat instantiation.
       }
       public static GlobalVars getInstance() {
          if(instance == null) {
             instance = new GlobalVars();
          }

          //  now get the current data from the DB, 

          WebiDBAdapter myDBAdapter = new WebiDBAdapter(this);  // <- cannot use this in static context

          gDBAdapter = myDBAdapter;     // <- cannot use this in static context
          myDBAdapter.open();

          Cursor cursor = myDBAdapter.fetchMainEntry();
          startManagingCursor(cursor); // <- the method startManagingCursor is undefined for the type GlobalVars
          // if there is no DB yet, lets just create one with default data
            if (cursor.getCount() == 0) {
                cursor.close();
                createData();  // <- the method createData is undefined for the type GlobalVars
                cursor = myDBAdapter.fetchMainEntry();
                startManagingCursor(cursor);// <- the method startManagingCursor is undefined for the type GlobalVars
            }

            gi1 = cursor.getInt(cursor  // <- Cannot make a static reference to the non-static field gi1
                    .getColumnIndexOrThrow(WebiDBAdapter.KEY1));
            if (gi1 == null) gi1 = 0;

            cursor.close();

          return instance;
       }

But all the references to the instance vars are not recognized.

All the // <- entries show the errors I get

I guess there is some basics that I am not doing right here.

What would be the right way to create the singleton and initialize its values with the values from the database?

Thanks for your help!

  • 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-26T14:57:33+00:00Added an answer on May 26, 2026 at 2:57 pm

    Use the Application class,

    also there is no such thing as this in a static function

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

Sidebar

Related Questions

I'd use a singleton like this: Singleton* single = Singleton::instance(); single->do_it(); I'd use an
This is a slight variance of this question: Possible to use a singleton with
We have a wrapper around a suds (SOAP) request, that we use like this
I'd like to use the stackable trait pattern with singleton objects, but i can't
Assume you have a Singleton Constants class, instance of which you'd like to use
I am using AdoNetAppender (SQL server) in my asp.net application and would like use
I like to use static functions in C++ as a way to categorize them,
I like to use Emacs' shell mode, but it has a few deficiencies. One
I like to use IPython's zope profile to inspect my Plone instance, but a
Most websites use - (like Stack Overflow) but most PHP frameworks generate + encoded

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.