Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

connection pooling+jsp

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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();
}
///////////////////////////////////////////////////////
}
 
Bartender
Posts: 2662
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please UseCodeTags .
Related to your other post https://coderanch.com/t/505359/JDBC/java/connection-pooling

 
    Bookmark Topic Watch Topic
  • New Topic