persistent database connection
Asked By: FoiaZoig
Originally Asked On: 2014-01-05 23:48:38
Asked Via: stackoverflow
I’m making a small program in java that is going to run on an embedded device.
the purpose of it is to store some values (let’s say temperature) in a database located in a cloud.
- Scenario 1: data gets stored every possible cycle (connect-store-store-store… -disconnect)
- Scenario 2: data gets stored every 5 minutes (connect-store-wait-store-wait… -disconnect)
- Scenario 3: data gets stored every hour
Is it better to maintain a constant connection with the database or to connect only when needed? Why?
What would happen if deployed 100 of this devices (one can never have enough temperature data)?
He received 1 answers
eventually accepting:
Mitch Wheat’s answer to
persistent database connection
Unless there are special circumstances, it’s almost always connect only when needed. There are several advantages:
- Resources are held for the shortest time possible, thus maximising their availability.
- The lifetime scope of connections is controlled at a single point in code, minimising the chance of resource leaks.
- Data is written frequently, thus minimising loss of data due to crashes.
- Retry logic is simpler, since connection state is localised to a single point in code
In general: Open connections as late as possible, as briefly as is possible, and close as quickly as possible
(Many database systems implement connection pooling which makes this process very efficient)
If the selected answer did not help you out, the other answers might!
All Answers For: persistent database connection
Mitch Wheat’s answer to
persistent database connection
Unless there are special circumstances, it’s almost always connect only when needed. There are several advantages:
- Resources are held for the shortest time possible, thus maximising their availability.
- The lifetime scope of connections is controlled at a single point in code, minimising the chance of resource leaks.
- Data is written frequently, thus minimising loss of data due to crashes.
- Retry logic is simpler, since connection state is localised to a single point in code
In general: Open connections as late as possible, as briefly as is possible, and close as quickly as possible
(Many database systems implement connection pooling which makes this process very efficient)
Of course, you should really check out the original question.
The post persistent database connection [ANSWERED] appeared first on Tech ABC to XYZ.