0

Can anyone tell me what does this thing do? Also if anyone can give an example if would be helpful.

public class ConnectionManager{
    private static ConnectionManager instance = null;
  .....}

Here is the complete code:

package com.gollahalli.main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager
{
    private static ConnectionManager instance = null;

    private final String USERNAME = "root";
    private final String PASSWORD = "root";
    private final String H_CONN_STRING = "jdbc:hsqldb:data/explorecalifornia";
    private final String M_CONN_STRING = "jdbc:mysql://localhost/explorecalifornia";

    private DBType dbType = DBType.MYSQL;

    private Connection conn = null;

    private ConnectionManager() { }

    public static ConnectionManager getInstance() {
        if (instance == null) {
            instance = new ConnectionManager();
        }
        return instance;
    }

    public void setDBType(DBType dbType) {
        this.dbType = dbType;
    }

    private boolean openConnection() {
        try {
            switch (dbType) {
            case MYSQL:
                conn = DriverManager.getConnection(M_CONN_STRING, USERNAME, PASSWORD);
                return true;

            case HSQLDB:
                conn = DriverManager.getConnection(H_CONN_STRING, USERNAME, PASSWORD);
                return true;

            default: 
                return false;
            }
        }
        catch (SQLException e) {
            System.err.println(e);
            return false;
        }
    }

    public Connection getConnection() {
        if (conn == null) {
            if (openConnection()) {
                System.out.println("Connection opened");
                return conn;
            } else {
                return null;
            }
        }
        return conn;
    }

    public void close() {
        System.out.println("Closing connection");
        try {
            conn.close();
            conn = null;
        } catch (Exception e) { }
    }
}
7
  • 4
    Seems like the beginning of Singleton pattern implementation. Commented Sep 1, 2014 at 7:13
  • Can you please show us full code ? Commented Sep 1, 2014 at 7:15
  • @sᴜʀᴇsʜᴀᴛᴛᴀ sourcepod.com/nvymqk77-38352 here is the code Commented Sep 1, 2014 at 7:25
  • @user3782963 Select All + Copy + Paste here please. Commented Sep 1, 2014 at 7:26
  • @RohitJain its a pretty big one sourcepod.com/nvymqk77-38352 have a look at this Commented Sep 1, 2014 at 7:29

3 Answers 3

2

There is the singleton design pattern.

It used to make sure that only one instance of a class can be created.

public class MySingletonClass {

    private static MySingletonClass instance;


    public synchronized static MySingletonClass getInstance() {
        if (instance == null) {
            instance = new MySingletonClass(); // "lazy" initialization
        }

        return instance;
    }


   /**
    * private constructor can be called only inside of MySingleton class, but not from    outside.
   */
   private MySingletonClass() {
       // your code here
   }
}

So, to get an instance of this class in the code, a developer does not use the constructor.

Developer uses the static method getInstance().

MySingletonClass mySingleton = MySingletonClass.getInstance();

Please be careful with singletons. Many novice developers abuse use of singletons and use them as global variables. Don't do it :)

UPDATE:

I added synchronized to the getInstance() method to make it thread safe.

Sign up to request clarification or add additional context in comments.

4 Comments

One of the simplest solutions of making it thread safe is using enum for singleton
@HamoriZ Not only one of the simplest. It's the simplest way to create a lazy-loading efficiently threadsafe singleton in Java.
@brimborium what did you change in my answer?
@RafaelOsipov You can click on the edit link to see the history, with dif's for every change. In this case, you wrote sycnrhonized and I just fixed that.
1

It simply declares a field called instance whose type is ConnectionManager and initializes it to null (which is redundant because that would be its default value anyway).

Most likely the class is a singleton class (only one instance is allowed from them) judging by the instance field declaration and by the name of the class.

2 Comments

thank you very much for the help. but what is a Singleton class?
You should really use Google sometimes. "Singleton class" first hit on Google. Singleton pattern
1

It's called the Singleton pattern.

This is used when you need only one object of a class, the singleton. It will be construct only one time and then you can access it through getInstance().

Naive implementation

public class SingletonDemo {
    //Holds the singleton
    private static SingletonDemo instance = null;

    //Overrides default constructor, not to instantiate another one.
    //Only getInstance will construct
    private SingletonDemo() { }

    //Only this method can construct a singleton, always call this one
    public static SingletonDemo getInstance() {
        if (instance == null) { //No singleton yet, create one
            instance = new SingletonDemo();
        }
        //return the singleton (created this time or not)
        return instance;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.