3 messages in com.mysql.lists.javaRe: Question Regarding MM JBDC Driver...| From | Sent On | Attachments |
|---|---|---|
| Douglas A. Capeci | 02 Oct 1999 06:49 | |
| Cris Perdue | 02 Oct 1999 08:30 | |
| Douglas A. Capeci | 02 Oct 1999 19:25 |
| Subject: | Re: Question Regarding MM JBDC Driver Connections![]() |
|---|---|
| From: | Cris Perdue (cr...@perdues.com) |
| Date: | 10/02/1999 08:30:49 AM |
| List: | com.mysql.lists.java |
These are good questions. We should put them in a FAQ. Anybody got a good location for a MySQL JDBC FAQ???
I am developing Java servlets to be invoked by Sun's Java Web Server product, and am wondering if it is necessary to obtain a new connection from the driver for each individual user http session that gets created by the Web server, or is it possible to obtain only one connection for all user http sessions to share.
A key issue here is that Servlets are multithreaded, and may be working on multiple requests simultaneously. I believe there is not enough locking in the current drivers to guarantee their integrity if you share a JDBC Connection across threads. As a practical matter you probably would not wish to share a JDBC Connection across requests (thus across threads). Both of the modern drivers, MM and TWZ, map each JDBC Connection to a MySQL connection, and MySQL only handles one SQL request at a time on one connection. So to get prompt response to each HTTP request, you will want to assign each active request a Connection.
If only one connection is needed, can I be sure that individual user sessions won't interfere with one another and that the integrity of the database won't be compromised.
You will want more than one Connection. Integrity of the database itself though, is probably not of concern since MySQL does contain adequate internal locking.
Could someone please enlighten me on this issue? Please consider the posibility that individual sessions could have open result sets that are accessed across multiple servlet invocations.
Yes, your concern is right on target.
What people do, and what our site does, is to give each HTTP request its own JDBC Connection, and to allocate JDBC connections from a "pool" and return them to the pool. You should be able to benefit from a package such as the ConnectionPool package available from www.bitmechanic.com.
With pooling, the idea is to make some pretty reliable mechanism ensure that each HTTP request and thus each thread has its own Connection, yet Connections get reused so you don't leak connections and file descriptors. You can do this with application design discipline (and Java "catch" or "finally" clauses!) or a thread pooling package.
If you either use GSP (also from bitmechanic.com) or if your Servlet engine (Java Web Server) supports the 2.1 version of the Servlet API's, you might associate a JDBC Connection with each HTTP request, by making it an attribute of the Request with "setAttribute", and pass the connection around as you pass the Request around. This may make it easier to manage Connections.
Note that JDBC allows each Statement to have one ResultSet open, and each Connection can have multiple open Statements. Both the MM and TWZ drivers multiplex the MySQL connection to accomplish this. (Remember MySQL allows only one open result set on a connection at one time.) MM always greedily reads from the MySQL connection to always free it up for another request ASAP. TWZ doesn't get greedy unless it receives another request. A second request on the connection prods it to grab the remaining data from MySQL and buffer it internally. Both seem to work well in practice.
-- Cris Perdue Impact Online, Inc. http://www.volunteermatch.org




