I searched for connection pooling and read about it. If I understand it correctly, a connection pool is like a collection of open connections. If a connection is established or created it should be added to the connection pool, and if that connection is closed it should be removed in connection pool; while it is open I can use it again and again.
While reading these tutorials and explanations about connection pooling I have some questions:
-
Can a pool of connections only be used on a certain computer? Like ComputerA
cannot share its connection pool with ComputerB? -
Where should
connection.close()
be placed?
Is it correct to use a connection ONLY when selecting/loading record? After I got the returned records/data I close the connection at finally
statement. Same as adding, editing and deleting records. And while it is processing I place a progress bar so the user will have to wait for it to be completed and to do some process again, which means I will only open connection one at a time.
Thanks for the explanation. 🙂
Best Answer
Note: I assume we're talking about the
java.sql.Connection
interface.A connection exists between a running application and a database. Naturally, two different machines can't share the same running application, so they can't share connections with a database.
You should always make sure to call
close()
on aConnection
instance after using it (typically in afinally
block). If pooling is being used, this will actually return the connection to the pool behind the scenes. Reference: Closing JDBC Connections in PoolYes, that's correct. You don't want to manually hang on to a
Connection
reference - use it to execute SQL/DML and then check it back into the pool by callingclose()
in thefinally
block, just like you're doing.