• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Connection to the database in Web-Application (JSP apps.)

I have some questions regarding how you maintain connection to the database when building web applications.

1.) What's the best practice to maintain connection to the database to get data from it?
Is it request-scope, session-scope, or application-scope?

2.) Currently I have a session-scope bean that maintains connections to the database. The problem is if I open the browser overnight and the came back in the morning, I get some CommunicationsLinkFailure exception. I think this is because the session has died. How should I handle this?

Thanks in advance for all the help.
0
rnicholus
Asked:
rnicholus
  • 3
  • 2
2 Solutions
 
Ajay-SinghCommented:
Best is: application scope. Or even better tomcat/web container global
connection pool. tomcat uses commons-dbcp which has automatic detection for
stale connections
 
http://commons.apache.org/dbcp/configuration.html
0
 
rnicholusAuthor Commented:
Could you please tell me the reason why you say application scope is the best?
0
 
Ajay-SinghCommented:
You end up creating less number of connections with database.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
rnicholusAuthor Commented:
I have tried having a bean with application-scope to maintain a connection to the database.
Then I went home and tried to open it at public computers.
It still gives me "communication links failure" error like what happen with #2 in my first post.

------------------------------------------------------------------
2.) Currently I have a session-scope bean that maintains connections to the database. The problem is if I open the browser overnight and the came back in the morning, I get some CommunicationsLinkFailure exception. I think this is because the session has died. How should I handle this?
------------------------------------------------------------------

0
 
KuldeepchaturvediCommented:
First of all, having connections live in session is not a good idea, secondly having a live connection sitting overnite is not a good idea either..

The best way to connect to the database is to use container managed connection pools. Search for JDBC connection pool & you will get information about it.

Now if your application NEEDs to be continued logged on even after overnite of inactivity then you will have to change some time out settings around your application for it.

I suggest you start with reading up connection pooling a bit and we can take it from there.
0
 
Ajay-SinghCommented:
which connection pool are you using?
Look at this on how to configure the pool
http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now