r2d2_adbc
An r2d2 connection pool manager for ADBC (Arrow Database Connectivity) connections.
Overview
This crate provides a connection pool manager implementation that bridges ADBC database drivers with the r2d2 connection pooling library. It allows you to efficiently manage and reuse ADBC database connections in multi-threaded applications.
Features
- Generic ADBC Support: Works with any ADBC
Databaseimplementation - Connection Options: Configure connections with custom options
- Thread-Safe: Full support for multi-threaded connection pooling
- Type-Safe: Leverages Rust's type system for compile-time safety
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= "0.20"
Usage
Basic Usage
use AdbcConnectionManager;
// Create your ADBC database instance
let database = /* your ADBC Database implementation */;
// Create the connection manager
let manager = new;
// Create the connection pool
let pool = new?;
// Get a connection from the pool
let conn = pool.get?;
// Use the connection
let statement = conn.new_statement?;
With Connection Options
You can configure connections with options that will be applied to each new connection:
use AdbcConnectionManager;
let database = /* your ADBC Database implementation */;
// Method 1: Create with options upfront
let options = vec!;
let manager = with_options;
// Method 2: Add options after creation
let mut manager = new;
manager.add_option;
manager.add_option;
// Create the pool
let pool = builder
.max_size
.build?;
// All connections from the pool will have the configured options
let conn = pool.get?;
Configuring the Pool
The r2d2 pool itself can be configured with various parameters:
use AdbcConnectionManager;
use Duration;
let database = /* your ADBC Database implementation */;
let manager = new;
let pool = builder
.max_size // Maximum number of connections
.min_idle // Minimum idle connections
.connection_timeout
.idle_timeout
.max_lifetime
.build?;
How It Works
Connection Management
The AdbcConnectionManager implements the r2d2::ManageConnection trait:
connect(): Creates new connections usingDatabase::new_connection()orDatabase::new_connection_with_opts()if options are configuredis_valid(): Validates connections by attempting to create a statementhas_broken(): Quick check for broken connections (defers tois_valid()for ADBC)
Connection Options
Connection options are stored as string key-value pairs and automatically converted to the proper ADBC types (OptionConnection and OptionValue) when creating connections. Standard ADBC options include:
autocommit- Enable/disable autocommit modeisolation_level- Set transaction isolation levelcurrent_catalog- Set the current catalogcurrent_schema- Set the current schemaread_only- Restrict connection to read-only mode- Driver-specific options are also supported
Compatibility
- ADBC: Compatible with
adbc_core0.20.x - r2d2: Compatible with r2d2 0.8.x
- Rust: Requires Rust 2024 edition or later
ADBC Drivers
This crate works with any ADBC driver that implements the adbc_core::Database trait. Some available ADBC drivers include:
- PostgreSQL
- SQLite
- Flight SQL
- Snowflake
- And more...
Refer to the ADBC documentation for a complete list of available drivers.
Example: Complete Application
use AdbcConnectionManager;
use Error;
Error Handling
The crate provides an AdbcError type that wraps adbc_core::error::Error and implements the standard Error trait. All connection pool operations return results with this error type.
match pool.get
Thread Safety
Both AdbcConnectionManager and the resulting connection pool are fully thread-safe and can be shared across threads using Arc:
use Arc;
use thread;
let pool = new;
let mut handles = vec!;
for i in 0..10
for handle in handles
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Related Projects
- r2d2 - Generic connection pool for Rust
- Apache Arrow ADBC - Arrow Database Connectivity specification
- adbc_core - Core ADBC types and traits for Rust