//fix this /
Hint: The bug in the
pool class is not a
concurrency issue.
In the May/June 2012 issue, Angela
Caicedo presented us with a JavaFX code challenge
around binding. She asked us to consider a code
fragment and determine the output.
The correct answer is #4: you will get an exception
when you try to call the set method on myBoundInt.
What is happening? myBoundInt is a bound variable,
and it is not supposed to be changed directly. You can
only change the value of myBoundInt through myInt,
the property myBoundInt is bound to.
This issues challenge comes from Jason Hunter (top left),
author of Java Servlet Programming, 2nd Edition (O Reilly
Media) and deputy CTO at MarkLogic, and Boris Shukhat,
applications programming manager, vice president, Bank of
America Merrill Lynch.
2 THE CODE
Below is a trimmed code snippet from the pool class. Can you spot the
problem and fix it in place without redesigning the whole program?
private Hashtable connections = new Hashtable();
private void initializePool(...) ... {
for (int i = 0; i < initialPoolSize; i++)
connections.put(getNewConnection(...), Boolean.FALSE); //
false=free
}
}
public Connection getConnection() ... {
// ... find a connection with a FALSE flag ...
connections.put(con, Boolean.TRUE);
return con;
}
public void returnConnection(Connection returned) {
connections.put(returned, Boolean.FALSE);
}JAVA IN ACTION
1 THE PROBLEM
The ConnectionPool.java example from Chapter 9 of
Java Servlet Programming has a subtle bug. The pool code
worked fine for many years, but a recent upgrade to a
JDBC driver broke it.
3 WHAT S THE FIX?
1) Instead of Connection, use a PooledConnection.
2) Instead of Hashtable, use another implementation of
the Map interface.
3) Extend the problematic Connection implementation,
overriding some methods.
4) Create an implementation of Connection wrapping the
problematic Connection.
ABOUT US
blog
GOT THE ANSWER?
Look for the answer in the next issue. Or submit your own code challenge!