Connection

Trait Connection 

Source
pub trait Connection: Executor {
    // Required methods
    fn connect(
        url: Cow<'static, str>,
    ) -> impl Future<Output = Result<<Self::Driver as Driver>::Connection>>
       where Self: Sized;
    fn begin(
        &mut self,
    ) -> impl Future<Output = Result<<Self::Driver as Driver>::Transaction<'_>>>;

    // Provided methods
    fn sanitize_url(url: Cow<'static, str>) -> Result<Url>
       where Self: Sized { ... }
    fn disconnect(self) -> impl Future<Output = Result<()>>
       where Self: Sized { ... }
}
Expand description

A live database handle capable of executing queries and spawning transactions.

This trait extends Executor adding functionality to acquire a connection and to begin transactional scopes.

Drivers implement concrete Connection types to expose backend-specific behavior (timeouts, pooling strategies, prepared statement caching, etc.).

§Lifecycle

  • connect creates (or fetches) an underlying connection. It may eagerly establish network I/O for validation; always await it.
  • begin starts a transaction returning an object implementing [Transaction]. Commit / rollback MUST be awaited to guarantee resource release.

Required Methods§

Source

fn connect( url: Cow<'static, str>, ) -> impl Future<Output = Result<<Self::Driver as Driver>::Connection>>
where Self: Sized,

Create a connection (or pool) with at least one underlying session established to the given URL.

The returned future must be awaited to obtain the connection object (type Self::Driver::Connection). Implementations may perform I/O or validation during connect; callers should treat this as a potentially expensive operation.

Source

fn begin( &mut self, ) -> impl Future<Output = Result<<Self::Driver as Driver>::Transaction<'_>>>

Begin a transaction scope tied to the current connection.

The returned value implements [Transaction] for the underlying driver. commit / rollback MUST be awaited to ensure resources are released and the scope is finalized.

Provided Methods§

Source

fn sanitize_url(url: Cow<'static, str>) -> Result<Url>
where Self: Sized,

Source

fn disconnect(self) -> impl Future<Output = Result<()>>
where Self: Sized,

Disconnect and release the underlying session(s).

Default implementation is a no-op; drivers may override to close sockets or return the connection to a pool asynchronously.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§