Do I need to close the connection on each query?

7

Currently I have a web application that queries multiple databases in the same controller. For each bank I make a connection at server startup and keep them in a static class where their DAOs have used it and close it in the context destroy of the application.

My question is, do I really need to open and close the connection to each controller call? Does not the way I use today bring me performance issues?

    
asked by anonymous 13.07.2017 / 14:50

1 answer

5
  

My question is, do I really need to open and close the connection to each controller call?

Database connections are often stateless . That is, open, do what you have to do and close.

I, in particular, see no reason to hold an open connection to the bank, even though datasources settings who just check for idle connections and close them.

When I say "do what you have to do" does not mean to run a single query. You can run N queries, N transactions and etc.

If you have a list of customers to register for the bank, for example, you do not need to open and close a connection for each item in that list. You may well open a connection, iterate the list by entering all the clients in the database through that connection and then closing it. The important thing is not to hold the connection open "forever".

  

Does not the way I use today bring me performance issues?

If you use Connection Pool do not. The pool was designed exactly for the purpose of making it easier to save a connection to the bank.

    
13.07.2017 / 16:51