pub struct DatabaseAsync<S> { /* private fields */ }Expand description
This is an async wrapper for Database.
Transaction methods on this type are identical to Database, but they spawn
an async task and return a future to await the transaction completion.
Because of this difference, the transaction closures must be 'static.
You can easily implement DatabaseAsync manually using tokio::task::spawn_blocking,
but this wrapper is a little bit more efficient while also being runtime agnostic.
Implementations§
Source§impl<S: 'static + Send + Sync + Schema> DatabaseAsync<S>
impl<S: 'static + Send + Sync + Schema> DatabaseAsync<S>
Sourcepub fn new(db: Arc<Database<S>>) -> Self
pub fn new(db: Arc<Database<S>>) -> Self
Create an async wrapper for the Database.
The database is wrapped in an Arc as it needs to be shared with any thread executing a transaction. These threads can live longer than the future that started the transaction.
By accepting an Arc, you can keep your own clone of the Arc and use the database synchronously and asynchronously at the same time!
Sourcepub async fn transaction<R: 'static + Send>(
&self,
f: impl 'static + Send + FnOnce(&'static Transaction<S>) -> R,
) -> R
pub async fn transaction<R: 'static + Send>( &self, f: impl 'static + Send + FnOnce(&'static Transaction<S>) -> R, ) -> R
Create an immutable Transaction. Immutable transactions never need to wait on any other transactions. The transaction will be executed on a read-only snapshot of the database.
This function will panic if the schema was modified compared to when the Database value was created. This can happen for example by running another instance of your program with additional migrations.
Note that many systems have a limit on the number of file descriptors that can exist in a single process. On my machine the soft limit is (1024) by default. If this limit is reached, it may cause a panic in this method.
Sourcepub async fn transaction_mut<O: 'static + Send, E: 'static + Send>(
&self,
f: impl 'static + Send + FnOnce(&'static mut Transaction<S>) -> Result<O, E>,
) -> Result<O, E>
pub async fn transaction_mut<O: 'static + Send, E: 'static + Send>( &self, f: impl 'static + Send + FnOnce(&'static mut Transaction<S>) -> Result<O, E>, ) -> Result<O, E>
Create a mutable Transaction. This operation needs to wait for all other mutable Transactions for this database to be finished. There is currently no timeout on this operation, so it will wait indefinitly if required.
Whether the transaction is commited depends on the result of the closure. The transaction is only commited if the closure return Ok. In the case that it returns Err or when the closure panics, a rollback is performed.
This function will panic if the schema was modified compared to when the Database value was created. This can happen for example by running another instance of your program with additional migrations.
Note that many systems have a limit on the number of file descriptors that can exist in a single process. On my machine the soft limit is (1024) by default. If this limit is reached, it may cause a panic in this method.
Sourcepub async fn transaction_mut_ok<R: 'static + Send>(
&self,
f: impl 'static + Send + FnOnce(&'static mut Transaction<S>) -> R,
) -> R
pub async fn transaction_mut_ok<R: 'static + Send>( &self, f: impl 'static + Send + FnOnce(&'static mut Transaction<S>) -> R, ) -> R
Same as Self::transaction_mut, but always commits the transaction.
The only exception is that if the closure panics, a rollback is performed.