release connection in servlet.

Discussion in 'Java' started by finecur, Nov 15, 2008.

  1. finecur

    finecur Guest

    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
     
    finecur, Nov 15, 2008
    #1
    1. Advertising

  2. finecur

    Arne Vajhøj Guest

    finecur wrote:
    > 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
     
    Arne Vajhøj, Nov 15, 2008
    #2
    1. Advertising

  3. finecur

    Pushkaraj Guest

    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
     
    Pushkaraj, Nov 15, 2008
    #3
  4. On Nov 14, 11:20 pm, Pushkaraj <> wrote:
    > 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
     
    Owen Jacobson, Nov 15, 2008
    #4
  5. finecur

    finecur Guest

    On Nov 14, 5:04 pm, finecur <> wrote:
    > 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
     
    finecur, Nov 15, 2008
    #5
  6. finecur

    Lew Guest

    finecur wrote:
    > 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.

    --
    Lew
     
    Lew, Nov 16, 2008
    #6
  7. finecur

    Arne Vajhøj Guest

    finecur wrote:
    > 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
     
    Arne Vajhøj, Nov 16, 2008
    #7
  8. finecur

    Pushkaraj Guest

    > 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..
     
    Pushkaraj, Nov 17, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    4
    Views:
    2,423
    Andy Fish
    Dec 17, 2003
  2. Sean Clarke
    Replies:
    1
    Views:
    1,900
    Sudsy
    Jan 7, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    2,018
    Jack Jia
    Apr 4, 2004
  4. chamikara
    Replies:
    1
    Views:
    49,876
  5. DiscoStu
    Replies:
    0
    Views:
    506
    DiscoStu
    Jul 8, 2004
Loading...

Share This Page