diff options
| author | Vishvananda Ishaya <vishvananda@yahoo.com> | 2010-08-30 12:58:52 -0700 |
|---|---|---|
| committer | Vishvananda Ishaya <vishvananda@yahoo.com> | 2010-08-30 12:58:52 -0700 |
| commit | 2107ed58b1e7fccb4e1b22d4d76ebbe4b7a7bd74 (patch) | |
| tree | 00d8bdd7babff929995a4220a51b0458e2edbf94 /nova | |
| parent | fd039eef503eaa65d0b0415b56ef4c975aabdd2d (diff) | |
| parent | 7756a1d269946f72e76bae7a8015c3d72063b2c6 (diff) | |
| download | nova-2107ed58b1e7fccb4e1b22d4d76ebbe4b7a7bd74.tar.gz nova-2107ed58b1e7fccb4e1b22d4d76ebbe4b7a7bd74.tar.xz nova-2107ed58b1e7fccb4e1b22d4d76ebbe4b7a7bd74.zip | |
merged session from devin
Diffstat (limited to 'nova')
| -rw-r--r-- | nova/db/sqlalchemy/session.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/nova/db/sqlalchemy/session.py b/nova/db/sqlalchemy/session.py new file mode 100644 index 000000000..06e2ca8cd --- /dev/null +++ b/nova/db/sqlalchemy/session.py @@ -0,0 +1,53 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from sqlalchemy import create_engine +from sqlalchemy.orm import create_session +from sqlalchemy.ext.declarative import declarative_base + +from nova import flags + +FLAGS = flags.FLAGS + +def managed_session(autocommit=True): + return SessionExecutionManager(autocommit=autocommit) + + +class SessionExecutionManager: + _engine = None + _session = None + + def __init__(self, autocommit): + cls = SessionExecutionManager + if not cls._engine: + cls._engine = create_engine(FLAGS.sql_connection, echo=False) + self._session = create_session(bind=cls._engine, + autocommit=autocommit) + + + def __enter__(self): + return self._session + + def __exit__(self, type, value, traceback): + import pdb + if type or value or traceback: + pdb.set_trace() + # TODO(devcamcar): Rollback on exception. + # TODO(devcamcar): Log exceptions. + if self._session: + self._session.close()
\ No newline at end of file |
