필터 지우기
필터 지우기

Singleton for database connection: availability problem

조회 수: 7 (최근 30일)
Michaela
Michaela 2015년 5월 7일
편집: Nobel Mondal 2015년 5월 7일
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
Nobel Mondal
Nobel Mondal 2015년 5월 7일
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
Michaela
Michaela 2015년 5월 7일
Thanks Nobel!
This was not really the issue, as it is only a leftover from the original version of my code which I modified for this post. Seems as I have to check my code again to solve the problem.

댓글을 달려면 로그인하십시오.

답변 (1개)

Nobel Mondal
Nobel Mondal 2015년 5월 7일
편집: Nobel Mondal 2015년 5월 7일
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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by