wood burning stoves 2.0*
The moose likes Servlets and the fly likes Help with DbConnectionBroker....Anyone else used it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Help with DbConnectionBroker....Anyone else used it?" Watch "Help with DbConnectionBroker....Anyone else used it?" New topic
Author

Help with DbConnectionBroker....Anyone else used it?

DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
I am building a site in servlets & read about the db connection broker here. Since I am concerned about multiple hits (a couple hundred at a time) at certain times of the day I decided to try this out but Im not sure if Ive got it set up right, as a matter of fact I know I dont because Im getting a NullPointerException at the line myBroker.freeConnection(conn);. I have written to Marc @ javaExchange but I also told him I would post this here in case he was too busy to get backj to me right away. Ive got a feeling I dont have the strings in the constructor set up right for my machine even though using normal methods of connecting this servlet runs perfectly. Any & all help would be greatly appreciated. Heres the code...BTW the HtmlTop & HtmlBot are just classes that draw my page layout.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import com.javaexchange.dbConnectionBroker.*;
import HtmlTop;
import HtmlBot;
public class pageServBU extends HttpServlet{
DbConnectionBroker myBroker;
public void init(ServletConfig config) throws ServletException{
super.init(config);
try {
myBroker = new DbConnectionBroker("org.gjt.mm.mysql.Driver", "jdbc:mysql://localhost:3306/intLovers", "usrname", "password", 2, 7, "C:\tomcat\\Servlet_errors.log", 1.0);
}
catch (IOException e5) { }
}
String pageNameA;
String pageName;
int pageType;
Statement stmt = null;
Connection conn = null;

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
PrintWriter out = res.getWriter();
try {
conn = myBroker.getConnection();
pageNameA = req.getParameter("pageName").toUpperCase();
pageName = pageNameA.substring(0, pageNameA.length() - 1);
pageType = Integer.parseInt(pageNameA.substring(pageNameA.length() - 1, pageNameA.length()));
stmt = conn.createStatement();
res.setContentType ("text/html");
HtmlTop top = new HtmlTop();
top.DrawTop(res, out, pageName, pageType);
String query = "SELECT pageText from sitePages where pageName = '"+pageName+"'";
ResultSet rs = stmt.executeQuery(query);
if (rs.next()){
String pageText = rs.getString(1);
out.println(pageText);
}
HtmlBot bot = new HtmlBot();
bot.DrawBot(res, out, pageName);

}
catch (SQLException e) {
System.err.println (e.getMessage());
System.exit(2);
}
catch (IOException e) {
System.err.println (e.getMessage () );
}
finally {
try{
if(stmt != null) {
stmt.close();
}
} catch(SQLException e){
};
myBroker.freeConnection(conn);
}
out.close();
res.getOutputStream().close();
}
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
doPost(req, res);
}
public void destroy ()
{
myBroker.destroy();
super.destroy();
}
}
BTW ....Im testing this on a win98 machine with tomcat & a mysql db.

[This message has been edited by DC Dalton (edited June 27, 2001).]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
JavaRanch has a Naming Policy In order to be entered in the drawing for the book giveaway, your name must meet this policy. Please reregister with a correct name.

And sorry but, I see one of my pet peeves.

