Logging and Database Connection Pooling

I l @ ve RuBoard

Another thing you're going to spend much of your time doing in this project is opening and closing database connections. If you were doing this using J2EE, you would have connection-pooling mechanisms available automatically. But because you are working in pure JSP, you have to manage the connections yourself.

Now, one way to handle this is to have each usage of the database open and close a connection explicitly. A number of problems are associated with this approach, however. For one thing, this would require every class that talked to the database to have enough information available to establish a connection. Also, opening and closing database connections are just about the most expensive thing an application can do, so you wouldn't want to do them lightly.

Another possibility to accomplish this task is to have a global database connection object hanging around as a session or application property. You could use that whenever you wanted to perform a database operation. This is getting closer to the "right" solution; however, it still requires every class that wants to use the connection to do a lookup on the session or application, which is also a bit computationally expensive. In addition, this solution requires you to monitor the connection because, if you leave it idle too long, it might time out. This also would force you to synchronize access to the database because having multiple threads using the same JDBC connection can cause some weird errors.

You could also write your own connection-pooling package, but, luckily, the good folks over at the Apache project have one already available as part of the Turbine project; you can just borrow theirs.

Turbine

Turbine is an application framework that allows developers to create secure Web applications. It has all the "glue" that developers find themselves looking for when they author complex applications, such as database connection pooling, job scheduling, parameter parsing, and so on. Turbine can be used as an all-in-one Web platform or as a series of discrete modules, as you will use it here.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net