• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

which method is better..

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]


 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank U all for the suggestions. I'm now using the first method, which is, opening the connection at every request.
Regards
@li
 
I've been selected to go to the moon! All thanks to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic