Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8649481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:34:21+00:00 2026-06-12T13:34:21+00:00

When I try to do more than one transaction in a JSF page, I

  • 0

When I try to do more than one transaction in a JSF page, I get the following error:

A potential connection leak detected for connection pool MSSQL. The stack trace of the thread is provided below : 
com.sun.enterprise.resource.pool.ConnectionPool.setResourceStateToBusy(ConnectionPool.java:324)
com.sun.enterprise.resource.pool.ConnectionPool.getResourceFromPool(ConnectionPool.java:758)
com.sun.enterprise.resource.pool.ConnectionPool.getUnenlistedResource(ConnectionPool.java:632)
com.sun.enterprise.resource.pool.AssocWithThreadResourcePool.getUnenlistedResource(AssocWithThreadResourcePool.java:196)
com.sun.enterprise.resource.pool.ConnectionPool.internalGetResource(ConnectionPool.java:526)
com.sun.enterprise.resource.pool.ConnectionPool.getResource(ConnectionPool.java:381)
com.sun.enterprise.resource.pool.PoolManagerImpl.getResourceFromPool(PoolManagerImpl.java:245)
com.sun.enterprise.resource.pool.PoolManagerImpl.getResource(PoolManagerImpl.java:170)
com.sun.enterprise.connectors.ConnectionManagerImpl.getResource(ConnectionManagerImpl.java:338)
com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:301)
com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:190)
com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:165)
com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:160)
com.sun.gjc.spi.base.DataSource.getConnection(DataSource.java:113)
cl.codesin.colegios.util.persistencia.DAOManejador.abrir(DAOManejador.java:126)

Please notice the last line I pasted:

cl.codesin.colegios.util.persistencia.DAOManejador.abrir(DAOManejador.java:126)

abrir does the following:

public void abrir() throws SQLException {
    try
    {
        if(this.con==null || this.con.isClosed())
            this.con = fuenteDatos.getConnection();
    }
    catch(SQLException e)
    {
        throw e;
    }
}

It works in a singleton DAO manager this way: the DAO manager has one instance of each DAO and manages a single connection that every DAO shares. When a DAO is requested, it does the following:

public DAORegion getDAOregion() throws SQLException {
        try
        {
            if(con == null) //con is the connection the DAO manager uses
            {
                this.abrir();
            }
        }
        catch(SQLException e)
        {
            throw e;
        }
        if(this.DAOregion==null)
        {
            this.DAOregion = new DAORegion(this.con);
        }
        return DAOregion;
    }

When closing a connection, the manager just calls to con.close() without anything else.

By the way, I have no persistence.xml since I’m working with JDBC.

What am I doing wrong? Thank you beforehand.

EDIT: By desactivating the leak detection from the Glassfish server I could avoid the exception, however I’m still getting a “Connection closed” error. Worst is, now I don’t know exactly where the error is being thrown.

EDIT 2: I changed my DAO manager again. Here’s the implementation.

public class DAOManejador {

    public static DAOManejador getInstancia() {
        return DAOManejadorSingleton.INSTANCIA;
    }

    //This is just a sample, every getDAOXXX works the same.
    public DAOUsuario getDAOusuario() throws SQLException {
        try
        {
            if(con == null)
            {
                this.abrir();
            }
        }
        catch(SQLException e)
        {
            throw e;
        }
        if(this.DAOusuario==null)
        {
            this.DAOusuario = new DAOUsuario(this.con, this.stmt, this.res);
        }
        return DAOusuario;
    }

    public void abrir() throws SQLException {
        try
        {
            if(this.con==null || this.con.isClosed())
                this.con = fuenteDatos.getConnection();
        }
        catch(SQLException e)
        {
            throw e;
        }
    }

    public void iniciaTransaccion() throws SQLException {
        try
        {
            con.setAutoCommit(false);
        }
        catch(SQLException e)
        {
            throw e;
        }
    }

    public void cierraTransaccion()  throws SQLException {
        try
        {
            con.setAutoCommit(true);
        }
        catch(SQLException e)
        {
            throw e;
        }
    }

    public void comprometer() throws SQLException {
        try
        {
            con.commit();
        }
        catch(SQLException e)
        {
            throw e;
        }
    }

    public void deshacer() throws SQLException {
        try
        {
            con.rollback();
        }
        catch(SQLException e)
        {
            throw e;
        }
    }

    public void cerrar() throws SQLException {
        try
        {
            if(this.stmt!=null && !this.stmt.isClosed())
                stmt.close();

            if(this.res!=null && !this.res.isClosed())
                this.res.close();

            if(this.con!=null && !this.con.isClosed())
                con.close();
        }
        catch(SQLException e)
        {
            throw e;
        }
    }

    public void comprometerYTerminarTransaccion() throws SQLException {
        try
        {
            this.comprometer();
            this.cierraTransaccion();
        }
        catch(SQLException e)
        {
            throw e;
        }
    }

    public void comprometerYCerrarConexion() throws SQLException {
        try
        {
            this.comprometer();
            this.cierraTransaccion();
            this.cerrar();
        }
        catch(SQLException e)
        {
            throw e;
        }
    }

    //Protegidos
    @Override
    protected void finalize() throws SQLException, Throwable
    {
        try
        {
            this.cerrar();
        }
        finally
        {
            super.finalize();
        }
    }

    //Private
    private DataSource fuenteDatos;
    private Connection con = null;
    private PreparedStatement stmt = null;
    private ResultSet res = null;

