Open valentinedwv opened 8 years ago
Digging (looking into random failures)
base:
def getSession(self):
return self._session
base:
def getSession(self):
if self._session is None:
self._session= self._session_factory.getSession()
return self._session
Then all Service classes need to use getSession(), rather than self._session
This is from the turned on test_odm2.py/test_sessionNotFailed
tests/test_odm2/test_odm2.py:93:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
odm2api/ODM2/services/createService.py:226: in createPerson
self._session.commit()
../venv_odm/lib/python2.7/site-packages/sqlalchemy/orm/session.py:801: in commit
self.transaction.commit()
../venv_odm/lib/python2.7/site-packages/sqlalchemy/orm/session.py:390: in commit
self._assert_active(prepared_ok=True)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <sqlalchemy.orm.session.SessionTransaction object at 0x104d38f10>
prepared_ok = True, rollback_ok = False, deactive_ok = False
closed_msg = 'This transaction is closed'
def _assert_active(self, prepared_ok=False,
rollback_ok=False,
deactive_ok=False,
closed_msg="This transaction is closed"):
if self._state is COMMITTED:
raise sa_exc.InvalidRequestError(
"This session is in 'committed' state; no further "
"SQL can be emitted within this transaction."
)
elif self._state is PREPARED:
if not prepared_ok:
raise sa_exc.InvalidRequestError(
"This session is in 'prepared' state; no further "
"SQL can be emitted within this transaction."
)
elif self._state is DEACTIVE:
if not deactive_ok and not rollback_ok:
if self._rollback_exception:
raise sa_exc.InvalidRequestError(
"This Session's transaction has been rolled back "
"due to a previous exception during flush."
" To begin a new transaction with this Session, "
"first issue Session.rollback()."
" Original exception was: %s"
> % self._rollback_exception
E InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (sqlite3.IntegrityError) NOT NULL constraint failed: People.PersonFirstName [SQL: u'INSERT INTO people (personfirstname, personmiddlename, personlastname) VALUES (?, ?, ?)'] [parameters: (None, 'michael', 'castronova')]
I think we will need to do a try-exception block, with session.rollback if things do not work. Or is there an object.isValid command that can be run before commiting
Adding tests based on @Castronova branch, and seeing that if a constraint is invalid, that the session becomes invalid.
Need to see check is the session is still open before attempting to create.