• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Open connection issues

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brief Overview of my code:

I have the following Java process which is listening to MyMessageQueue queue of ActiveMQ.
As soon as it gets the message in the format userName#employeeID#companyID#projectID#clientTitle , I am extracting and using in my code below.

The code keeps on sending message in the format userName#employeeID#companyID#projectID#clientTitle back to ActiveMQ until it sees
if (receivedStatus.equals("COMPLETE")) { and sends out an email.

The problem I am facing is that after the whole process(which sometime could take few mins or 30 mins) is finished I am seeing open inactive connections. Basically my DBA is complaining about connections which are not closed. The connection count is
increasing after each use of this process. Any idea what's wrong with the connections in my code below?





 

 
Marshal
Posts: 26131
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start I would advise closing the connection in a finally-clause.
 
Jack Tauson
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:For a start I would advise closing the connection in a finally-clause.



You mean something like the following at the following location:

 

// if there are any problems close the connection and it will be re-created next time
             if (connection != null) {
                 connection.close();
              }
              connection = null;
           }
        }
     } catch (Throwable th) {
        th.printStackTrace();
     }
    finally {
    if(connection != null) {
     connection.close();
    }
     }
  }

 
Paul Clapham
Marshal
Posts: 26131
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't really say. I don't know what the method is supposed to do, although I think it's supposed to handle a single String. And it's quite hard to tell what it is doing because the indentation is all over the place and I can't tell what code is in what code blocks. And you're catching the exception twice, which I can't really follow.

The other thing is, it looks to me like maybe the code is designed so that it may return from that method without closing the connection. Seems to me that's a possible cause of orphaned connections -- is that really necessary? Why not have the method start by getting a connection, then use it, then close it when you're finished? That would be where the finally block is used.
 
reply
    Bookmark Topic Watch Topic
  • New Topic