Is it efficient method to open database connection for every database operation and accordingly close
or open the connection at the starting and keep calling the same opened connection for multiple operation and close it when all the activities is done. ?
I am creating a Java Swing application where i have a main frame and using the card layout i am selecting different panel and each of this panel have different database operation to perform. Presently i am having individual connection opened for each panel , so when i open my main application multiple connection gets opened and closed.
As i want to be sure that i am doing the right thing and avoid any bad practice and bottleneck in performance so i am asking this question to Java Experts.
Please suggest .Thanks for all your help .
No, it isn’t recommended to “create” a new connection for each database transaction. Creating connections is typically expensive. Try using a connection pool which abstracts the connection open and close semantics. That way, your application can call
Connection#closeetc. and behind the scenes the pool will grab and return connections to the connection poll accordingly. There are many solutions out there; DBCP, C3P0 or BoneCP.