本文整理汇总了Python中sqlalchemy.exc.DBAPIError方法的典型用法代码示例。如果您正苦于以下问题:Python exc.DBAPIError方法的具体用法?Python exc.DBAPIError怎么用?Python exc.DBAPIError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.exc
的用法示例。
在下文中一共展示了exc.DBAPIError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_session
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def validate_session():
""" Validate ORM Session """
worker_precheck = conf.getboolean('core', 'worker_precheck', fallback=False)
if not worker_precheck:
return True
else:
check_session = sessionmaker(bind=engine)
session = check_session()
try:
session.execute("select 1") # pylint: disable=no-member
conn_status = True
except exc.DBAPIError as err:
log.error(err)
conn_status = False
session.close() # pylint: disable=no-member
return conn_status
示例2: get_logbook
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def get_logbook(self, book_uuid, lazy=False):
try:
logbooks = self._tables.logbooks
with contextlib.closing(self._engine.connect()) as conn:
q = (sql.select([logbooks]).
where(logbooks.c.uuid == book_uuid))
row = conn.execute(q).first()
if not row:
raise exc.NotFound("No logbook found with"
" uuid '%s'" % book_uuid)
book = self._converter.convert_book(row)
if not lazy:
self._converter.populate_book(conn, book)
return book
except sa_exc.DBAPIError:
exc.raise_with_cause(exc.StorageFailure,
"Failed getting logbook '%s'" % book_uuid)
示例3: filters
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def filters(dbname, exception_type, regex):
"""Mark a function as receiving a filtered exception.
:param dbname: string database name, e.g. 'mysql'
:param exception_type: a SQLAlchemy database exception class, which
extends from :class:`sqlalchemy.exc.DBAPIError`.
:param regex: a string, or a tuple of strings, that will be processed
as matching regular expressions.
"""
def _receive(fn):
_registry[dbname][exception_type].extend(
(fn, re.compile(reg))
for reg in
((regex,) if not isinstance(regex, tuple) else regex)
)
return fn
return _receive
# NOTE(zzzeek) - for Postgresql, catch both OperationalError, as the
# actual error is
# psycopg2.extensions.TransactionRollbackError(OperationalError),
# as well as sqlalchemy.exc.DBAPIError, as SQLAlchemy will reraise it
# as this until issue #3075 is fixed.
示例4: test_tostring_with_newlines
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_tostring_with_newlines(self):
try:
raise sa_exceptions.DBAPIError.instance(
"this is a message\nthis is the next line\nthe last line",
None,
OperationalError(),
DatabaseError,
)
except sa_exceptions.DBAPIError as exc:
eq_(
str(exc),
"(test.base.test_except.OperationalError) \n"
"[SQL: this is a message\nthis is the next line\n"
"the last line]\n"
"(Background on this error at: http://sqlalche.me/e/%s/e3q8)"
% sa_exceptions._version_token,
)
示例5: test_statement_error_w_code
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_statement_error_w_code(self):
try:
raise sa_exceptions.DBAPIError.instance(
"select * from table",
[{"x": 1}],
sa_exceptions.InvalidRequestError("hello", code="abcd"),
DatabaseError,
)
except sa_exceptions.StatementError as err:
eq_(
str(err),
"(sqlalchemy.exc.InvalidRequestError) hello\n"
"[SQL: select * from table]\n"
"[parameters: [{'x': 1}]]\n"
"(Background on this error at: http://sqlalche.me/e/%s/abcd)"
% sa_exceptions._version_token,
)
eq_(err.args, ("(sqlalchemy.exc.InvalidRequestError) hello",))
示例6: test_conn_reusable
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_conn_reusable(self):
conn = self.db.connect()
conn.execute(select([1]))
eq_(self.dbapi.connect.mock_calls, [self.mock_connect])
self.dbapi.shutdown()
assert_raises(tsa.exc.DBAPIError, conn.execute, select([1]))
assert not conn.closed
assert conn.invalidated
eq_([c.close.mock_calls for c in self.dbapi.connections], [[call()]])
# test reconnects
conn.execute(select([1]))
assert not conn.invalidated
eq_(
[c.close.mock_calls for c in self.dbapi.connections],
[[call()], []],
)
示例7: test_invalidated_close
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_invalidated_close(self):
conn = self.db.connect()
self.dbapi.shutdown()
assert_raises(tsa.exc.DBAPIError, conn.execute, select([1]))
conn.close()
assert conn.closed
assert not conn.invalidated
assert_raises_message(
tsa.exc.ResourceClosedError,
"This Connection is closed",
conn.execute,
select([1]),
)
示例8: test_invalidate_conn_w_contextmanager_disconnect
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_invalidate_conn_w_contextmanager_disconnect(self):
# test [ticket:3803] change maintains old behavior
pool = self.db.pool
conn = self.db.connect()
self.dbapi.shutdown("execute")
def go():
with conn.begin():
conn.execute(select([1]))
assert_raises(exc.DBAPIError, go) # wraps a MockDisconnect
assert conn.invalidated
ne_(pool._invalidate_time, 0) # pool is invalidated
conn.execute(select([1]))
assert not conn.invalidated
示例9: test_savepoint_rollback_fails_flat
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_savepoint_rollback_fails_flat(self, local_connection):
connection = local_connection
t1 = connection.begin()
s1 = connection.begin_nested()
# force the "commit" of the savepoint that occurs
# when the "with" block fails, e.g.
# the RELEASE, to fail, because the savepoint is already
# released.
connection.dialect.do_release_savepoint(connection, s1._savepoint)
assert_raises_message(
exc.DBAPIError, r".*SQL\:.*ROLLBACK TO SAVEPOINT", s1.rollback
)
assert not s1.is_active
with testing.expect_warnings("nested transaction already"):
s1.rollback() # no error (though it warns)
t1.commit() # no error
示例10: test_savepoint_release_fails_flat
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_savepoint_release_fails_flat(self):
with testing.db.connect() as connection:
t1 = connection.begin()
s1 = connection.begin_nested()
# force the "commit" of the savepoint that occurs
# when the "with" block fails, e.g.
# the RELEASE, to fail, because the savepoint is already
# released.
connection.dialect.do_release_savepoint(connection, s1._savepoint)
assert_raises_message(
exc.DBAPIError, r".*SQL\:.*RELEASE SAVEPOINT", s1.commit
)
assert not s1.is_active
s1.rollback() # no error. prior to 1.4 this would try to rollback
t1.commit() # no error
示例11: test_empty_insert_pk2_fv
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_empty_insert_pk2_fv(self):
assert_raises(
exc.DBAPIError,
self._test_empty_insert,
Table(
"b",
MetaData(testing.db),
Column(
"x",
Integer,
primary_key=True,
server_default=FetchedValue(),
),
Column(
"y",
Integer,
primary_key=True,
server_default=FetchedValue(),
),
),
)
示例12: test_empty_insert_pk3_fv
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_empty_insert_pk3_fv(self):
assert_raises(
exc.DBAPIError,
self._test_empty_insert,
Table(
"c",
MetaData(testing.db),
Column(
"x",
Integer,
primary_key=True,
server_default=FetchedValue(),
),
Column("y", Integer, DefaultClause("123"), primary_key=True),
),
)
示例13: test_pyodbc_version_fallback
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_pyodbc_version_fallback(self):
dialect = pyodbc.MSDialect_pyodbc()
dialect.dbapi = Mock()
for vers, expected in [
("11.0.9216.62", (11, 0, 9216, 62)),
("notsqlserver.11.foo.0.9216.BAR.62", (11, 0, 9216, 62)),
("Not SQL Server Version 10.5", (5,)),
]:
conn = Mock(
exec_driver_sql=Mock(
return_value=Mock(
scalar=Mock(
side_effect=exc.DBAPIError("stmt", "params", None)
)
)
),
connection=Mock(getinfo=Mock(return_value=vers)),
)
eq_(dialect._get_server_version_info(conn), expected)
示例14: test_standalone_orphans
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_standalone_orphans(self):
if self.redefine_colprop:
person_attribute_name = "person_name"
else:
person_attribute_name = "name"
session = Session()
daboss = Boss(
status="BBB",
manager_name="boss",
golf_swing="fore",
**{person_attribute_name: "daboss"}
)
session.add(daboss)
assert_raises(sa_exc.DBAPIError, session.flush)
示例15: test_report_primary_error_when_rollback_fails
# 需要导入模块: from sqlalchemy import exc [as 别名]
# 或者: from sqlalchemy.exc import DBAPIError [as 别名]
def test_report_primary_error_when_rollback_fails(self):
User, users = self.classes.User, self.tables.users
mapper(User, users)
session = Session(testing.db)
with expect_warnings(".*during handling of a previous exception.*"):
session.begin_nested()
savepoint = session.connection()._nested_transaction._savepoint
# force the savepoint to disappear
session.connection().dialect.do_release_savepoint(
session.connection(), savepoint
)
# now do a broken flush
session.add_all([User(id=1), User(id=1)])
assert_raises_message(
sa_exc.DBAPIError, "ROLLBACK TO SAVEPOINT ", session.flush
)