You have no idea if you successfully created the Connection Broker or an exception was thrown! If you are going to catch it at least print the error message it returns.
I have successfilly used DbConnectionBroker to meet the criteria you discribe. Print out any error messages in this catch and let us know if it reveals anything.
------------------
Hope This Helps
Carl Trusiak, SCJP2
[This message has been edited by Carl Trusiak (edited June 27, 2001).]
[This message has been edited by Carl Trusiak (edited June 27, 2001).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
When your right your right.......I had copy & pasted that from their example code & didnt even notice that (note the others print errors) I agree why bother if your not gonna print them. Turned out the path to the error log was no good (missing an \ before the \tomcat (OOPS) fixed that & it runs fine with one minor exception. It seems to really take my machines resources down to a crawl....the page even scrolls weird. Should this be happening? Im running an AMD 900 with 750 meg of RAM......Ive never seen anything suck down the resources like this. BTW as far as the name goes I have been using this username for two months now (no one has complained yet) for no other reason than thats what everyone calls me, My best friends barely remember my real name & everything I do professionally is with DC. Been that way for 40 some years. If its a major problem Ill re-register but thats what everyone calls me.
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
One other little gremlin that has crept up into this servlet. Im getting an IllegalStateException everytime I hit a new page. Its not fatal, the pages still load. The error message is "Writer is already being used for this request" I havent a clue what this is all about. Im declaring PrintWriter out = res.getWriter(); & then I pass it to the HtmlTop (creates the top html layout) in the method call & then I pass it to the HtmlBot also in the method call(creates the bottom html layout). Is the DBConnectionBroker somehow holding on to the printWriter? Im using out.close(); to shut it down........any idea where the exception is coming from?......Thanks again
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
I narrowed the IllegalStateException problem down to the res.getOutputStream().close(); line. When looking in the servlet API it states you can only call getWriter() or getOutputStream() once in any given servlet. (Makes perfect sense) This came from the DBConnectionBroker Examples & I think I see what they were trying to do here but obviously its illegal. Is it really necessary to close the response object? Doesnt it basically die when the page is loaded & processing is finished? Thanks again for any info.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
You are getting the PrintWriter before setting the content type. The API's on this state
public void setContentType(java.lang.String type)
Sets the content type of the response being sent to the client. The content type may include the type of character encoding used, for example, text/html; charset=ISO-8859-4.
If obtaining a PrintWriter, this method should be called first.

------------------
Hope This Helps
Carl Trusiak, SCJP2
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
Hi Carl & thanks for your patience with this. Building the servlets themselves are easy but I guess these little "glitches" come from experience so I have to plow through them one by one.
I moved the setContentType to the line before I get the PrintWriter & added back in the res.getOutputStream().close(); after out.close(); Once again the Exception is there. I did move the PrintWriter out; declaration line up with the rest of the global declarations because of the out.close(); .....the compiler wasn't seeing "out"......Im at a a loss here. I am also getting an intermittent IO exception "connection reset by peer: socket write error" whenever I pull a page from the db that includes a hyperlink to a photo. (IE: out.println("<img border='0' src='/images/photo.jpg' width='1' height='1'>"); Ive done this this way because I would rather have the photos in a folder I can get to easily instead of having to go to the db. The photos show up fine but I do get this error once & a while, never on the same page so I cant narrow it down to any one pic. Im thinking this might be a tomcat - MySql problem on win98. Ever seen anything like this on a "real" server? Thanks again for your patience & time dealing with a "real site" servlet first timer!
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Can't say anything about what is causing the connectio reset error without setting things up the same. I see one more thing with your servlet
out.close();
res.getOutputStream().close();
The line
res.getOutputStream().close();
is totally unnecssary and another source of the IllegalStateException, remove that line of code and that error should go away.
What code are you using to retrieve the page from the database?

------------------
Hope This Helps
Carl Trusiak, SCJP2
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
I thought that line was relatively useless. I got that from the examples that come with the DBConnectionBroker & yes when I pull it out the IllegalStateException goes away. As far as the Connection reset error Im really at a loss. It happens when an image tag (not an image) is printed with the db text. I also got it today from this line, which is in the HtmlTop class (create the top page layout) out.println("<LINK REL='stylesheet' TYPE='text/css' HREF='/examples/Styles/ILstyles.css'>");
This is just code that is creating the html layout of the page etc.
These are the lines pulling the info from the db:
String query = "SELECT pageText from sitePages where pageName = '"+pageName+"'";
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
String pageText = rs.getString(1);
out.println(pageText);
}
The db info is nothing more than the article & it's supporting css tags but some pages do have lines that write an image tag as such:
<img border="0" src="/examples/photos/flexo.jpg" width="170" height="288" alt="flexo photo" align="left">
When one of these is written & the browser pulls the image into the page is when Im getting the Exception. Here is the exact exception from tomcat:
Ctx( /examples ): IOException in: R( /examples + /images/flexo.jpg + null) Connection reset by peer: socket write error
I hope this makes some sense of it for you. As I said in the other post everything is loading fine (images & all) but I just can't leave it throwing an exception everytime a user clicks on a page.......
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Yes, these are 404 errors. You have the incorrect URI(URL) fragment for these resources. I take it from these, you are running your servlet from the example webapp under Tomcat. Remove the /example from the URI Fragment and this error too shall pass.
------------------
Hope This Helps
Carl Trusiak, SCJP2
[This message has been edited by Carl Trusiak (edited June 28, 2001).]
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
Well part right & part not but none the less you have solved the problem. Taking the /examples out did in fact remove the error but also removed the pictures.....ssoooo what I did was put the entire path to the pic in, errors gone pics OK.......thanks a million fold for your patience & knowledge.....this site is the greatest!
 
jQuery in Action, 2nd edition
 
subject: Help with DbConnectionBroker....Anyone else used it?
 
Similar Threads
Help !!!!!!! Connection problems from you know where!
Servlets, Sessions & Thread Safety??
Problem with DbConnectionBroker
can anyone help me using jspsmartupload to upload an image???
FileReader - Servlet Problem.....Cant find file