Commit e6f9ff36 authored by Hanno Schlichting's avatar Hanno Schlichting

Cleanup ZApplicationWrapper.

Remove unused klass_args argument and avoid aborting transactions if
none is active. The publisher should always commit or abort the
transaction, so the Cleanup instance in `REQUEST._hold` shouldn't need
to abort anything. This gets rids of debug log messages, where each
request opens and aborts a secondary transaction.
parent d13d4640
...@@ -19,24 +19,21 @@ and used when bobo publishes a bobo_application object. ...@@ -19,24 +19,21 @@ and used when bobo publishes a bobo_application object.
import sys import sys
import transaction
if sys.version_info >= (3, ): if sys.version_info >= (3, ):
basestring = str basestring = str
connection_open_hooks = []
class ZApplicationWrapper: class ZApplicationWrapper(object):
def __init__(self, db, name, klass=None, klass_args=()): def __init__(self, db, name, klass=None):
self._stuff = db, name self._db = db
self._name = name
if klass is not None: if klass is not None:
conn = db.open() conn = db.open()
root = conn.root() root = conn.root()
if name not in root: if name not in root:
root[name] = klass() root[name] = klass()
transaction.commit() conn.transaction_manager.commit()
conn.close() conn.close()
self._klass = klass self._klass = klass
...@@ -45,12 +42,7 @@ class ZApplicationWrapper: ...@@ -45,12 +42,7 @@ class ZApplicationWrapper:
return getattr(self._klass, name) return getattr(self._klass, name)
def __bobo_traverse__(self, REQUEST=None, name=None): def __bobo_traverse__(self, REQUEST=None, name=None):
db, aname = self._stuff conn = self._db.open()
conn = db.open()
if connection_open_hooks:
for hook in connection_open_hooks:
hook(conn)
# arrange for the connection to be closed when the request goes away # arrange for the connection to be closed when the request goes away
cleanup = Cleanup(conn) cleanup = Cleanup(conn)
...@@ -58,7 +50,7 @@ class ZApplicationWrapper: ...@@ -58,7 +50,7 @@ class ZApplicationWrapper:
conn.setDebugInfo(REQUEST.environ, REQUEST.other) conn.setDebugInfo(REQUEST.environ, REQUEST.other)
v = conn.root()[aname] v = conn.root()[self._name]
if name is not None: if name is not None:
if hasattr(v, '__bobo_traverse__'): if hasattr(v, '__bobo_traverse__'):
...@@ -71,14 +63,13 @@ class ZApplicationWrapper: ...@@ -71,14 +63,13 @@ class ZApplicationWrapper:
return v return v
def __call__(self, connection=None): def __call__(self, connection=None):
db, aname = self._stuff db = self._db
if connection is None: if connection is None:
connection = db.open() connection = db.open()
elif isinstance(connection, basestring): elif isinstance(connection, basestring):
connection = db.open(connection) connection = db.open(connection)
return connection.root()[aname] return connection.root()[self._name]
class Cleanup: class Cleanup:
...@@ -86,5 +77,8 @@ class Cleanup: ...@@ -86,5 +77,8 @@ class Cleanup:
self._jar = jar self._jar = jar
def __del__(self): def __del__(self):
transaction.abort() if self._jar.transaction_manager._txn is not None:
# Only abort a transaction, if one exists. Otherwise the
# abort creates a new transaction just to abort it.
self._jar.transaction_manager.abort()
self._jar.close() self._jar.close()
...@@ -46,7 +46,7 @@ def getSchema(): ...@@ -46,7 +46,7 @@ def getSchema():
def getApp(): def getApp():
from App.ZApplication import ZApplicationWrapper from App.ZApplication import ZApplicationWrapper
DB = getConfiguration().dbtab.getDatabase('/') DB = getConfiguration().dbtab.getDatabase('/')
return ZApplicationWrapper(DB, 'Application', Application, ())() return ZApplicationWrapper(DB, 'Application', Application)()
original_config = None original_config = None
......
...@@ -135,8 +135,7 @@ def startup(): ...@@ -135,8 +135,7 @@ def startup():
# Set up the "app" object that automagically opens # Set up the "app" object that automagically opens
# connections # connections
app = App.ZApplication.ZApplicationWrapper( app = App.ZApplication.ZApplicationWrapper(
DB, 'Application', OFS.Application.Application, () DB, 'Application', OFS.Application.Application)
)
Zope2.bobo_application = app Zope2.bobo_application = app
# Initialize the app object # Initialize the app object
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment