Skip to content

Commit

Permalink
Cleanup ZApplicationWrapper.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hannosch committed Sep 8, 2016
1 parent d13d464 commit e6f9ff3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
32 changes: 13 additions & 19 deletions src/App/ZApplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,21 @@

import sys

import transaction

if sys.version_info >= (3, ):
basestring = str

connection_open_hooks = []


class ZApplicationWrapper:
class ZApplicationWrapper(object):

def __init__(self, db, name, klass=None, klass_args=()):
self._stuff = db, name
def __init__(self, db, name, klass=None):
self._db = db
self._name = name
if klass is not None:
conn = db.open()
root = conn.root()
if name not in root:
root[name] = klass()
transaction.commit()
conn.transaction_manager.commit()
conn.close()
self._klass = klass

Expand All @@ -45,20 +42,15 @@ def __getattr__(self, name):
return getattr(self._klass, name)

def __bobo_traverse__(self, REQUEST=None, name=None):
db, aname = self._stuff
conn = db.open()

if connection_open_hooks:
for hook in connection_open_hooks:
hook(conn)
conn = self._db.open()

# arrange for the connection to be closed when the request goes away
cleanup = Cleanup(conn)
REQUEST._hold(cleanup)

conn.setDebugInfo(REQUEST.environ, REQUEST.other)

v = conn.root()[aname]
v = conn.root()[self._name]

if name is not None:
if hasattr(v, '__bobo_traverse__'):
Expand All @@ -71,20 +63,22 @@ def __bobo_traverse__(self, REQUEST=None, name=None):
return v

def __call__(self, connection=None):
db, aname = self._stuff

db = self._db
if connection is None:
connection = db.open()
elif isinstance(connection, basestring):
connection = db.open(connection)

return connection.root()[aname]
return connection.root()[self._name]


class Cleanup:
def __init__(self, jar):
self._jar = jar

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()
2 changes: 1 addition & 1 deletion src/OFS/tests/testAppInitializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def getSchema():
def getApp():
from App.ZApplication import ZApplicationWrapper
DB = getConfiguration().dbtab.getDatabase('/')
return ZApplicationWrapper(DB, 'Application', Application, ())()
return ZApplicationWrapper(DB, 'Application', Application)()

original_config = None

Expand Down
3 changes: 1 addition & 2 deletions src/Zope2/App/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ def startup():
# Set up the "app" object that automagically opens
# connections
app = App.ZApplication.ZApplicationWrapper(
DB, 'Application', OFS.Application.Application, ()
)
DB, 'Application', OFS.Application.Application)
Zope2.bobo_application = app

# Initialize the app object
Expand Down

0 comments on commit e6f9ff3

Please sign in to comment.