release connection in servlet.

F

finecur

Hi, I hope you guys can help me.

I have a servlet and inside it I have code like this:

public void doGet(HttpSevletRequest request, HttpServletResponse
request)
throws ServletException, IOException {

Connection conn = getConnectionFromPool();

doMyBigWork(); //this line takes long time

closeMyConnection(conn);
}

Since doMyBigWork() takes a long time, sometimes user did not wait
for my servlet return and click other pages. It ended up with
closeMyConnection(conn) never called. And I have more and more
connection lost and eventrually the connection pool got exhausted. How
can I handle this problem?

Thanks,

fm
 
A

Arne Vajhøj

finecur said:
I have a servlet and inside it I have code like this:

public void doGet(HttpSevletRequest request, HttpServletResponse
request)
throws ServletException, IOException {

Connection conn = getConnectionFromPool();

doMyBigWork(); //this line takes long time

closeMyConnection(conn);
}

Since doMyBigWork() takes a long time, sometimes user did not wait
for my servlet return and click other pages. It ended up with
closeMyConnection(conn) never called. And I have more and more
connection lost and eventrually the connection pool got exhausted. How
can I handle this problem?

Your servlet should not in any way be affected by the browser going to
another page.

But if an exception happen, then the connection would leak. Put
the closeConnection in a finally block.

And in general it is not good web page behavior to do stuff that
takes so long time.

Arne
 
P

Pushkaraj

Arne, I have a question here

When I was working in .ASP, I saw some API by which we can check if
the user is active.. means the browser connection is still active with
server for the current connection.
Do we have some thing like that in J2EE?

Pushkaraj
 
O

Owen Jacobson

Arne, I have a question here

When I was working in .ASP, I saw some API by which we can check if
the user is active.. means the browser connection is still active with
server for the current connection.
Do we have some thing like that in J2EE?

Pushkaraj

No, and it never actually worked in ASP, either. The underlying
protocol, HTTP, is stateless and connectionless. The closest you can
realistically get to determining if a user is still viewing your site
is to measure how long it's been since the last time someone from a
specific IP with a specific cookie set visited a URL - which is how
HTTP session management works in J2EE, .net, ASP classic, PHP, perl,
python, and every other language.

Look, you really can't do web development unless you're willing to put
in some effort to learn the tools, regardless of language. That
includes understanding what HTTP can and cannot give you.

As for your original question, close the connection handle in a
finally block, as Arne suggested. This goes for any kind of
"releasable" resource - acquire it, then immediately enter a try { }
block with a finally { } block that releases the resource. For
database connections, that looks like

public void doGet(HttpSevletRequest request, HttpServletResponse
request)
throws ServletException, IOException {
Connection conn = getConnectionFromPool();
try {
doMyBigWork(); //this line takes long time
} finally {
closeMyConnection(conn);
};
}

Unless the VM dies unexpectedly or you do some fairly complex and
wrong things, finally {} blocks will always execute, regardless of
whether code in the matching try {} completes or throws an exception.
They're the only reliable way to dispose of resources when you're
finished with them.

-o
 
F

finecur

Hi, I hope you guys can help me.

I have a servlet and inside it I have code like this:

 public void doGet(HttpSevletRequest request, HttpServletResponse
request)
    throws ServletException, IOException {

     Connectionconn = getConnectionFromPool();

     doMyBigWork(); //this line takes long time

     closeMyConnection(conn);

}

Since  doMyBigWork() takes a long time, sometimes user did not wait
for my servlet return and click other pages. It ended up with
closeMyConnection(conn) never called. And I have more and moreconnectionlost and eventrually theconnectionpool got exhausted. How
can I handle this problem?

Thanks,

fm

Since I put the close statment in the "finally" block, things becomes
much better. Thanks!!!

But I found another problem. When the user is waiting for my page to
come back, if he clicks the refresh button of the browser quickly for
a few times, I still got lost connections. That is because when I
call closeMyConnection(conn), it through a Exception saying that "the
connection is already closed". But I know it is not. I can see lost
connection number increase in Database Visualizer.

Could you help please?

Thanks again.

fm
 
L

Lew

finecur said:
But I found another problem. When the user is waiting for my page to
come back, if he clicks the refresh button of the browser quickly for
a few times, I still got lost connections. That is because when I
call closeMyConnection(conn), it through a Exception saying that "the
connection is already closed". But I know it is not. I can see lost
connection number increase in Database Visualizer.

Could you help please?

Guard against the same action repeating the same consequence, making the
action "idempotent" (GIYF, WIYF). One way is through the Token Pattern.

When you first render the page, put a token in the session indicating that the
page has not been submitted. When the first submission is received, delete
the token. If the token is already absent, this is a repeat submission, so
don't repeat the action.

This is similar in concept to idempotency in a financial application. The
bank certainly wouldn't want you to increase an account when one repeats an
identical deposit, and the account holder certainly doesn't want to decrease
their account when the bank repeats an identical withdrawal. Idempotency.

As to why your connections don't close when you explicitly close them, there
must be a programming error in there.
 
A

Arne Vajhøj

finecur said:
Since I put the close statment in the "finally" block, things becomes
much better.

It should.
But I found another problem. When the user is waiting for my page to
come back, if he clicks the refresh button of the browser quickly for
a few times, I still got lost connections. That is because when I
call closeMyConnection(conn), it through a Exception saying that "the
connection is already closed". But I know it is not. I can see lost
connection number increase in Database Visualizer.

This sounds to me as if you have have a field or static field with
the connection - and that it gets overwritten by the next request.

Make the connection a local variable in the method doing the work.

Arne
 
P

Pushkaraj

No, and it never actually worked in ASP, either. The underlying
protocol, HTTP, is stateless and connectionless. The closest you can
realistically get to determining if a user is still viewing your site
is to measure how long it's been since the last time someone from a
specific IP with a specific cookie set visited a URL - which is how
HTTP session management works in J2EE, .net, ASP classic, PHP, perl,
python, and every other language.


Thanks, Owen
I re-checked but got that, I wrongly interpreted the Teacher..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top