NAME
DBIx::Class::Async::Storage::DBI - DBI-based async storage backend for DBIx::Class::Async
VERSION
Version 0.56
SYNOPSIS
# Typically obtained via the storage of an async schema
my $storage = $schema->storage;
# Async Cursor / Streaming Support
my $rs = $schema->resultset('User')->search({ active => 1 });
# Create an async cursor to iterate over results without blocking the loop
my $cursor = $storage->cursor($rs);
# Iterate through the results one by one
my $next_item;
$next_item = sub {
$cursor->next->then(sub {
my $row_data = shift;
return Future->done unless $row_data; # End of stream
say "Processing user data: " . $row_data->{name};
# Recurse to get the next item
return $next_item->();
});
};
$next_item->()->get;
# Low-Level Execution
# Execute raw SQL through the worker pool
$storage->execute_all("UPDATE users SET last_login = ?", [ time ])
->then(sub {
say "Bulk update complete.";
});
# Modern Async/Await iteration (Recommended)
use Future::AsyncAwait;
my $cursor = $schema->storage->cursor($rs);
async sub process_all_users {
while (my $row = await $cursor->next) {
say "Streaming user: " . $row->name;
}
say "All users processed.";
}
DESCRIPTION
This class provides a DBI-based storage backend for DBIx::Class::Async::Schema. It extends DBIx::Class::Async::Storage and implements DBI-specific functionality for managing database connections and operations in an asynchronous worker pool environment.
Unlike traditional DBIx::Class::Storage::DBI, this storage class does not maintain database handles in the parent process. Instead, database handles are managed by worker processes, allowing for non-blocking asynchronous database operations using Future objects.
This class is automatically instantiated when you connect to a database using "connect" in DBIx::Class::Async::Schema and generally does not need to be instantiated directly.
METHODS
dbh
my $dbh = $storage->dbh;
Returns undef in async storage mode.
Unlike traditional DBIx::Class::Storage::DBI, the async storage layer does not maintain a database handle in the parent process. Instead, database handles are held by worker processes in the background worker pool, which execute queries asynchronously.
This method exists for API compatibility with standard DBIx::Class storage objects, but always returns undef to indicate that direct database handle access is not available in async mode.
my $storage = $schema->storage;
my $dbh = $storage->dbh; # Always undef in async mode
if (!defined $dbh) {
say "Running in async mode - no direct DBH access";
}
If you need to perform database operations, use the DBIx::Class::Async::ResultSet and DBIx::Class::Async::Row methods which handle async execution transparently through the worker pool.
cursor
my $cursor = $storage->cursor($resultset);
Creates and returns a DBIx::Class::Async::Storage::DBI::Cursor object for asynchronously iterating through a ResultSet's rows.
The cursor provides a low-level interface for fetching rows one at a time using Futures, which is useful for processing large result sets without loading all rows into memory at once.
Arguments
$resultset- A DBIx::Class::Async::ResultSet object
Returns
A DBIx::Class::Async::Storage::DBI::Cursor object configured for the given ResultSet.
use Future::AsyncAwait;
my $rs = $schema->resultset('User');
my $cursor = $storage->cursor($rs);
my $iter = async sub {
while (my $row = await $cursor->next) {
say $row->name;
}
};
$iter->get;
The cursor respects the ResultSet's rows attribute for batch fetching:
my $rs = $schema->resultset('User')->search(undef, { rows => 50 });
my $cursor = $storage->cursor($rs); # Fetches 50 rows at a time
See DBIx::Class::Async::Storage::DBI::Cursor for available cursor methods.
debug
# Get current debug level
my $level = $storage->debug;
# Set debug level
$storage->debug(1);
Gets or sets the debug level for the storage layer.
When called without arguments, returns the current debug level (defaults to 0). When called with an argument, sets the debug level to the specified value and returns it.
Arguments
$level(optional) - Integer debug level (0 = off, higher values = more verbose)
Returns
The current or newly set debug level.
# Enable debugging
$storage->debug(1);
# Check if debugging is enabled
if ($storage->debug) {
say "Debug mode is on at level " . $storage->debug;
}
# Disable debugging
$storage->debug(0);
Note: The actual debug output behavior may vary depending on the storage implementation and connected database driver.
ARCHITECTURE
This storage class implements a distributed, non-blocking execution model that differs fundamentally from traditional DBIx::Class::Storage::DBI:
- No Parent Process DBH
-
The parent process never instantiates a DBI handle. All database connections are isolated within worker processes. This prevents the "forked handle" corruption common in multiprocess Perl applications and keeps the main event loop lightweight.
- Async Operations
-
Every database interaction (search, create, update, etc.) returns an IO::Async::Future object. This allows the main application to continue processing web requests, timers, or other I/O while waiting for the worker to return results.
- Worker Pool via IO::Async::Function
-
Queries are dispatched to a persistent pool of background workers. By using persistent workers with state-cached connections, the bridge eliminates the latency of
connect/disconnectcycles for every query. - Transparent API
-
The bridge provides a high degree of parity with the standard DBIC API. Advanced features like
prefetch,collapse, and complex transactions (txn_do) are supported through a specialised serialisation layer that reconstructs object graphs across process boundaries.
CAVEATS
Transaction Management
Transactions in DBIx::Class::Async differ from standard DBIx::Class because the I/O is delegated to a worker pool. You cannot use the traditional $schema->storage->txn_begin pattern in a non-blocking loop, as subsequent calls might be routed to different workers.
The Recommended Approach: txn_do
To ensure atomicity, use the txn_do or txn_batch methods. These methods bundle multiple operations into a single "Instruction Set" that is dispatched to a single worker process.
The worker executes the entire block within a local database transaction. If any step fails, the worker performs a local rollback and returns the error to the parent process.
Placeholder Resolution
The async txn_do supports internal variable registration. You can create a record in Step A and use its auto-incremented ID in Step B using the $name.id syntax. This allows for complex, multi-step dependent operations to remain fully asynchronous and atomic.
ResultSet State
Because data is deflated for transport between the worker and the parent, the objects returned by on_done are "POPO" (Plain Old Perl Objects/HashRefs) rather than "Live" DBIx::Class::Row objects. Calling methods like update or delete on these returned objects will not affect the database directly.
SEE ALSO
DBIx::Class::Async::Storage - Base async storage class
DBIx::Class::Async::Storage::DBI::Cursor - Async cursor implementation
DBIx::Class::Async::Schema - Async schema class
DBIx::Class::Async::ResultSet - Async ResultSet class
DBIx::Class::Storage::DBI - Traditional synchronous DBI storage
Future - Asynchronous result objects
IO::Async - Asynchronous event-driven programming
AUTHOR
Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>
REPOSITORY
https://github.com/manwar/DBIx-Class-Async
BUGS
Please report any bugs or feature requests through the web interface at https://github.com/manwar/DBIx-Class-Async/issues. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc DBIx::Class::Async::Storage::DBI
You can also look for information at:
BUG Report
CPAN Ratings
Search MetaCPAN
LICENSE AND COPYRIGHT
Copyright (C) 2026 Mohammad Sajid Anwar.
This program is free software; you can redistribute it and / or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License.By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you,you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement,then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.