Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with connection pooling.

p Walsh
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , I relativly new to this but i am trying to run a program to demonstrate pooling using a jdbc connection to my sql database.
Everytime i try to run this program i receive an error
"Exception in thread "main" java.lang.NoSuchMethodError: main"

The code is as follows....

import java.sql.*;
import java.util.*;
import java.lang.*;
public class ConnectionPool implements Runnable {
private String driver, url, username, password;
private int maxConnections;
private boolean waitIfBusy;
private Vector availableConnections, busyConnections;
private boolean connectionPending = false;
public ConnectionPool(String driver, String url,
String username, String password,
int initialConnections,
int maxConnections,
boolean waitIfBusy)
throws SQLException {
this.driver = "com.mysql.jdbc.Driver";
this.url = "jdbc:mysql://localhost/expenses";
this.username = "pat";
this.password = "09451664";
this.maxConnections = maxConnections;
this.waitIfBusy = waitIfBusy;
if (initialConnections > maxConnections) {
initialConnections = maxConnections;
availableConnections = new Vector(initialConnections);
busyConnections = new Vector();
for(int i=0; i<initialConnections; i++) {

public synchronized Connection getConnection()
throws SQLException {
if (!availableConnections.isEmpty()) {
Connection existingConnection =
int lastIndex = availableConnections.size() - 1;
// If connection on available list is closed (e.g.,
// it timed out), then remove it from available list
// and repeat the process of obtaining a connection.
// Also wake up threads that were waiting for a
// connection because maxConnection limit was reached.
if (existingConnection.isClosed()) {
notifyAll(); // Freed up a spot for anybody waiting
} else {
} else {

// Three possible cases:
// 1) You haven't reached maxConnections limit. So
// establish one in the background if there isn't
// already one pending, then wait for
// the next available connection (whether or not
// it was the newly established one).
// 2) You reached maxConnections limit and waitIfBusy
// flag is false. Throw SQLException in such a case.
// 3) You reached maxConnections limit and waitIfBusy
// flag is true. Then do the same thing as in second
// part of step 1: wait for next available connection.

if ((totalConnections() < maxConnections) &&
!connectionPending) {
} else if (!waitIfBusy) {
throw new SQLException("Connection limit reached");
// Wait for either a new connection to be established
// (if you called makeBackgroundConnection) or for
// an existing connection to be freed up.
try {
} catch(InterruptedException ie) {}
// Someone freed up a connection, so try again.
// You can't just make a new connection in the foreground
// when none are available, since this can take several
// seconds with a slow network connection. Instead,
// start a thread that establishes a new connection,
// then wait. You get woken up either when the new connection
// is established or if someone finishes with an existing
// connection.
private void makeBackgroundConnection() {
connectionPending = true;
try {
Thread connectThread = new Thread(this);
} catch(OutOfMemoryError oome) {
// Give up on new connection
public void run() {
try {
Connection connection = makeNewConnection();
synchronized(this) {
connectionPending = false;
} catch(Exception e) { // SQLException or OutOfMemory
// Give up on new connection and wait for existing one
// to free up.
// This explicitly makes a new connection. Called in
// the foreground when initializing the ConnectionPool,
// and called in the background when running.

private Connection makeNewConnection()
throws SQLException {
try {
// Load database driver if not already loaded
// Establish network connection to database
Connection connection =
DriverManager.getConnection(url, username, password);
} catch(ClassNotFoundException cnfe) {
// Simplify try/catch blocks of people using this by
// throwing only one exception type.
throw new SQLException("Can't find class for driver: " +
public synchronized void free(Connection connection) {
// Wake up threads that are waiting for a connection

public synchronized int totalConnections() {
return(availableConnections.size() +
/** Close all the connections.**/
public synchronized void closeAllConnections() {
availableConnections = new Vector();
busyConnections = new Vector();
private void closeConnections(Vector connections) {
try {
for(int i=0; i<connections.size(); i++) {
Connection connection =
if (!connection.isClosed()) {
} catch(SQLException sqle) {
// Ignore errors; garbage collect anyhow

public synchronized String toString() {
String info =
"ConnectionPool(" + url + "," + username + ")" +
", available=" + availableConnections.size() +
", busy=" + busyConnections.size() +
", max=" + maxConnections;
public static void main(String args[])
ConnectionPool pool = new ConnectionPool():
I presumed this error related to the fact that i did not have a void main method so i added one see last 4 lines above and then received an error 'class' or 'interface' expected
public static void main(String args[])
Can you please advise me on this,
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've incorrectly placed some braces. You inadvertently placed your main function outside of a class -- which in Java is course a no-no.
You should take out one of the braces immediately prior to your main function declaration and place it after the main function. At that point, your class will compile and you can invoke it as normal.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic