I try to use connection pooling to make database connection and put the
connection object in ServletContext. Here is how my application work.
- user login through JSP and submit the request which handled by first
level servlet. In the init( ), I did:
ServletContext context = getServletContext( ), then make database
connection and put in the attribute
context.setAttribute("connection", connection");
- first level servlet then call second level servlet. In the second
servlet I try to getServletContext( ):
ServletContext context = getServletContext( );
When executing that line, I got the following error:
null
java.lang.NullPointerException
at
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:205)
at com.wg.hr.PersonnetWelcome.execute(PersonnetWelcome.java:37)
at com.wg.hr.Personnet.doGet(Personnet.java:67)
at com.wg.hr.Personnet.doPost(Personnet.java:85)
...
Any suggestion is appreciated
Thanks,
Andi
Geeta Ramani
<[EMAIL PROTECTED] To: [EMAIL PROTECTED]
OM> cc:
Sent by: "A mailing Subject: Re: Session & Database
connection
list for discussion
about Sun
Microsystem's Java
Servlet API
Technology."
<SERVLET-INTEREST@JAV
A.SUN.COM>
04/27/01 02:12 PM
Please respond to "A
mailing list for
discussion about Sun
Microsystem's Java
Servlet API
Technology."
Andi;
Every user who executes doGet will run in his/her own thread and so one
person's session/connection will never "get mixed up" in another person's
session or connection. ..So in fact you don't have to even add the
"synchronized(session) " bit. (It is automatically synchronized.)
However, getting and relasing a connection thsi way is not (generally) a
good
idea: you may want to use a connection pool (though the reasons have
nothing to
do with synchronization, but with performance, etc.). (You may want to
search
the archives under "Connection pool")
Also if this is confusing, maybe you should study how a servlet works (and
about its life cycle) in Jason's (or any other basic) book.
Good luck!
Geeta
Andi Setiyadi wrote:
> I have a question about database connection.
> In the code below, what happen when one request has just creating a
session
> for storing the connection, and the another request is about to execute
> session.removeAttribute that will kill the session? Is the session
unique
> for each request, means that when one user kill connection session will
not
> affect other user's connection session? Or all request share the same
> connection session?
> Is this a good way to maintain connection for each request? Why?
>
> Thank you. I appreciated any input.
>
> Andi Setiyadi
>
> Attached code:
>
> public void doGet(HttpServletRequest req, HttpServletResponse res) throws
> IOException, ServletException {
>
> HttpSession session = req.getSession(true);
> Connection conn;
>
> synchronized(session) {
> conn = (Connection) session.getAttribute("connection");
> if (conn == null) {
> ConnectionBean cBean = new ConnectionBean();
> conn = cBean.getConnection();
> session.setAttribute("connection", conn);
> }
> }
>
> // perform some tasks
> ...
>
> try {
> if (conn != null) {
> conn.close();
> }
> }
>
> catch(Exception e) { }
>
> session.removeAttribute("connection");
> }
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html