SCJP 1.4, SCWCD 1.3, SCBCD 1.3
[OCP 11 Complete Study Guide] [OCP 11 Programmer I Book] [OCP 11 Programmer II Book] [OCP 11 Practice Tests Book] [OCA 8 Book] [OCP 8 Book] [OCP 8 Practice Tests Book] [Blog]
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
[OCP 11 Complete Study Guide] [OCP 11 Programmer I Book] [OCP 11 Programmer II Book] [OCP 11 Practice Tests Book] [OCA 8 Book] [OCP 8 Book] [OCP 8 Practice Tests Book] [Blog]
Originally posted by Scott Selikoff:
You don't need the static closeConnection nor would it be a good idea to have. You can just do conn.close() with the object you all ready have in the first line. In general, getConnection() should always allocate a new connection, since the process as you point out is going to close it in a finally block.
[OCP 11 Complete Study Guide] [OCP 11 Programmer I Book] [OCP 11 Programmer II Book] [OCP 11 Practice Tests Book] [OCA 8 Book] [OCP 8 Book] [OCP 8 Practice Tests Book] [Blog]
I was commenting more on Roger suggestion, if guaranteed to close the connection after using it (via a finally block), it makes no sense to keep the connection alive in some static object, since other threads could come in and request the connection before/after/during you closed it. In the event you're guaranteed to close the connection, then its better that getConnection() just creates a new connection.
You can store a connection locally as part of a single process, but storing in as a static object across the entire application is not a great idea especially if there's any chance this is a multi-threaded environment. This is what connection pools are for, to keep a group of available connections. When you request a connection object from a pool, you never get one someone else is using.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
[OCP 11 Complete Study Guide] [OCP 11 Programmer I Book] [OCP 11 Programmer II Book] [OCP 11 Practice Tests Book] [OCA 8 Book] [OCP 8 Book] [OCP 8 Practice Tests Book] [Blog]
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|