On Fri, Jan 29, 2010 at 8:37 AM, Alan Harris-Reid <span dir="ltr"><<a href="mailto:aharrisreid@googlemail.com">aharrisreid@googlemail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Hi,<br>
<br>
I am creating a web application (using Python 3.1 and CherryPy 3.2) where a SQLite connection and cursor object are created using the following code (simplified from the original):<br>
<br>
class MainSite:<br>
   con = sqlite.connect('MyDatabase.db')<br>
   cursor = con.cursor()<br></blockquote><div><br></div><div>This is bad. For one thing, every thread should have its own connection at least-- you can store it in thread-local storage (see the threading module) for re-use if you want, if cherrypy does thread pooling techniques or some such (I'd be sorta surprised if it didn't).</div>

<div><br></div><div>For another thing, cursors are NOT meant to be long-running interfaces.  You should not leave one open beyond one concrete action (e.g., one user request, with however many SQL statements it takes to accomplish that work) even if you are re-using the same connection across multiple methods/invocations.</div>

<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Questions...<br>
1.  Is there a large overhead in opening a new SQLite connection for each thread (ie. within each method)?<br></blockquote><div><br></div><div>You're making a web-app in Python, man. :) The overhead of a SQLite connection is not exactly significant at that point :) Though you could use thread pooling techniques with each thread having its own dedicated connection to mitigate what overhead there is.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
2.  Is there any way to use the same connection for the whole class (or should I forget that idea completely?)<br></blockquote><div><br></div><div>You shouldn't think of "connection" and "class" as having any kind of meaningful link, it just doesn't make sense. It won't work and what you're -trying- to do doesn't actually even make sense. Besides the fact that you're worrying prematurely about overhead and trying to optimize it away, connections (and from them, cursors) to databases are things that are used for binding together a bunch of statements into transactions and maintaining 'current state' of your interaction with the database: its a a logical abstraction of "what I'm doing Now to the database", whereas the controller class is, "what can be done."</div>

<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
3.  When a method returns to the calling method, is the connection automatically closed (assuming the object is local, of course) or does it have to be done explicitly using connection.close()?<br></blockquote><div><br></div>

<div>If it goes out of scope, it will be closed if its a local variable (be sure you commit if you need to first!). Assuming you aren't caching/saving the connection in the thread for future calls it handles, if you have a thread pool doing your work (again, I'd be -really- surprised if CherryPy with threads didn't involve thread pools as opposed to starting up and stopping threads continually as requests come in).</div>

<div><br></div><div>HTH,</div><div><br></div><div>--S</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
TIA,<br><font color="#888888">
Alan Harris-Reid</font><div><div></div><div class="h5"><br>
<br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br><br clear="all"><div name="mailplane_signature">--S</div>