Java JDBC efficiency: How long should a connection be maintained?

后端 未结 4 997
悲哀的现实
悲哀的现实 2021-02-10 01:37

I\'m still working on the same problem mention here. It seems to work fine especially after creating an AbstractModel class shown below:

public abstract class Ab         


        
4条回答
  •  无人及你
    2021-02-10 01:58

    You'are right that you don't need to close the connection after each call.
    Bare in mind that that modern database implement internal connection pools, but your application still need to connect and retrieve a connection object, and this is what it does now.
    You should consider using a database connection pool - there are various Java frameworks to provide you such a solution, and they will define (you will be able to configure of course) when a database connection pool is closed.

    In general - you should ask yourself whether your database serves only your application, or does it serve other application as well - if it does not serve other application as well, you might be able to be more "greedy" and keep connections open for a longer time.
    I would also recommend that your application will create on start a fixed number of connections (define it in your configuration with a value of "Minimum connections number") and you will let it grow if needed to a maximum connection numbers.

    As I previously mentioned - the ideas are suggest are implemented already by all kinds of frameworks, for example - the DBCP project of Apache.

提交回复
热议问题