<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-GB link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal>Hello Guys,<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Well, thanks to Andy Todd I now know this to be the correct
list to post on so thought I would start a new thread with my question.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Essentially I only have a single database attached to my
application but I’m looking for the best way to handle its connection so
thought I would come and get your thoughts and opinions.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Within my applications I’ll have many classes which access
the database, I’m wondering to what level I should extract the database
connection. <o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Should I create a new database connection and close it for
every method which calls the database? Should I create the connection to
the DB when I construct the class and place the cursor in the self scope? Or
should I create an application wide connection to the database and inject the connection
into all the classes which require it?<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>At the moment I’ve created a singleton class which
contains the connection to the database, I then pass this class into my other
objects and store it as a property in the self scope. Then, for each function
which needs to access the database I create a new cursor and then close the
cursor at the end of the method, like so:<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>class someclass:<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal> #
I’m the constructor method<o:p></o:p></p>
<p class=MsoNormal style='text-indent:36.0pt'>def __init__(self, connection):<o:p></o:p></p>
<p class=MsoNormal> #
Create the connection<o:p></o:p></p>
<p class=MsoNormal> self.connection
= connection<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal> #
Method which calls the database<o:p></o:p></p>
<p class=MsoNormal> def
mymethod(self):<o:p></o:p></p>
<p class=MsoNormal> new_cursor
= self.connection.cursor()<o:p></o:p></p>
<p class=MsoNormal style='margin-left:36.0pt;text-indent:36.0pt'>new_cursor.execute(“””INSERT
INTO......”””)<o:p></o:p></p>
<p class=MsoNormal style='margin-left:36.0pt;text-indent:36.0pt'>new_cursor.close()<o:p></o:p></p>
<p class=MsoNormal style='margin-left:36.0pt;text-indent:36.0pt'><o:p> </o:p></p>
<p class=MsoNormal>Is this the best way to handle things? I’m simply
looking for the least verbose and simple way to query the database.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Thanks for your advice and thoughts, I look forward to
seeing how you guys handle this.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Robert<o:p></o:p></p>
</div>
</body>
</html>