K
K
koi com2014-02-15 19:02:34
MySQL
koi com, 2014-02-15 19:02:34

How and when to get a connection to the database?

An application is being written. To interact with the database, a DAO layer is used. And now I think to open the connection when the application starts and, accordingly, when the application is closed - close it. Or open a connection only when it is directly accessed (for example, calling the getAll() method) and close it when the operation is completed (connection.close()). I write in java, DBMS - MySQL. Point to the right path. Or links where it is described in detail what and why.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
svd71, 2014-02-15
@koi_jp

if the application has only a couple of requests to the database - this approach is not scary. But if it involves intensive work and open cursors, then you need to open the database the first time you access the database, then just check that the connection is established. Closing is best when finalizing objects.

S
Sergey, 2014-02-15
@begemot_sun

Well, it would be normal to open a connection when it is really needed, and close it after the N-time of not using it.
BUT.
If this is a desktop application, then such logic can postpone the moment when it turns out that the database is not available.

O
OnYourLips, 2014-02-16
@OnYourLips

Open at the first request to the database, it will close itself.
If there is one base, or there are several of them, then you can always open it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question