I'm connecting to a small PostgreSQL database on the local LAN using Java and running some queries and I'm not sure what is best practice.
Should I close the connection after each query or keep the connection open for the next query?
I'm connecting to a small PostgreSQL database on the local LAN using Java and running some queries and I'm not sure what is best practice.
Should I close the connection after each query or keep the connection open for the next query?
you probably want to keep the connection open for as long a possible, but in practice it probably won't matter much unless you're making hundreds of little queries and reconnecting for every one
here's an overview of what's going on, assuming your network has ping times of around 1ms:
if you reuse this connection then you save the cost of authentication and catalog loading. you're therefore recommended to reuse connections where possible so this work isn't performed every time. depending on your usage this may or may not actually make much difference. for example: