Aditya Dhekney

Greenhorn
+ Follow
since Jan 06, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Aditya Dhekney

Originally posted by Mike Curwen:
Yes, you're creating a single connection object every time you call getConnection() so this is not a very good Pool. Not to mention the InitialContext.

Is there any reason you're not using DBCP that comes with Tomcat?

http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html


Hi,
I thought i was using the DBCP that comes with tomcat...i used that example to set everything up in tomcat actually. The code might be a bit misleading cause i'm not using "DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/jdbc/TestDB");"
in that example...but in the actual code i am (can't access the code from where i am right now...).
In any case, will this (pooling) work if i create a new ConnectionPool Servlet, with a public DataSource object, and the rest of the code (getting InitialContext, Initializing/associating the DataSource with the db.) within the init() method. Then, i am thinking of having another getConnection() method within the servlet that just does a ds.getConnection()and returns this where it is needed. I am not sure if i can return objects in a servlet though...if i can't, i am thinking of saving the connection in a javabean...but dont' know if that is a good idea.
Could i implement it in just a regular class (ie. not servlet) and have it inside it's init() method? Will this work the same way? I know that for a servlet, the init() method is only called once (when it is intially invoked)...but don't know if regular classes 1) have such a thing, and 2) if it would only be called once.
Could someone please help me clear up my confusion? I currently have an app that works great for a few hours, and then suddenly comes down to a screeching halt...and that requires me to restart tomcat!
Thanks in advance,
Aditya
20 years ago
Hi all,
I'm a bit confused about how connection pooling works with tomcat right now. I have what i think is connection pooling working right now, but have a few concerns.
The following is some code that i picked up from one of the sites with which i retreive a connection:
public class Pool {
/**
* Method Pool create a new connection pool
*/
public Pool() {
System.err.println("DBUtil instance created.");
}
/**
* Method getConnection.
* @return Connection New connection from the pool
*/
public static Connection getConnection() {
Connection conn = null;
try {
Context ctx = (Context) new InitialContext().
lookup("java:comp/env");
conn = ((DataSource) ctx.lookup("jdbc/db2")).
getConnection();
}
catch (Exception e) {
e.printStackTrace(System.err);
throw new ApplicationError(e);
}
return conn;
}
}

Everytime i need a connection, i use:
Connection conn=Pool.getConnection();
..and conn.close(); to return it to the pool.
Now, here is what i don't understand. Am i not creating a new Initial Context every time i call this? I know that creating an InitialContext is VERY expensive..and does this not create N number of connections in the pool everytime? I don't think what i'm doing is right, but if it is, i need some reassurance.
I thought that i should only be creating one InitialContext when the application is started. Now, for a jsp/servlet application, when/where should i be doing this? Can someone please explain to me what happens when i call the getConnection method above? Am i actually creating a new context everytime, or does tomcat have some sort of way of handling this?
Thanks in advance,
Aditya
20 years ago

Originally posted by Jeanne Boyarsky:
Aditya,
You may want to store the data in a 1D arraylist of value objects (getters and setters for each field), so the data makes sense. It will also be easier to track down problems and maintain.


How do i go about doing this? Can you please give me some code examples?
Thanks in advance,
Aditya
20 years ago
JSP
Hi everyone,
I have been trying to do what i thought would be a very simple thing...perform a search based on some criteria, and return the query result back to be displayed on a jsp page.
I have a jsp page where some dropdown lists are populated on entry to the page. This works fine. I want to (eventually!) perform the search based on what is selected in these dropdowns. When i click the search button, i invoke a servlet called "SearchInventory.java" which gets the result. I then store this result in an ArrayList object. I then set a bean property (bean name->SearchBean, property->inventory) from within the servlet, and redirect back to the search page. This works if the query only gets back one column.
Unfortunately, when i ask for more than one column in the query, i get a "java.lang.NullPointerException" error.
Here is my code for all segments:
--------------------------------------------------------
JSP:
<%@ page language="java" import="java.util.ArrayList,java.lang.*,java.sql.*,javax.sql.*,PopLists.PopInvLists,beans.SearchBean"%>

<%
SearchBean sbean=new SearchBean();
ArrayList myAry=sbean.getInventory();
int size=myAry.size();
%>
.
.
.
<%
for(int i=0;i<size;i++){
String pname=(String)myAry.get(i);
%>
...
<%}%>
-------------------------------------------------------
Servlet:
String QueryStr="select ProdName,ManufName from products";// where ProdID="+ProdID;
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(QueryStr);
while(rs.next()){
rowAry.add(rs.getString(1));
rowAry.add(rs.getString(2));
rowSetAry.add(rowAry.clone());
}
sbean.setInventory(rowSetAry);
req.getSession(true).setAttribute("s_resbean",sbean);
.
.
res.sendRedirect("/DBTest/SearchInventory.jsp");
-----------------------------------------------------------------------------------------------------
In the servlet, i have also tried the following method of redirecting back to jsp page:
String url="/SearchInventory.jsp";
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(url);
dispatcher.forward(req,res);
...this gives me a java.lang.ClassCastException
-------------------------------------------------------------------------------------------------------
here is my "bean" class:
public class SearchBean extends HttpServlet{
private int searchFlag=0;
private static ArrayList inventory;
...
public static ArrayList getInventory(){
return inventory;
}
public void setInventory(ArrayList rs){
this.inventory=rs;
}
---------------------------------------------------------------------------------------------------------
When i perform a query that selects just one column, and doesn't use any sort of search fields for selecting, it does seem to work. for eg, : "select ProdName from products" as the query will return the product names successfully.
Now, i realize what i'm doing is not correct....however, i don't know what the correct way of doing this is. Can someone please please show me how i should be going about doing all of this? It really shouldn't be difficult, but I just can't seem to get it to work.
Thanks in advance,
Aditya
20 years ago
JSP

Originally posted by David O'Meara:
You're writing an 's_resbean', which is a beans.SearchBean then you are trying to read it back as a ResultSet.
Can I say I hat ethe idea of passing database resources around in web-applications. You should have a level below which database operations exist, and not allow knowledge about the database to leak above this layer. Therefore you'd be better off convertind from database fields to custom Java objects as quickly as possible and forget about passing database objects around.
Dave


Ok...i'll try and put it in an ArrayList first...i'm assuming i send this back to the jsp page the same way i am the ResultSet. Is this true? I.E. set a bean, get info from bean in jsp.
20 years ago
JSP

Originally posted by Amit More:
Hi Aditya,
Can you provide the code for forwarding your request from servlet to jsp.
Because it does matter.
Are u using RequestDispatcher object or something different?
Let me know.


RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(url);
dispatcher.forward(req,res);
20 years ago
JSP
Hey all, i've been stuck on this for too long now...just trying to return a ResultSet from a servlet to jsp page.
Had a bunch of problems earlier...which i think were fixed but...now i get a "java.lang.NullPointerException" in my jsp page when i try to get elements from the ResultSet object.
Here is the latest version of my code:
---------------------------------------------------------------------------------
Servlet:
String QueryStr="select ProdName from products";
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(QueryStr); //get resultset
sbean.setInventory(rs); //set ResultSet in bean
req.getSession(true).setAttribute("s_resbean",sbean); //create session/request variable, set to bean
---------------------------------------------------------------------------------
Bean:
package beans;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import javax.sql.*;
public class SearchBean extends HttpServlet{
private int searchFlag=0;
private ResultSet inventory;
public SearchBean(){
}
public int getSearchFlag(){
return searchFlag;
}
public ResultSet getInventory(){
return inventory;
}
public void setInventory(ResultSet rs){
this.inventory=rs;
}
public void setSearchFlag(){
this.searchFlag=1;
}
}
--------------------------------------------------------------------------------------------------
jsp:
<%@ page language="java" import="java.lang.*,java.sql.*,javax.sql.*,PopLists.PopInvLists,beans.SearchBean"%>
<jsp:useBean scope="session" id="s_resbean" class="beans.SearchBean" />
...
<% ResultSet categories=PopInvLists.getCat();
ResultSet manuf=PopInvLists.getManuf();
ResultSet supplier=PopInvLists.getSupplier();
ResultSet cars=PopInvLists.getCars();
ResultSet search=(ResultSet)request.getAttribute("s_resbean");
%>
...
<% while(search.next()){
String pname=search.getString("ProdName");
}
%>
---------------------------------------------------------------------------------------------
It craps out when i try to loop through the "search" ResultSet.
I can loop through the rest of the ResultSets no problem....just this one doesn't work because it's set in a servlet, not a simple java class.
Just to clarify, i am populating some dropdown lists on entry to the screen, which the user will use to perform a search. Once the search btn is clicked, the servlet is called, gets the request info for the search, performs search, and returns the resultset to the original screen. I want to eventually display the result under the search criteria.
Someone....Please Please please tell me how to get this working...it should be very simple, but i just can't get it to work.
Thanks in advance,
Aditya
P.S.
Also, if i try:
ResultSet search=(ResultSet)session.getAttribute("s_resbean");
in the jsp page, i get a "java.lang.ClassCastException"
What exactly should i be doing to get this ResultSet back to my jsp page?
20 years ago
JSP