• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

servlet destroy method and session

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello guys

iam facing a problem regarding one of my servlet

i use init() method to initialize the connection to the database and after that i use destroy method to disconnect from the database.
like this

destroy() {
try {

connection.close();
}
catch (Exception e){

out.println(e);
}

but the problem is after a couple of hours and i think when the destroy method executed any request to the database failed.
this is the exception

java.sql.SQLException: No operations allowed after connection closed.

now i dont really know the solution for this matter but if there is a way to solve it please dont hasitate to response

the second thing

how can i create a session to store radio button selection and if a user go out the radio buttons page and browse other pages, and if he get redirected to the radio button page .....he should see the same radio button he selected before.

thanks
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.)
Once the destroy method is called, the servlet won't accept any more requests without being re-initialized.

Could it be that the connection is timing out or being closed some other way?


2.)
To store values in session use request.getSession().setAttribute(key, object).
Tor retrieve; request.getSession().getAttribute(key).

You can read all about it here:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html
 
ali al
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks

so how can re-initialized the servlet.

or what should i do to avoid this problem

thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can hit it at all (and it sounds like you can) then either it's not being destroyed or the container is re-initializing it for you.
This is why I'm guessing that something else is closing those connections.

Put some debugging lines in the destroy method to find out if the container is even calling destroy. If not, then you know that's not the problem.
 
ali al
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ben is there a specific lenght of time until destroy can be called or it should be called directlly after finishing squance of the program
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ali,

you didn't answer Ben's this point.


Could it be that the connection is timing out or being closed some other way?



did you verify you are not closing connection some where else ? I think problem might be there

Shailesh
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the container calling 'destroy' is the cause of your problem.
The container won't call destroy until all active threads are complete.
Likewise, it will call init again before passing the servlet another request thread.
 
ali al
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah i am sure i didnt close it eccept in the destroy method

ok

could it be the server i uploaded the files to has some problems may be?

because i use the same servlet in my machine and it is working fine.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would put some debug statments in the "destroy" method so you can see if it's being called or not.

I would also check the documentation for the database you are using and the JDBC driver you are using to see if either will force a connection to expire or 'time out' after a period of time.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thought is thread collision.

If you only have one connection for the servlet, and one request has it tied up when another request comes in, will the second request wait for it or blow up?

If this is a small app with low traffic, you could stay with this design.
If it needs to scale at all, I would look into whatever connection pooling options you have available with your servlet container.
 
ali al
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is connection pooling?
 
ali al
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look i have got this error message

ben

and i think it is from the web server , what do you think:


HTTP Status 503 - Servlet org.apache.catalina.INVOKER.cw is currently unavailable

--------------------------------------------------------------------------------

type Status report

message Servlet org.apache.catalina.INVOKER.cw is currently unavailable

description The requested service (Servlet org.apache.catalina.INVOKER.cw is currently unavailable) is not currently available.


--------------------------------------------------------------------------------

Apache Tomcat/4.1.27
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ali al:
what is connection pooling?



Most servlet containers have the built in ability to generate a pool of database connections when your application starts up. Whenever your app needs a connection, it borrows one from the pool. As soon as your done with it, you close it and it will be returned to the pool. If all the connections are busy and another one is needed, the pool will create another one.

If your container doesn't provide this feature, you can download an implementation from:
http://jakarta.apache.org/commons/dbcp

You can also read more about it at that link.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ali al:
look i have got this error message
ben
and i think it is from the web server , what do you think:
HTTP Status 503 - Servlet org.apache.catalina.INVOKER.cw is currently unavailable
--------------------------------------------------------------------------------
type Status report
message Servlet org.apache.catalina.INVOKER.cw is currently unavailable
description The requested service (Servlet org.apache.catalina.INVOKER.cw is currently unavailable) is not currently available.
--------------------------------------------------------------------------------

Apache Tomcat/4.1.27



Are you using the invoker servlet?
Have you mapped your servlets in your deployment descriptor (web.xml) or are you calling them by typing ../servlet/{package}/{servlet-name}?

The invoker servlet has been deprecated and is commented out in all but the oldest releases of Tomcat. If you haven't already, map your servlets.
http://faq.javaranch.com/view?InvokerServlet
 
ali al
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ben

i found out that using connection pooling is the best solution.
 
reply
    Bookmark Topic Watch Topic
  • New Topic