johnnadratowski / golang-neo4j-bolt-driver

Golang Bolt driver for Neo4j
MIT License
213 stars 72 forks source link

sharing connection object within multiple functions #43

Open praveensl opened 6 years ago

praveensl commented 6 years ago

Hi,

Im trying to figure out how to place the connection object inside a struct that can be shared by multiple functions. in order to do that i have to define the placeholder inside the struct. because boltConn isnt exposed outside the package I cant find a way to do this.

is this really an defect? or was it never meant to be shared within multiple functions?

xiaojinwhu commented 6 years ago

I am also looking for a solution to this problem but I have no idea

collisonchris commented 6 years ago

Not sure if you're trying to do the same thing I am but I used this method to share conn objects/embed the DriverPool

type NeoClient struct {
    Pool bolt.DriverPool
}

func NewNeo4jClient(hostname string, port string, user string, password string) (NeoClient, error) {
    pool, err := bolt.NewDriverPool("bolt://"+user+":"+password+"@"+hostname+":"+port, 5)
    boltLogs.SetLevel("error")
    return NeoClient{pool}, err
}

func getConnectionFromPool(pool bolt.DriverPool) bolt.Conn {
    conn, err := pool.OpenPool()
    if err != nil {
        log.Error("Unable to get connection from Pool!")
    }
    return conn
}

Then just call getConnectionFromPool passing in the embeeded pool

func (neoClient NeoClient) GenericQueryAll(query string, params map[string]interface{}) ([][]interface{}, error) {
    conn := getConnectionFromPool(neoClient.Pool)
    defer conn.Close()
    data, _, _, err := conn.QueryNeoAll(query, params)
    return data, err
}