aspose file tools*
The moose likes Servlets and the fly likes which method is better.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "which method is better.." Watch "which method is better.." New topic
Author

which method is better..

Ali Haider
Greenhorn

Joined: Feb 21, 2001
Posts: 25
hi all,
I'm using jrun for jsp. Which method should I use to open database connection. Should I open the db connection once in a global.jsa file or should I open it @ every page. that'll be too heavy. I want to develop the project independent from jrun. which method should I use, which'll allow me to deploy the project later easily on any jsp server. I used the jdbc bean but I have to open the connection on every page. any suggestions to which method should I use would be highly appreciated.
Thanx
bye
Ali
manoj bagul
Greenhorn

Joined: Sep 20, 2001
Posts: 11
Hello Ali.
One way is that u can open a connection when u login to the system and put that database connection object inside the session and retrive whenever u want.
Also when u logout close the database connection.
sanjay yermalkar
Greenhorn

Joined: Sep 19, 2001
Posts: 23
I suggest keeping Jsp free from any database connection. That keeps it more maintanaible and can be easily given to a page designer who doesn't understand Java!
I'll rather keep database connection task bound. Let it be in a method (in a bean), which is actually doing the task of dealing with database. I'll not keep the connection open for all the time user is logged in. What if the user is just reading Help pages? :-)

[This message has been edited by sanjay yermalkar (edited September 20, 2001).]
Ali Haider
Greenhorn

Joined: Feb 21, 2001
Posts: 25
Hi Manoj,
Thanks manog. I got wat U meant but thing is that the website doesn't have any login session. its not like when somebody logs in and then uses the website. I can open the connection on the first page of the website. but wat if the user didn't get in from the first page. if he'll directly jump to the second page then there'll not exist any db connection which will show an error. One solution is that I'll check on every page that theres exist db connection , if there isn't then I'll create one else use the existing one. But for that I'll have to write this code on every page. I don't want to do that cuz there r so many pages. doesn't there exist a global.asa type thing, as in asp. In Jrun, I can use global.jsa but then I'll have to deploy the project on a jrun webserver on web. I don't want to do that. Any other sugguestions r most welcome.
Thanks
Ali
Originally posted by manoj bagul:
Hello Ali.
One way is that u can open a connection when u login to the system and put that database connection object inside the session and retrive whenever u want.
Also when u logout close the database connection.

Ali Haider
Greenhorn

Joined: Feb 21, 2001
Posts: 25
Hi Sanjay,
Wouldn't it be too much resource taking to open the db connection on every page.
Originally posted by sanjay yermalkar:
I suggest keeping Jsp free from any database connection. That keeps it more maintanaible and can be easily given to a page designer who doesn't understand Java!
I'll rather keep database connection task bound. Let it be in a method (in a bean), which is actually doing the task of dealing with database. I'll not keep the connection open for all the time user is logged in. What if the user is just reading Help pages? :-)

[This message has been edited by sanjay yermalkar (edited September 20, 2001).]

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Ali, it seems from your postings that you want to instantiate a single database connection that can be reused by any page in your web application. Here's my suggestion:
(1) Create a servlet and override its init() method, or create a JSP file and override its jspInit() method. The overriden method should open a database connection and store it as a servlet context attribute named, for example, "con".
(2) Configure this servlet or JSP page with the <load-on-startup> tag in the web.xml file.
(3) Then, on any given JSP page in this web application, application.getAttribute("con") will return your connection object.
This way, only one database connection is opened when the web application is loaded, and this connection can be reused as many times as needed by any page in the web context.

------------------
Miftah Khan
- Sun Certified Programmer for the Java 2 Platform
- Sun Certified Web Component Developer for the J2EE Platform
Yogen Vadnere
Ranch Hand

Joined: Sep 20, 2001
Posts: 58
I agree with u all, but here is my opinion...
Create the connection per method in ur class and close the connection at the end of the method.
Then in this case u dont need to change the JSPs
Example
pulic Class A{
public void methodA(){
Connection con = new Connection() ;//obj to get the connection from pool
try{
//ur code
}catch(){
//handle exception
}finally{
con.close()
}
}
}

Correct me if wrong!!!


Yogen Vadnere
dotJ Software
Greenhorn

Joined: Sep 20, 2001
Posts: 3
Most of these suggestions are way off. You should never "cache" open database connections in your applications. Keeping an open connection to the database (per user) will kill the scalability of your application.
The approach you should take is to call a JavaBean's method from JSP that will execute some SQL. The method will open a connection, issue the SQL, and then close the connection upon every method call.
Yes, opening a connection to the database is an expensive operation, but cleaning up and managing connections takes higher priority. If you are concerned about the overhead of opening a live connection every time the JavaBean's method is called, look for a connection pool class that will optimize SQL connections for you.
In fact, most commercial servlet containers provide such software. I know that JRun does for a fact.
The code would look like the following:
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/pubs");
Connection con = ds.getConnection();
... issue SQL ...
con.close();
Of course, wrap all of this in try/catch for proper error handling.

Ali Haider
Greenhorn

Joined: Feb 21, 2001
Posts: 25
Thank U all for the suggestions. I'm now using the first method, which is, opening the connection at every request.
Regards
@li
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: which method is better..