aspose file tools*
The moose likes Servlets and the fly likes servlet destroy method and session 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 "servlet destroy method and session" Watch "servlet destroy method and session" New topic
Author

servlet destroy method and session

ali al
Greenhorn

Joined: May 02, 2005
Posts: 9
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
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
ali al
Greenhorn

Joined: May 02, 2005
Posts: 9
hey thanks

so how can re-initialized the servlet.

or what should i do to avoid this problem

thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: May 02, 2005
Posts: 9
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
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

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


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: May 02, 2005
Posts: 9
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: May 02, 2005
Posts: 9
what is connection pooling?
ali al
Greenhorn

Joined: May 02, 2005
Posts: 9
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: May 02, 2005
Posts: 9
thanks ben

i found out that using connection pooling is the best solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet destroy method and session