lundi 7 mai 2018

Singleton database connection instance usage

I have a connection class for the database connection and i applied the singleton pattern, now i called the get instance function, but i want to access this instance in other forms, however if i do getinstance again it says connection already opened, if i close the connection in the other form i will loose data that is helpful in the form i am trying to open

 class Connection
    {
        public String ordb = "data source=orcl; user id=hr; password=hr;";
        private static Connection instance = new Connection();
        public OracleConnection connection;

        private Connection()
        {
            this.connection = new OracleConnection(ordb);
        }
        public void createConnection()
        {
            try
            {
                connection.Open();
                MessageBox.Show("Connection is Open");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        public static Connection getInstance()
        {
            return instance;
        }


Connection conn = Connection.getInstance();

I Tried creating a getter but it gives error

 public Connection SS()
        {
            return conn;
        }
//    Inconsistent accessibility: return type 'Connection' is less accessible than method 'Form2.SS()

Aucun commentaire:

Enregistrer un commentaire