    private DAOUsuario DAOusuario = null;
    private DAORegion DAOregion = null;
    private DAOProvincia DAOprovincia = null;
    private DAOComuna DAOcomuna = null;
    private DAOColegio DAOcolegio = null;

    private DAOManejador() throws Exception {
        try
        {
            InitialContext ctx = new InitialContext();
            this.fuenteDatos = (DataSource)ctx.lookup("jndi/MSSQL");
        }
        catch(Exception e){ throw e; }
    }

    private static class DAOManejadorSingleton {
        public static final DAOManejador INSTANCIA;
        static
        {
            DAOManejador dm;
            try
            {
                dm = new DAOManejador();
            }
            catch(Exception e)
            { dm=null; }
            INSTANCIA = dm;
        }
    }

}

What I did now is to provide a single access point for every DAO. When a DAO wants to use a statement or a resource, they’ll all use the same one. When they need to open again one, the system does the following:

public abstract class DAOGenerico<T> {

    //Protected
    protected final String nombreTabla;
    protected Connection con;
    protected PreparedStatement stmt;
    protected ResultSet res;

    protected DAOGenerico(Connection con, PreparedStatement stmt, ResultSet res, String nombreTabla) {
        this.nombreTabla = nombreTabla;
        this.con = con;
        this.stmt = stmt;
        this.res = res;
    }

    //Prepares a query
    protected final void prepararConsulta(String query) throws SQLException 
    {            
        try
        {
            if(this.stmt!=null && !this.stmt.isClosed())
                this.stmt.close();
            this.stmt = this.con.prepareStatement(query);
        }
        catch(SQLException e){ throw e; }
    }

    //Gets a ResultSet
    protected final void obtenerResultados() throws SQLException {
        try
        {
            if(this.res!=null && !this.res.isClosed())
                this.res.close();
            this.res = this.stmt.executeQuery();
        }
        catch(SQLException e){ throw e; }
    }

}

And it still doesn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:34:23+00:00Added an answer on June 12, 2026 at 1:34 pm

    I tried not doing anything when closing the connection. I commented the code in the cerrar method, and for some reason, it works! Even when it’s a bad practice! Is it okay to keep it like that, or should I find a way to close a connection?

    Disregard this, I found what’s wrong. I hope someone can make good use of this in the future.

    The problem

    if(this.con==null || this.con.isClosed())
        this.con = fuenteDatos.getConnection();
    

    Each time I try to open a connection, I get a completely brand new connection. What’s the problem with this?

    public DAOUsuario getDAOusuario() throws SQLException {
        try
        {
            if(con == null)
            {
                this.abrir();
            }
        }
        catch(SQLException e)
        {
            throw e;
        }
        if(this.DAOusuario==null)
        {
            this.DAOusuario = new DAOUsuario(this.con, this.stmt, this.res);
        }
        return DAOusuario;
    }
    

    Only when I create a new instance of the DAO I assign it a new connection. What will happen in the following case then?

    DAOManejador daoManager = DAOManejador.getInstancia(); //Get an instance of the DAO manager
    daoManager.abrir(); //Open the connection
    DAOUsuario daoUser = daoManager.getDAOusuario(); //Get a DAOUsuario, a type of DAO. It'll have the same connection as the DAOManager, and it'll be stored in the instance of the DAO manager
    ... //Do database stuff
    daoManager.cerrar(); //Close the connection
    daoManager.abrir(); //Open the connection again. Note that this will be a new instance of the conection rather than the old one
    

    If, from here, you try to do database stuff, you’ll get a Connection closed error since daoUser will still hold the old connection.

    What I did

    I modified the DAO manager class. It no longer has a getDAOXXX() per DAO, but rather the following:

    public DAOGenerico getDAO(Tabla t) throws SQLException {
        try
        {
            if(con == null || this.con.isClosed())
            {
                this.abrir();
            }
        }
        catch(SQLException e)
        {
            throw e;
        }
        switch(t)
        {
            case REGION:
                return new DAORegion(this.con, this.stmt, this.res);
            case PROVINCIA:
                return new DAOProvincia(this.con, this.stmt, this.res);
            case COMUNA:
                return new DAOComuna(this.con, this.stmt, this.res);
            case USUARIO:
                return new DAOUsuario(this.con, this.stmt, this.res);
            case COLEGIO:
                return new DAOColegio(this.con, this.stmt, this.res);
            default:
                throw new SQLException("Se intentó vincular a una tabla que no existe.");
        }
    }
    

    Each time the user requests a DAO, it’ll ask the manager to return the correct type of DAO. But instead of storing each instance, the manager will create new instances depending on the current connection (con is the connection, stmt is a PreparedStatement and res is a ResultSet – they will be used so they can be closed when the manager closes the connection so nothing leaks). Tabla is an enum holding the current table names in the database so it can return the correct DAO. This worked with no problems whatsoever. The rest of the class is the same, so if you want to use it, just replace the DAOUsuario method with the one above and it should work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello guys i try to get an object global/persistent over more than one php
If I have more than one table on a page, and I try coloring
I want to pass more than one variable to other web pages. I try
When I try to add more than one element to a PriorityQueue in my
When i add more than one Fieldable to my Document i get a nullpointer
I am new to this and when I try to add more than one
For some reason I'm getting the error that More than one value for the
5 and I try to solve the more than one entity situation using a
I am trying to get my animation move in more than one generated path.
Whenever I try to create more than one table in actionscript, only the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.