codewinsdotcom/PostgresClientKit

Not possible to pass around a connection?

SergioB-dev opened this issue · 1 comments

Expected behavior: To be able to pass around a Connection to any interested callers
Actual behavior: The only way I can successfully connect to a database is if I instantiate and use Connection in the same function that consumes it.

Following the example in the ReadMe I was able to connect to the database successfully. However, when I refactored this code to separate some shared responsibilities into separate functions I ran into problems.

My code follows this code in the readme exactly until this point..

let connection = try PostgresClientKit.Connection(configuration: config)
            defer { connection.close() } // Instead of deferring to close the connection here I return it to then be used by other functions

Here is how I then attempted to use this Connection

let connection = self.makeConnection()
        print("Connection closed?: \(connection.isClosed)") // Connection is open
        do {
            let statement = try connection.prepareStatement(text: sqlStatement)
            // I fail on this first try statement above with The operation couldn’t be completed. (PostgresClientKit.PostgresError error 3.)
        ...

I'm happy you got it working. Could you please post what the problem was, and how you resolved it, in case someone else has a similar issue? Thanks!