views:

49

answers:

4

Is there anyway to check the Connection leakage in J2EE application ? The application is running on my local machine. It uses MySQL database. User Enters his details into the Database.

In my opinion Connection leakage means not closing the Connection object properly. I am creating too many Database connections in my application. I want to check is there any connection leakage in database connections

+3  A: 

log4jdbc, a Java JDBC driver that can log SQL and/or JDBC calls for other JDBC drivers, has a logger which logs connection open and close events as well as dumping all open connection numbers. This is very useful for hunting down connection leak problems.

Another tool that you might want to check is ConnLeakFinder, a simple tool to pinpoint jdbc connection leaks in java code. I don't have any experience with it though.

Pascal Thivent
How does this compare to p6spy?
Thorbjørn Ravn Andersen
@Thorbjørn I consider it as a moderner alternative (the latest release of P6Spy was 7+ years ago): it uses SLF4J, it offers some features and configuration options that I like, it supports JDBC 4... I'm using it as replacement now.
Pascal Thivent
A: 

try using FindBug. It is a static code analysis tool and available as eclipse plugin as well as standalone application. Apart from Connection leackage it will find other problems in your application also

VinAy
+1  A: 

If you're using a Java EE app server, you should be able to configure it to check connections when they go out and reclaim stale connections when they don't come back.

Connection leakage is indeed a problem. I'd be worried if you had connection management scattered in so many places in the code that it was a big problem to find them all. I'd expect to see a Java EE connection pool that was used only within a well-defined persistence layer. Connections should be opened by a service layer that manages the transaction for that unit of work and closes it as soon as the use case is over, within method scope in a finally block.

If that's not true, I think it's time to refactor.

duffymo
A: 

Use a connection factory, for example:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionFactory {
    private static Connection connection;

    public static synchronized Connection getConnection() throws SQLException {
        if (connection == null || connection.isClosed()) {
            connection = DriverManager.getConnection("url");
        }
        return connection;
    }
}

This way you never leave unattended connections behind. Use a connection pool if you need more than one connection (for performance). Most appservers have a JDBC connection pool facility.

McPudding