#[non_exhaustive]pub struct AnyDatabase { /* private fields */ }Expand description
持有单个关系数据库连接池的运行时容器。
使用 #[non_exhaustive]:内部后端集合随 feature 变化,调用方不得按结构体字面量
构造,必须通过 AnyDatabase::mysql / AnyDatabase::postgres 构造(按 feature 启用)。
Implementations§
Source§impl AnyDatabase
impl AnyDatabase
Sourcepub fn id(&self) -> &DatasourceId
pub fn id(&self) -> &DatasourceId
返回逻辑数据源名。
Sourcepub fn backend(&self) -> BackendKind
pub fn backend(&self) -> BackendKind
返回后端类型。
Sourcepub fn capabilities(&self) -> &'static BackendCapabilities
pub fn capabilities(&self) -> &'static BackendCapabilities
返回后端静态能力契约。
Sourcepub fn require_capabilities(
&self,
required: &[BackendCapability],
) -> Result<(), BaseError>
pub fn require_capabilities( &self, required: &[BackendCapability], ) -> Result<(), BaseError>
启动期能力门禁:校验后端是否支持全部 required 能力。
缺失任一能力时返回 BaseError::UnsupportedCapability,错误消息包含后端
类型与缺失能力名。调用方应在启动/初始化时一次性校验自己实际用到的能力集合,
而不是把检查散落到每个查询上——这样配置错误在启动期就 fail-fast,而不是执行
到方言专属语法时才暴露。
§示例
database.require_capabilities(&[
BackendCapability::Select,
BackendCapability::Savepoint,
])?;Sourcepub async fn health_check(&self) -> Result<bool, DbError>
pub async fn health_check(&self) -> Result<bool, DbError>
健康检查:执行 SELECT 1 验证连接可用。
Sourcepub fn pool_status(&self) -> PoolStatus
pub fn pool_status(&self) -> PoolStatus
返回连接池状态快照。
Sourcepub fn as_postgres(&self) -> Option<&Database>
pub fn as_postgres(&self) -> Option<&Database>
当容器持有 PostgreSQL 后端时返回其连接池引用。
Sourcepub fn pool(&self) -> AnyPool
pub fn pool(&self) -> AnyPool
返回与当前后端共享同一连接池的可克隆句柄。
sqlx 连接池内部基于 Arc,克隆只是引用计数递增;本方法返回的 AnyPool
可在请求作用域内自由复制,不会复制物理连接。
Sourcepub fn source_pool(&self) -> PoolHandle
pub fn source_pool(&self) -> PoolHandle
返回带逻辑数据源身份的连接池句柄,供 TableQuery 做事务同源校验。
Sourcepub async fn begin(&self) -> Result<AnyTransaction, DbError>
pub async fn begin(&self) -> Result<AnyTransaction, DbError>
在当前后端上开启事务。
返回的 AnyTransaction 可传入 TableQuery 的 *_in_tx 系列方法,也可通过
AnyTransaction::commit / AnyTransaction::rollback 显式结束。未显式结束时
由 sqlx 在 drop 时尽力回滚。
Sourcepub async fn retry_transaction<T, F, Fut>(
&self,
max_attempts: u32,
backoff: Duration,
operation: F,
) -> Result<T, DbError>
pub async fn retry_transaction<T, F, Fut>( &self, max_attempts: u32, backoff: Duration, operation: F, ) -> Result<T, DbError>
整事务重试:按次数与指数退避重跑 operation,只重试序列化失败与死锁。
仅 PostgreSQL 的 40001(序列化失败)与 40P01(死锁)会重试,判定复用
SqlErrorKind 的 SQLSTATE 归一,与 yang-db 的 PgDatabase::retry_transaction
口径一致。其他错误立即原样返回——包括约束冲突(如 23505)、
DbError::ConnectionError 与 SQLSTATE 08/53 类瞬时连接错误。提交阶段
断连时事务是否已生效未知,重试可能重复执行副作用,因此不属于可重试范围。
operation 每次被调用都应自行开启并结束一个事务(例如 self.begin() →
业务语句 → commit())。重试前 helper 按 backoff * 2^(n-1) 退避(最多放大
32 倍);失败事务的回滚由被丢弃的事务句柄兜底。
§示例
let value = database
.retry_transaction(3, Duration::from_millis(50), || async {
let mut tx = database.begin().await?;
// ... 业务语句 ...
tx.commit().await?;
Ok(())
})
.await?;Trait Implementations§
Source§impl Clone for AnyDatabase
impl Clone for AnyDatabase
Auto Trait Implementations§
impl !Freeze for AnyDatabase
impl !RefUnwindSafe for AnyDatabase
impl !UnwindSafe for AnyDatabase
impl Forget for AnyDatabase
impl Move for AnyDatabase
impl Send for AnyDatabase
impl Sync for AnyDatabase
impl Unpin for AnyDatabase
impl UnsafeUnpin for AnyDatabase
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more