wood burning stoves*
The moose likes JDBC 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
Bookmark "Would someone please decipher a  line of code for me" Watch "Would someone please decipher a  line of code for me" New topic
Author

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();
res.setContentType("text/html");
out.println("<html><head><title>Orders</title></head><body>");
out.println("<br>PooledConnectionCount:"+pooledCount+", nonPooledConnectionCount:"+nonPooledCount+"<br>");
if (pooledDuration > 0 && nonPooledDuration > 0) {
out.println("<br>"+
"Average pooled response:"+pooledDuration/pooledCount+
"Average non pooled response:"+nonPooledDuration/nonPooledCount);
}


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
PL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Would someone please decipher a line of code for me
 
Similar Threads
session listeners
how to acces a remote servlet from a java client?????
Newbie: Need help with checkbox post parameters
login in to a mysql database
while (unsolved) {BrickWall + Head}