File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Would someone please decipher a  line of code for me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Would someone please decipher a  line of code for me" Watch "Would someone please decipher a  line of code for me" New topic

Would someone please decipher a line of code for me

Paul Lusk
Ranch Hand

Joined: Aug 29, 2009
Posts: 34
Thanks in advance for even looking at this request. The line is from the code example listed in the article " JDBC Connection Pooling Best Practices" by David Murphy, JNetDirect
I have listed whole the method definition below to provide some context, but the line in question is the 3rd line. I see that it is an assignment of everything to the right of "=" to a boolean variable called poolingEnabled and I recognize the "==" operator as being a comparison of the string variable queryStr with the ORed expressions to the right of the equality operator, but it gets a bit complicated figuring out the possible values of " !queryStr.equals("disablePooling") ". So if someone could explain that bit , I would sure appreciate it because I would really like to understand what is going on rather than just accept that it works. Thanks again. PL

public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String queryStr = req.getQueryString();
boolean poolingEnabled = queryStr == null || !queryStr.equals("disablePooling");

PrintWriter out = res.getWriter();
out.println("<br>PooledConnectionCount:"+pooledCount+", nonPooledConnectionCount:"+nonPooledCount+"<br>");
if (pooledDuration > 0 && nonPooledDuration > 0) {
"Average pooled response:"+pooledDuration/pooledCount+
"Average non pooled response:"+nonPooledDuration/nonPooledCount);

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

The "queryStr == null" is to make sure that "queryStr.equals" is not going to throw a NullPointerException. If it's null, the string value is not evaluated, pooling is enabled. If it's not null, the string value is evaluated, and pooling is enabled is the string is not equal to "disablePooling".

Would you understand it better if it were written :

or even simplier

[My Blog]
All roads lead to JavaRanch
Paul Lusk
Ranch Hand

Joined: Aug 29, 2009
Posts: 34
Yes Christphe, I do see it better both ways you demonstrated, The First way is the easiest to logically folllow ( for me ) But I do see that the 2nd way does the same thing with less coding. S oto summarize the absence of parameter string ( null ) will default to connection pooling and connection pooling must be explicitly turned off by including the disablePooling parameter.
Thanks so much for answering my request . PL
P.S. If you have just another minute or two , Please let me know if the following supposition is true. I am thinking that as far as the DB Server is concerned , any connections in the pool are indeed open even though not actively working for any client and further that any client taking a connection from the pool must have already passed some type of authentcation. Thanks again
I agree. Here's the link:
subject: Would someone please decipher a line of code for me
It's not a secret anymore!