pg: Replacing Connection::Transaction() with TransactionBuilder::run()? Modify Transaction Isolation Level to REPEATABLE READ
#4982
-
BackendPostgreSQL Diesel version2.3.0 Diesel Featurespostgres, uuid, chrono, serde Operating System VersionNo response Third party librariesNo response What do you want to do?I have a handler function called I currently use this: //connection() returns a r2d2 PooledConnection
let ret = context.connection().transaction::<T, E, _>(|connection| {
//normal: connection is of type PooledConnection
let mut context = MyContext::new(extra_stuff, connection);
f(&mut context)
});I tried this: //connection() returns a r2d2 PooledConnection
context.connection().build_transaction()
.repeatable_read().run::<T, E, _>(|connection| {
//issue: connection changed from PooledConnection to PgConnection
let mut context = MyContext::new(extra_stuff, connection);
f(&mut context)
});I encountered an error where the type of connection that is passed inside of TransactionBuilder::run() has been changed from the pooled connection type into a regular connection. Unfortunately, all of my internal APIs require the pooled type (maybe they shouldn't? I didn't think they were interchangeable or equivalent). Corollary questions:
[1] https://docs.rs/diesel/latest/diesel/r2d2/struct.PooledConnection.html Compile time errormismatched types
expected mutable reference `&mut PooledConnection<ConnectionManager<diesel::PgConnection>>`
found mutable reference `&mut diesel::PgConnection`Additional detailsI could manually emit a Checklist
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
This is expected behaviour. The corresponding transaction methods just expose the underlying connection types. |
Beta Was this translation helpful? Give feedback.
-
|
For those reading along the answer appears to be that R2D2's pooled connection type is a smart pointer. And if you find yourself in my situation (where you have a database abstraction layer that expects a certain specific/non-generic type of connection) you can change a Thankfully in my internal library all I need to do is change into and call |
Beta Was this translation helpful? Give feedback.
For those reading along the answer appears to be that R2D2's pooled connection type is a smart pointer. And if you find yourself in my situation (where you have a database abstraction layer that expects a certain specific/non-generic type of connection) you can change a
&mut PooledConnection<ConnectionManager<PgConnection>>into a&mut PgConnectionvia the deref trait ie.my_pooled_connection.deref_mut()Thankfully in my internal library all I need to do is change
into
and call
.deref_mut()in a few places. And then I'll be able to use theTransactionBuilderAPI without runni…