| Crates.io | r2d2_adbc |
| lib.rs | r2d2_adbc |
| version | 0.2.0 |
| created_at | 2025-11-08 18:45:49.237219+00 |
| updated_at | 2025-12-02 15:26:01.8752+00 |
| description | An ADBC connection pool implementation using r2d2. |
| homepage | |
| repository | https://github.com/adbc-drivers/r2d2-adbc |
| max_upload_size | |
| id | 1923157 |
| size | 46,550 |
An r2d2 connection pool manager for ADBC (Arrow Database Connectivity) connections.
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.
Database implementationAdd this to your Cargo.toml:
[dependencies]
r2d2_adbc = "0.1"
adbc_core = "0.20"
use r2d2_adbc::AdbcConnectionManager;
// Create your ADBC database instance
let database = /* your ADBC Database implementation */;
// Create the connection manager
let manager = AdbcConnectionManager::new(database);
// Create the connection pool
let pool = r2d2::Pool::new(manager)?;
// Get a connection from the pool
let conn = pool.get()?;
// Use the connection
let statement = conn.new_statement()?;
You can configure connections with options that will be applied to each new connection:
use r2d2_adbc::AdbcConnectionManager;
let database = /* your ADBC Database implementation */;
// Method 1: Create with options upfront
let options = vec![
("autocommit".to_string(), "true".to_string()),
("isolation_level".to_string(), "read_committed".to_string()),
("timeout".to_string(), "30".to_string()),
];
let manager = AdbcConnectionManager::with_options(database, options);
// Method 2: Add options after creation
let mut manager = AdbcConnectionManager::new(database);
manager.add_option("autocommit", "true");
manager.add_option("read_only", "false");
// Create the pool
let pool = r2d2::Pool::builder()
.max_size(15)
.build(manager)?;
// All connections from the pool will have the configured options
let conn = pool.get()?;
The r2d2 pool itself can be configured with various parameters:
use r2d2_adbc::AdbcConnectionManager;
use std::time::Duration;
let database = /* your ADBC Database implementation */;
let manager = AdbcConnectionManager::new(database);
let pool = r2d2::Pool::builder()
.max_size(20) // Maximum number of connections
.min_idle(Some(5)) // Minimum idle connections
.connection_timeout(Duration::from_secs(30))
.idle_timeout(Some(Duration::from_secs(600)))
.max_lifetime(Some(Duration::from_secs(1800)))
.build(manager)?;
The AdbcConnectionManager implements the r2d2::ManageConnection trait:
connect(): Creates new connections using Database::new_connection() or Database::new_connection_with_opts() if options are configuredis_valid(): Validates connections by attempting to create a statementhas_broken(): Quick check for broken connections (defers to is_valid() for ADBC)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 modeadbc_core 0.20.xThis crate works with any ADBC driver that implements the adbc_core::Database trait. Some available ADBC drivers include:
Refer to the ADBC documentation for a complete list of available drivers.
use r2d2_adbc::AdbcConnectionManager;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
// Initialize your ADBC database
let database = /* initialize your ADBC database */;
// Create the connection manager with options
let mut manager = AdbcConnectionManager::new(database);
manager.add_option("autocommit", "true");
// Build the connection pool
let pool = r2d2::Pool::builder()
.max_size(10)
.build(manager)?;
// Use connections from the pool
for i in 0..5 {
let conn = pool.get()?;
println!("Got connection {}", i);
// Use the connection
let mut stmt = conn.new_statement()?;
// Execute queries...
}
Ok(())
}
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() {
Ok(conn) => {
// Use connection
}
Err(e) => {
eprintln!("Failed to get connection: {}", e);
if let Some(source) = e.source() {
eprintln!("Caused by: {}", source);
}
}
}
Both AdbcConnectionManager and the resulting connection pool are fully thread-safe and can be shared across threads using Arc:
use std::sync::Arc;
use std::thread;
let pool = Arc::new(r2d2::Pool::new(manager)?);
let mut handles = vec![];
for i in 0..10 {
let pool = Arc::clone(&pool);
let handle = thread::spawn(move || {
let conn = pool.get().unwrap();
// Use connection in this thread
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
Contributions are welcome! Please feel free to submit a Pull Request.
Licensed under the Apache License, Version 2.0. See LICENSE for details.