From 7756a1d269946f72e76bae7a8015c3d72063b2c6 Mon Sep 17 00:00:00 2001 From: Devin Carlen Date: Mon, 30 Aug 2010 12:49:31 -0700 Subject: Added session.py --- nova/db/sqlalchemy/session.py | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 nova/db/sqlalchemy/session.py (limited to 'nova') 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 -- cgit