Koon Yue Lam wrote:
If you are using an object whose class implements javax.sql.DataSource, you are indeed using a connection pool. The number of connections in the pool is determined by your configuration properties, and the getConnection() method will reuse an existing connection from the pool (if there is one), only creating new connections when necessary -- and when the total number of connections you specify has not yet been reached.Hi ! I have successfully use struts to connect MySql but I wonder do I using the database connection pool, or just a new connection for each request?? All I did is very simple stuffs and I follow strict to the database How-To section of struts doc. How can I know I am actually using a connection pool ??
A very important consideration when using connection pools is to ensure that you *always* return the connection to the pool, even when an exception is encountered. One pattern I like to follow that ensures this is to use a "try ... finally" block, something like this:
Connection conn = null; DataSource ds = ...; // Get a reference to your data source try { conn = ds.getConnection(); // Allocate a connection from the pool ... use the connection as necessary ... } catch (SQLException e) { ... deal with exceptions as needed ... } finally { // If you got a connection from the pool, the close() method // does *not* close the physical connection; it returns this one // to the available pool if (conn != null) { conn.close(); } }
The "finally" block is always executed (even if an exception occurs), so this design idiom ensures that there's no way for me to forget to return a connection when I am done with it.
Regards
Craig McClanahan
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]