outersky / r2d2-mysql Goto Github PK
View Code? Open in Web Editor NEWMySQL support for the r2d2 connection pool (Rust)
License: MIT License
MySQL support for the r2d2 connection pool (Rust)
License: MIT License
Currently mysql is on 11.1.2, this being pinned to 8.0.0 can cause dependency conflicts.
In the document about r2d2::ManageConnection.has_broken:
This will be called synchronously every time a connection is returned to the pool, so it should not block.
However r2d2_mysql::MysqlConnectionManager.has_broken
may block when the network between the client and the server is in trouble.
For example, an implementation might check if the underlying TCP socket has disconnected. Implementations that do not support this kind of fast health check may simply return
false
.
Rust-mysql-simple doesn't support such kind of fast health check, so it may be better to simply return false
.
Please update mysql version to 1.2
There are two reasons I would argue that is should be of type Opts
Edit: I'll have to revoke argument 2 since Conn::new() wants ownership.
Hi,
I'm pretty new to RUST, but I'm not understanding something.
I've got a shared Pool which I initialize using a trait so that we can switch easly to MySQL or SQLite3 management.
When using MySQL, I create a tokio spawn, in which it waits for a certain amount of time, to execute a Insert batch.
When doing this insert batch, it seems like it's literally locking and freezing the rest of the application until it's done.
What am I doing wrong here, do I need to use a different method to make sure the rest of the application keeps on going while a tokio spawn is doing it's work ?
Explanation is appreciated, or examples how to avoid this.
Kind regards.
can see this example project.
https://github.com/damody/r2d2mysql
always get time out
https://github.com/damody/r2d2mysql/blob/81e94cd757eea1c15f0342f7bd75593b32b5a0b9/src/main.rs#L71
and get error
Commands out of sync; you can\'t run this command now
Looking at the Cargo.toml
, the repo marks this crate as version 9.0.0
, yet the version that's known to Crates.io is only version 8.0.0
. So any of the latest fixes (including my PR, #10) aren't available via the standard Cargo.toml
inclusion.
I'd like for you to upload a new version of the crate to Crates.io (cargo publish
).
Thanks!
error: failed to run custom build command for miniz-sys v0.1.11
process didn't exit successfully: D:\Rust\Rd2dTest\target\debug\build\miniz-sys-d13c924666e24cd6\build-script-build (exit code: 101)
--- stdout
TARGET = Some("x86_64-pc-windows-gnu")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-pc-windows-gnu")
CC_x86_64-pc-windows-gnu = None
CC_x86_64_pc_windows_gnu = None
HOST_CC = None
CC = None
CFLAGS_x86_64-pc-windows-gnu = None
CFLAGS_x86_64_pc_windows_gnu = None
HOST_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
CARGO_CFG_TARGET_FEATURE = Some("fxsr,mmx,sse,sse2")
running: "gcc.exe" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-m64" "-DMINIZ_NO_STDIO" "-DMINIZ_NO_ARCHIVE_APIS" "-DMINIZ_NO_ARCHIVE_WRITING_APIS" "-DMINIZ_NO_TIME" "-DMINIZ_NO_ZLIB_COMPATIBLE_NAMES" "-o" "D:\Rust\Rd2dTest\target\debug\build\miniz-sys-935e563594ae4ae2\out\miniz.o" "-c" "miniz.c"
cargo:warning=cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
exit code: 1
--- stderr
thread 'main' panicked at '
Internal error occurred: Command "gcc.exe" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-m64" "-DMINIZ_NO_STDIO" "-DMINIZ_NO_ARCHIVE_APIS" "-DMINIZ_NO_ARCHIVE_WRITING_APIS" "-DMINIZ_NO_TIME" "-DMINIZ_NO_ZLIB_COMPATIBLE_NAMES" "-o" "D:\Rust\Rd2dTest\target\debug\build\miniz-sys-935e563594ae4ae2\out\miniz.o" "-c" "miniz.c" with args "gcc.exe" did not execute successfully (status code exit code: 1).
', C:\Users\xiujie.zhu.cargo\registry\src\github.com-1ecc6299db9ec823\cc-1.0.35\src\lib.rs:2398:5
stack backtrace:
0: std::sys_common::backtrace::_print
at src\libstd\sys\windows\backtrace/mod.rs:95
at src\libstd\sys\windows\backtrace/mod.rs:82
at src\libstd\sys_common/backtrace.rs:71
1: std::panicking::default_hook::{{closure}}
at src\libstd\sys_common/backtrace.rs:59
at src\libstd/panicking.rs:197
2: std::panicking::default_hook
at src\libstd/panicking.rs:211
3: std::panicking::rust_panic_with_hook
at src\libstd/panicking.rs:474
4: std::panicking::continue_panic_fmt
at src\libstd/panicking.rs:381
5: std::panicking::begin_panic_fmt
at src\libstd/panicking.rs:336
6: cc::fail
at C:\Users\xiujie.zhu.cargo\registry\src\github.com-1ecc6299db9ec823\cc-1.0.35\src/lib.rs:2398
7: cc::Build::compile
at C:\Users\xiujie.zhu.cargo\registry\src\github.com-1ecc6299db9ec823\cc-1.0.35\src/lib.rs:951
8: build_script_build::main
at .\build.rs:23
9: std::rt::lang_start::{{closure}}
at /rustc/474e7a6486758ea6fc761893b1a49cd9076fb0ab\src\libstd/rt.rs:64
10: std::panicking::try::do_call
at src\libstd/rt.rs:49
at src\libstd/panicking.rs:293
11: _rust_maybe_catch_panic
at src\libpanic_unwind/lib.rs:87
12: std::rt::lang_start_internal
at src\libstd/panicking.rs:272
at src\libstd/panic.rs:388
at src\libstd/rt.rs:48
13: std::rt::lang_start
at /rustc/474e7a6486758ea6fc761893b1a49cd9076fb0ab\src\libstd/rt.rs:64
14: main
15: _tmainCRTStartup
16: mainCRTStartup
17: unit_addrs_search
18: unit_addrs_search
Hi!
I'm trying to compile code:
struct Foo {
pool: r2d2::Pool<r2d2_mysql::MysqlConnectionManager>,
}
which gives me error
src/main.rs:65:5: 65:39 error: the trait bound `r2d2_mysql::MysqlConnectionManager: r2d2::ManageConnection` is not satisfied [E0277]
src/main.rs:65 pool: Pool<MysqlConnectionManager>,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:65:5: 65:39 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:65:5: 65:39 note: required by `r2d2::Pool`
My Cargo.toml
[dependencies]
mysql = "*"
r2d2 = "*"
r2d2_mysql = "*"
rustc-serialize = "*"
If I try to use https://github.com/sfackler/r2d2-postgres and replace code with
struct Foo {
pool: r2d2::Pool<r2d2_postgres::PostgresConnectionManager>,
}
everything compiles.
I can't see real differences in this two cargos. Both have
impl r2d2::ManageConnection for PostgresConnectionManager {
impl r2d2::ManageConnection for MysqlConnectionManager {
I would appreciate any help. Thanks
I would appreciate any help for the error below
error
error[E0277]: the trait bound `r2d2::PooledConnection<r2d2_mysql::MysqlConnectionManager>: diesel::Connection` is not satisfied
--> src/graphql.rs:36:46
|
36 | .load::<crate::models::Customer>(&executor.context().db_con)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::Connection` is not implemented for `r2d2::PooledConnection<r2d2_mysql::MysqlConnectionManager>`
|
= note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<r2d2::PooledConnection<r2d2_mysql::MysqlConnectionManager>, models::Customer>` for `schema::customers::table`
error[E0277]: the trait bound `r2d2::PooledConnection<r2d2_mysql::MysqlConnectionManager>: diesel::Connection` is not satisfied
--> src/graphql.rs:56:52
|
56 | .get_result::<crate::models::Customer>(&executor.context().db_con)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the
dependencies
actix-web = "1.0.9"
actix-cors = "0.1.0"
juniper = "0.14.1"
juniper-from-schema = "0.5.1"
juniper-eager-loading = "0.5.0"
r2d2_mysql = "*"
r2d2-diesel = "0.16.0"
mysql = "*"
r2d2 = "*"
src/graphql.rs
use std::convert::From;
use std::sync::Arc;
use actix_web::{web, Error, HttpResponse};
use futures01::future::Future;
use juniper::http::playground::playground_source;
use juniper::{http::GraphQLRequest, Executor, FieldResult};
use juniper_from_schema::graphql_schema_from_file;
use diesel::prelude::*;
use itertools::Itertools;
use crate::{DbCon, DbPool};
graphql_schema_from_file!("src/schema.graphql");
pub struct Context {
db_con: DbCon,
}
impl juniper::Context for Context {}
pub struct Query;
pub struct Mutation;
impl QueryFields for Query {
fn field_customers(
&self,
executor: &Executor<'_, Context>,
_trail: &QueryTrail<'_, Customer, Walked>,
) -> FieldResult<Vec<Customer>> {
use crate::schema::customers;
customers::table
.load::<crate::models::Customer>(&executor.context().db_con)
.and_then(|customers| Ok(customers.into_iter().map_into().collect()))
.map_err(Into::into)
}
}
impl MutationFields for Mutation {
fn field_create_customer(
&self,
executor: &Executor<'_, Context>,
_trail: &QueryTrail<'_, Customer, Walked>,
name: String,
email: String,
) -> FieldResult<Customer> {
use crate::schema::customers;
let new_customer = crate::models::NewCustomer { name: &name, email: &email };
diesel::insert_into(customers::table)
.values(&new_customer)
.get_result::<crate::models::Customer>(&executor.context().db_con)
.map(Into::into)
.map_err(Into::into)
}
}
pub struct Customer {
id: u64,
name: String,
email: String,
}
impl CustomerFields for Customer {
fn field_id(&self, _: &Executor<'_, Context>) -> FieldResult<juniper::ID> {
Ok(juniper::ID::new(self.id.to_string()))
}
fn field_name(&self, _: &Executor<'_, Context>) -> FieldResult<&String> {
Ok(&self.name)
}
fn field_email(&self, _: &Executor<'_, Context>) -> FieldResult<&String> {
Ok(&self.email)
}
}
impl From<crate::models::Customer> for Customer {
fn from(customer: crate::models::Customer) -> Self {
Self {
id: customer.id,
name: customer.name,
email: customer.email,
}
}
}
fn playground() -> HttpResponse {
let html = playground_source("");
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(html)
}
fn graphql(
schema: web::Data<Arc<Schema>>,
data: web::Json<GraphQLRequest>,
db_pool: web::Data<DbPool>,
) -> impl Future<Item = HttpResponse, Error = Error> {
let ctx = Context {
db_con: db_pool.get().unwrap(),
};
web::block(move || {
let res = data.execute(&schema, &ctx);
Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?)
})
.map_err(Error::from)
.and_then(|customer| {
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(customer))
})
}
pub fn register(config: &mut web::ServiceConfig) {
let schema = std::sync::Arc::new(Schema::new(Query, Mutation));
config
.data(schema)
.route("/", web::post().to_async(graphql))
.route("/", web::get().to(playground));
}
src/main.rs
#[macro_use]
extern crate diesel;
extern crate r2d2;
use actix_cors::Cors;
use actix_web::{web, App, HttpServer};
use r2d2_mysql::mysql::{Opts, OptsBuilder};
use r2d2_mysql::MysqlConnectionManager;
use diesel::{
prelude::*,
};
pub mod graphql;
pub mod models;
pub mod schema;
pub type DbPool = r2d2::Pool<MysqlConnectionManager>;
pub type DbCon = r2d2::PooledConnection<MysqlConnectionManager>;
fn main() {
let db_pool = create_db_pool();
let port: u16 = std::env::var("PORT")
.ok()
.and_then(|p| p.parse().ok())
.unwrap_or(8080);
let addr = std::net::SocketAddr::from(([0, 0, 0, 0], port));
HttpServer::new(move || {
App::new()
.data(db_pool.clone())
.wrap(Cors::new())
.configure(graphql::register)
.default_service(web::to(|| "404"))
})
.bind(addr)
.unwrap()
.run()
.unwrap();
}
pub fn create_db_pool() -> DbPool {
let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let opts = Opts::from_url(&db_url).unwrap();
let builder = OptsBuilder::from_opts(opts);
let manager = MysqlConnectionManager::new(builder);
r2d2::Pool::new(manager).expect("Failed to create DB Pool")
}
When trying to use r2d2-mysql I get several of these: type 'io::IoPack + 'static' is private
in mod.rs
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.