Singleton for database connection: availability problem
3 views (last 30 days)
Show older comments
Trying to avoid global variables I had the idea to introduce a singleton class for my database connection. The aim is to create one database connection object which is available in functions called by other functions e.g. if I create an instance of myConnection and call a function adjust() in main.m then the instance should be recognized within the function adjust().
Thanks a lot!
classdef (Sealed) MyConnection < handle
properties
db = 'test';
user = 'test';
passwd = 'test';
end
properties (SetAccess = protected, GetAccess = public)
conn = '';
isNewInstance = 1;
error = '';
end
properties (Dependent)
isConn = 0;
end
methods (Access = private)
function obj = MyConnection
end
end
methods (Static)
function singleObj = getInstance
persistent localObj
if isempty(localObj)
localObj = SdwConnection;
disp('New instance of MyConnection created.');
localObj.isNewInstance = 1;
try
localObj.conn = database(localObj.db,localObj.user,localObj.passwd);
catch ME
localObj.error = ME.message ;
end
else
disp('Old instance of MyConnection is used.');
localObj.isNewInstance = 0;
end
singleObj = localObj;
end
end
methods
function isConn = get.isConn(obj)
isConn = isconnection(obj.conn);
end
end
end
2 Comments
Nobel Mondal
on 7 May 2015
It is a great approach to use singletons for DB connections. Although, you might want to reduce the complexity of the above code a little bit. And, there is one sure mistake in there, your localObj has to be an object of the singleton class.
localObj = MyConnection; % not SdwConnection
Answers (1)
Nobel Mondal
on 7 May 2015
Edited: Nobel Mondal
on 7 May 2015
I became intrigued and scribbled something on my own. I guess you already know this stuff:
classdef (Sealed) MyDBConnection < handle
% Usage:
%
% To create an instance of the class -
% obj = MyDBConnection.getInstance
%
% To establish database connection -
% obj.DBname, obj.userName, obj.passWd should
% be assigned before this operation
% obj.getConnection
properties (SetAccess = private)
connObj;
end
properties
DBname;
userName;
passWd;
end
methods (Access = private)
function obj = MyDBConnection
end
end
methods (Static)
function singleObj = getInstance
persistent localObj
if isempty(localObj) || ~isvalid(localObj)
localObj = SingleInstance;
end
singleObj = localObj;
end
end
methods
% This would ensure that you don't need to establish...
% the connection as soon as the object is instantiated.
% Also, I would keep this method outside the static attribute...
% so that it can be overridden later for playing around.
% Like for, multiple connection for different users still ...
% with the flexibility to control from a single object.
function getConnection(obj)
if isempty(obj.connObj)
try
localObj.connObj = database(obj.DBname, obj.userName, obj.passWd);
disp('Database connection established.')
catch ME
disp(ME.message);
end
else
disp('Connection exists.');
end
end
function myConn = validConnExists(obj)
myConn = isconnection(obj.connObj);
end
% Create methods here for querying database...
% and pre-post-process data and query string.
end
end
0 Comments
See Also
Categories
Find more on Software Development Tools in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!