That code was really more for descriptive purposes than deployment. It
works, but it doesn't do a wonderful job of maintaining connection
integrity over the long term. And I feel perfectly comfortable saying so,
because I wrote that chapter <grin>.
A more robust alternative is the DbConnectionBroker class available from
javaexchange.com--that's what I've been using (with some tweaks) in a
production environment for the last year or so.
Will
At 08:50 AM 6/26/2000 +0900, you wrote:
>Hi Tim,
>
>I've bought Jason Hunter's book and following is the code from that book
>which is also freely available from his website http://www.servlets.com
>I think it is better you get that book, it is an excellent book.
>
>
>import java.sql.*;
>import java.util.*;
>
>public class ConnectionPool {
> private Hashtable connections;
> private int increment;
> private String dbURL, user, password;
>
> public ConnectionPool(String dbURL, String user, String password,
>String driverClassName, int initialConnections, int increment)
> throws SQLException, ClassNotFoundException {
>
> // Load the specified driver class
> Class.forName(driverClassName);
>
> this.dbURL = dbURL;
> this.user = user;
> this.password = password;
> this.increment = increment;
>
> connections = new Hashtable();
>
> // Put our pool of Connections in the Hashtable
> // The FALSE value indicates they're unused
> for(int i = 0; i < initialConnections; i++) {
> connections.put(DriverManager.getConnection(dbURL,
>user, password), Boolean.FALSE);
> }
> }
>
> public Connection getConnection() throws SQLException {
> Connection con = null;
>
> Enumeration cons = connections.keys();
>
> synchronized (connections) {
> while(cons.hasMoreElements()) {
> con = (Connection)cons.nextElement();
>
> Boolean b = (Boolean)connections.get(con);
> if (b == Boolean.FALSE) {
> // So we found an unused connection.
> // Test its integrity with a quick
>setAutoCommit(true) call.
> // For production use, more testing
>should be performed,
> // such as executing a simple query.
> try {
> con.setAutoCommit(true);
> }
> catch(SQLException e) {
> // Problem with the
>connection, replace it.
> con =
>DriverManager.getConnection(dbURL, user, password);
> }
> // Update the Hashtable to show this
>one's taken
> connections.put(con, Boolean.TRUE);
> // Return the connection
> return con;
> }
> }
> }
>
> // If we get here, there were no free connections.
> // We've got to make more.
> for(int i = 0; i < increment; i++) {
> connections.put(DriverManager.getConnection(dbURL,
>user, password), Boolean.FALSE);
> }
>
> // Recurse to get one of the new connections.
> return getConnection();
> }
>
> public void returnConnection(Connection returned) {
> Connection con;
> Enumeration cons = connections.keys();
> while (cons.hasMoreElements()) {
> con = (Connection)cons.nextElement();
> if (con == returned) {
> connections.put(con, Boolean.FALSE);
> break;
> }
> }
> }
>}
>
>Thanks & Regards, Goldman
>Atchutarao Killamsetty, Sachs
>GSAM-IT,Tokyo
>Tel:03-5573-7871
>
>
>
>-----Original Message-----
>From: Tim Stoop [mailto:[EMAIL PROTECTED]]
>Sent: Monday, June 26, 2000 7:05 AM
>To: [EMAIL PROTECTED]
>Subject: Servlets authenticating with MySQL (was: Re: JDBC Connection
>Management)
>
>
>----- Original Message -----
>From: William Crawford <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>Sent: Sunday, June 25, 2000 8:59 PM
>Subject: Re: JDBC Connection Management
>
>
> > None of which is to say that this isn't a useful technique in some
> > situations, as Nic described. The two cases where I would suggest this are
> > when authentication runs through the database's native authentication
> > system, and when transactions might run across multiple servlet
> > invocations. However, there are relatively few situations where this will
> > come up: authenticating through the database is a pretty resource
>intensive
> > operation, and you're probably better served storing this information in
>an
> > LDAP server (via JNDI) or a database table accessed by the server via a
> > generic account. I'd also suggest structuring your application to avoid
>the
> > necessity of stringing a transaction out across multiple invocations: use
> > intermediary tables and hold on to an identifier rather than a whole
> > connection.
>
>I'm relatively new at Java, servlets and servers, but I'm trying to learn as
>fast as I can. I'm not using connection pooling as yet, but I'm studying the
>possibility. They way I work is as followed: I have one Connection-object
>that connects to MySQL with it's own login/password-combo. Each instance of
>a servlet connects anew (sometimes several times during the process of
>processing one "string" of data) to the database.
>
>My question isn't as professional as others, but I would like to know the
>answer. Reading the post from William, I would like to know if the server is
>bogged down due to the authentication-process.
>
>Another question is, where can I find documentation about connection-pooling
>and how to implement this? (I'm using mm.mysql for JDBC.)
>
>Kind regards,
>Met vriendelijke groet,
>Tim Stoop
>
>___________________________________________________________________________
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
>___________________________________________________________________________
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
---
William Crawford + 01 617 577 7844
Invantage, Inc [EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html