由于没有发现潜在的递归导致MySQL链接数溢出:MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connec

DAOProxy的代码:下面代码中红色高亮的就是出问题的地方,DAOFactory中会构造一个PersonDAOProxy,调用listPersons或者addPerson显然会导致递归,从而导致MySQL链接数溢出!将高亮处的代码修改为:dao.listPersons()、dao.addPerson(person)即可!

public class PersonDAOProxy implements IPersonDAO {
    
    private DatabaseConnection conn = null;
    private IPersonDAO dao = null;
    
    public PersonDAOProxy() throws NamingException, SQLException {
        conn = DatabaseConnection.newInstance();
        dao = new PersonDAOImpl(conn.getConnection());
    }
    
    @Override
    public List<Person> listPersons() throws SQLException {
        List<Person> persons = null;
        try {
            persons = DAOFactory.getIPersonDAOInstance().listPersons();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            conn.close();
        }
        return persons;
    }
    
    @Override
    public void addPerson(Person person) throws SQLException {
        try {
            DAOFactory.getIPersonDAOInstance().addPerson(person);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            conn.close();
        }
    }
    
}

 

posted @ 2014-03-24 16:25  rldts  阅读(490)  评论(0编辑  收藏  举报