Hey List,
I understand that SQLite is not thread safe, but it still serves a
useful function as a single file database. I normally use cherrypy with
SQLObject and MySQL, but for other reasons wanted to use SQLite as
storage. I was lead to examine the information on cherrypy and databases:
http://www.cherrypy.org/wiki/SQLObjectThreadPerConnection
and
http://www.cherrypy.org/wiki/CherryPyDatabase
However, there is a disparity between the two recipes. In the SQLObject
method this is done (modified for cherrypy 2.1):
import cherrpy
def connect(threadIndex):
# Create a connection and store it in the current thread
cherrypy.threadData.db = SQLiteConnection(blah blah)
# Tell CherryPy to call "connect" for each thread, when it starts up
cherrypy.server.onStartThreadList = [connect]
but in the SQLite method this is done:
conn = dbconnection.ConnectionHub()
class Test(SQLObject):
""" Basic table object that has one field (name) """
_connection = conn
name = StringCol()
def connect(threadIndex):
""" Function to create a connection at the start of the thread """
conn.threadConnection = SQLiteConnection('test.db')
cherrypy.server.onStartThreadList = [connect]
I modified this to be:
hub = dbconnection.ConnectionHub()
def connect (threadIndex) :
""" Function to create a connection at the start of the thread. """
hub.threadConnection = connectionForURI('sqlite:///c|db/books.db')
cherrypy.threadData.db = hub
cherrypy.server.onStartThreadList = [connect]
and when accessing the connection in cherrypy handler method:
def index (self):
# Retrieve the thread connection.
c = cherrypy.threadData.db.threadConnection
namespace = {'tests' : Test.select(connection = c)}
return super(cherryBooks, self).Page(PAGE_INDEX, namespace)
Basically I could never get it to work if I set the threadPool setting
on cherrypy to to other than 1 (it would complain about the thread not
being the same thread that created the connection). So that's fine,
single threaded is ok, but the recipes imply that using SQLObject's
ConnectionHub 'fixes' the threading problem, but it doesn't seem to.
Can someone confirm that SQLObject can only be used (with SQLite) using
a single thread? Does the connection hub fix the problem and there is
some problem with the code above?
I guess another idea would be to open the connection within each
handler. Is that perhaps a better (but slower) method, and more
problematic with mutliple access to the database.
cheers, Alex.
--
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss