Skip to content

returning a Transaction for further usage #1227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
fredski02 opened this issue Mar 20, 2025 · 3 comments
Open

returning a Transaction for further usage #1227

fredski02 opened this issue Mar 20, 2025 · 3 comments

Comments

@fredski02
Copy link

fredski02 commented Mar 20, 2025

I'm trying to start a transaction and return it for later usage but i get lifetime issues because the lifetime of the transaction is tied to the client/connection. Is there a proper way to do this? I've tried wrapping the Transaction in a struct, using Box and tokio Mutex but nothing seemed to help

see below pseudo code.

#[async_trait]
trait myTrait {
  async fn start() -> Transaction {
         let mut client = self.get().await.unwrap(); // Get the client from the pool  
          let mut transaction = client.transaction().await.unwrap(); 
  
         // do some stuff with transaction
  
         transaction
  
  }
}

using the following core libs

tokio = { version = "1.38", features = ["macros", "rt-multi-thread", "signal"] }
tokio-postgres = { version = "0.7", features = ["runtime", "array-impls"] }
tokio-postgres-rustls = "0.13"
bb8 = "0.9"
bb8-postgres = "0.9"
warp = "0.3"
async-trait = "0.1"
@sfackler
Copy link
Owner

You'd need to structure your API so the client stays checked out of the pool until the transaction ends.

@fredski02
Copy link
Author

You'd need to structure your API so the client stays checked out of the pool until the transaction ends.

You mean starting the transaction outside of my fn start?

@sfackler
Copy link
Owner

That'd be one way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants