This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes connection pooling+jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Reply locked New topic
Author

connection pooling+jsp

gaser hasan
Greenhorn

Joined: Aug 03, 2010
Posts: 9
Please iam new to java and jsp and want to implement this message but it show me that there is no connection
so could any one implement it and tell me what is the problem
the code is place down


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author gaser_java
*/
public class DBConnectionPool {

private String name;//= "alsa7a1c";
private String URL;//= "jdbc:mysql://localhost:3307/alsa7a1c?useUnicode=true&characterEncoding=UTF-8";
private String user;//= "alsa7a1c";
private String password;// = "pasword";
private int maxConn;//= 25;
private Vector freeConnections;

public DBConnectionPool(String name, String URL, String user,
String password, int maxConn) {
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
}
int checkedOut = 0;

public synchronized Connection getConnection() throws SQLException {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(URL, user, password);
freeConnections = new Vector(maxConn);

if (freeConnections.size() > 0) {
// Pick the first Connection in the Vector
// to get round-robin usage
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
log("Removed bad connection from " + name);
// Try again recursively
con = getConnection();
}
} catch (SQLException e) {
log("Removed bad connection from " + name);
// Try again recursively
con = getConnection();
}
} else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
if (con != null) {
checkedOut++;
}

} catch (ClassNotFoundException ex) {
Logger.getLogger(connections.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}

private void log(String string) {
throw new UnsupportedOperationException("Not yet implemented");
}

private void log(SQLException e, String string) {
throw new UnsupportedOperationException("Not yet implemented");
}
////////////////////////////////////////

private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
} else {
con = DriverManager.getConnection(URL, user, password);
}
log("Created a new connection in pool " + name);
} catch (SQLException e) {
log(e, "Can't create a new connection for " + URL);
return null;
}
return con;
}
/////////////////////////////////////////////////

public synchronized Connection getConnection(long timeout) throws SQLException {
long startTime = new Date().getTime();
Connection con;
while ((con = getConnection()) == null) {
try {
wait(timeout);
} catch (InterruptedException e) {
}
if ((new Date().getTime() - startTime) >= timeout) {
// Timeout has expired
return null;
}
}
return con;
}
///////////////////////////////////////////////////////

public synchronized void freeConnection(Connection con) {
// Put the connection at the end of the Vector
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
///////////////////////////////////////////////////////

public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
log("Closed connection for pool " + name);

} catch (SQLException e) {
log(e, "Can't close connection for pool " + name);
}
}
freeConnections.removeAllElements();
}
///////////////////////////////////////////////////////
}
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

Please UseCodeTags .
Related to your other post http://www.coderanch.com/t/505359/JDBC/java/connection-pooling


OCUP UML fundamental and ITIL foundation
youtube channel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: connection pooling+jsp
 
Similar Threads
pool name in DBConnectionManager
why jsp cannot use this class, which is no problem when compile singlely.
nullpointerexception
Lost properties
a mistake for bean