Java-Ejb:检测到连接池的潜在语句泄漏

时间:2018-07-24 08:22:45

标签: java glassfish ejb connection-pooling connection-leaks

我使用Glassfish 3.1使用Java 8创建了一个Web应用程序。 在应用程序服务器上,我设置了此变量:

connection-leak-timeout-in-seconds=10
connection-leak-reclaim=true

启动我的应用程序时,出现以下警告:

检测到连接池myConnPool的潜在语句泄漏。...

我认为问题是秒级连接泄漏超时的10秒(查询需要更多时间,因此,如果我设置的值更大,警告将消失)。

我的疑问是,我是否编写了正确的代码来管理连接和相关语句。

这些是有问题的课程

@Singleton
@Startup
@DependsOn("DatabaseEjb")
public class TestEjb implements InterfaceTestEjb {

private int result;
@EJB 
public DatabaseEjb databaseEjb;

@PostConstruct
private void _init() {

    result = 0;
    Connection conn = null;
    MyDAO myDAO = null;

    try {
        conn = databaseEjb.getConnection();
        MyDAO = new MyDAO(conn);
        result = MyDAO.myMethod();
    } catch (Exception ex) {
        //my exception..
    }finally{
        if (conn != null){
            databaseEjb.releaseConnection(conn);
        }
    }
}

@Override
public void reload() {
    _init();
}
}
//********************************************
@Local
public interface InterfaceTestEjb {
public void reload();
}
//********************************************
@Singleton
@Startup
public class DatabaseEjb {

public Connection getConnection() {
    Connection connection=null;
    Context ctx = null;
    DataSource source = null;
    try {
        ctx = new InitialContext();
        source = (DataSource) ctx.lookup( "myDataSource" );
        connection = source.getConnection();
    } catch (NamingException ex) {
        connection = null;
    } catch (SQLException ex) {
        connection = null;
    }
    return connection;
}

public void releaseConnection(Connection connection){
    try {
        if (connection != null){
            connection.close();
        }
    } catch (SQLException ex) {
        //my exception...
    }
}
}
//********************************************
public class MyDAO extends MyDAOParent implements InterfaceMyDAO {

@EJB
InterfaceTestEjb interfaceTestEjb;

public MyDAO(Connection myConnection) {

    try {
        String naming = null;

        InitialContext context = new InitialContext();
        String contextName = (String) context.lookup("java:app/ContextName");

        naming = "java:global/" + contextName + "/TestEjb";
        interfaceTestEjb = (InterfaceTestEjb) new 
 InitialContext().lookup(naming);

    } catch (NamingException ex) {
        //my exception
    }
    this.connection = myConnection;
}

public int myMethod() {

    int result = 0;
    StringBuffer query = new StringBuffer();
    Statement statement = null;
    ResultSet rs = null;

    try {
        query.append("SELECT * FROM MyTable WHERE ID > 100;");
        //here below the exception: "A potential statement leak detected for connection pool myConnPool"
        statement = this.connection.createStatement();

        rs = statement.executeQuery(query.toString());
        while (rs.next()) {
            //data management
        }
        rs.close();

    } catch (Exception e) {
        //my exception
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
                //my exception
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ex) {
                //my exception
            }
        }
    }
    return result;
}

private void execute() throws SQLException {
    //data management
    interfaceTestEjb.reload();  
}   
}
//********************************************
public class MyDAOParent {
public Connection connection;  
}
//********************************************
public interface InterfaceMyDAO {
int myMethod();
}
//********************************************

非常感谢。

0 个答案:

没有答案