Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Servlet release database Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet release database Connection" Watch "Servlet release database Connection" New topic
Author

Servlet release database Connection

carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

Hi i am calling a procedure to execute from servlet , As the procedure takes quite long hours approximately 2 hours my application gets hanged for 2 hours. Also when i close my application the procedure did not run to completion but exit in between. I want my servlet to work such that when i click button to execute the procedure the procedure should run in the background without locking the application Also when i give command to execute th eprocedure and close my application the procedure should run to completion .

Can anybody suggest me how this could be achieved.

This is something like executing a job in background without locking the front end screens, and i am stuck and could not find a solution.

Any suggestion is appreciated.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Start a new thread?

(Preferably using an existing job library.)
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

On starting a new thread will my application do not get locked? also if i close application(logout or abrubt shutting down incase like power cut off) will the newly started thread continue execution?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If the power is cut off it's highly unlikely *any* process on the machine will continue execution.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Due to the lack of power.
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

i have designed the servlet to run the procedure ...





but here the procedure run for the first time when i start my server and run the servlet , when i run the servlet again without restarting the server the control just go to the doGet method of the servlet and the procedure do not get executed.

Please suggets me how do i execute a procedure such that the front end of the application do not get locked. also even ifi close the application the procedure should continue running.

Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
ruquia tabassum wrote:

but here the procedure run for the first time when i start my server and run the servlet , when i run the servlet again without restarting the server the control just go to the doGet method of the servlet and the procedure do not get executed.



That's because the init() method won't be called for each request, it's called only once when the container is initializing the servlet!

ruquia tabassum wrote:
Please suggets me how do i execute a procedure such that the front end of the application do not get locked.


If you want to do something asynchronously in doGet(), you'll have to start the new thread in doGet()!

ruquia tabassum wrote:
also even ifi close the application the procedure should continue running.


By application are you referring to the browser..?


OCJP 6 (93%)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
but here the procedure run for the first time when i start my server and run the servlet , when i run the servlet again without restarting the server the control just go to the doGet method of the servlet and the procedure do not get executed.

init is executed once if the servlet gets put into service, and then not again. If you want to start the thread each time someone accesses the servlet, put the code into the doGet method. Make sure to check whether there's already a thread running if you do that, so that you don't end up with multiple threads.

Please suggets me how do i execute a procedure such that the front end of the application do not get locked.

A background thread like you're trying to implement accomplishes that.

also even ifi close the application the procedure should continue running.

If by "application" you mean the web app (or the servlet container itself) then that's not possible because the thread is part of it. But production servers and application rarely get shut down, and not without planning beforehand, so that shouldn't be much of a problem.

error == ""

Don't compare string using the == operators; that's what the equals method is for.


Ping & DNS - my free Android networking tools app
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

If you're kicking off a process that runs for several hours, you should seriously consider moving that process out of the web server entirely. The Tomcat server cannot shut down until ALL threads shutdown, so if you need to restart Tomcat for any reason - even one unrelated to the app in question - you'll have to forcibly kill the entire server.

There seems to be a lot of webapps that don't know how to shutdown these days. I stumbled over another one just yesterday.

To avoid holding the webserver hostage, consider moving the long-running process to an independent executable. I had one that could run 10 hours. I made it launchable, queryable, and controllable via RMI, although other mechanisms are also possible.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
 
subject: Servlet release database Connection