[OJB-developers] Handling Exception
Brought to you by:
thma
From: <ber...@wa...> - 2001-12-10 15:16:06
|
Hi, I have a problem with the way Obj handles exceptions. For example, when I do a getIteratorByQuery(), if something goes wrong with my database during a prepare statement the exception will be caught in StatementForClass::getPreparedStatement() that will print a trace on the console but I will receive a perfectly correct Iterator that will return no element. In the program itself there is no way to tell the difference between such an error condition and a query that will return no element, if the program runs stand alone, the only way is to redirect standard output and go and look for error message. I think it would be better for StatementForClass::getPreparedStatement to throw a new PersistenceBrokerException. That way the program could decide how to handle the error. Of course all the methods called along the chain should be modified so that they declare that they throw a PersistenceBrokerException and don't hide the exception themself. This should also make thinks easier for unit testing. I modified the different methods for getIteratorByQuery() as an example.I have added a //!! comment in the different places where I made a change. Of course many more methods do just the same as StatementForClass::getPreparedStatement() and many more changes will be needed. Because PersistenceBrokerException extends RuntimeException adding the throw clause isn't required but is it really a good idea to have all exception behaving as runtime exceptions. In the code there seems to be Methods that declare to throw the exception other that don't, sometime very similar methods like getIteratorByQuery() and getIteratorFromQuery(). If you think the change is worth it I could give you some help. Bertrand PersistenceBrokerImpl: public Iterator getIteratorByQuery(Query query) throws PersistenceBrokerException { Class itemClass = query.getSearchClass(); ClassDescriptor cld = m_DescriptorRepository.getDescriptorFor(itemClass); return getIteratorFromQuery(query, cld); } private Iterator getIteratorFromQuery(Query query, ClassDescriptor mif) //!! throws PersistenceBrokerException { //!! /* try { RsIterator iter = null; iter = new RsIterator(query, mif, this); return iter; } catch (Exception ex) { System.out.println("get iterator from query " + ex.getMessage ()); ex.printStackTrace(); } return null; */ RsIterator iter = null; iter = new RsIterator(query, mif, this); return iter; } RsIterator: public RsIterator(Query query, ClassDescriptor mif, PersistenceBrokerImpl broker) //!! throws PersistenceBrokerException { if (_debug) { System.out.println("RsIterator(" + query + ", " + mif + ")"); } m_rs = new JdbcAccess(broker).executeQuery(query, mif); m_broker = broker; m_mif = mif; itemProxyClass = mif.getProxyClass(); } JdbcAccess ResultSet executeQuery(Query query, ClassDescriptor mif) //!! throws PersistenceBrokerException { if (_debug) { System.out.println("executeQuery: " + query); } try { // if query has criteria, use them in where clause if (query.getCriteria() != null && !query.getCriteria().isEmpty ()) { String sql = SqlGenerator.getInstance ().getPreparedSelectStatement(query, mif); PreparedStatement stmt = broker.getStatementManager ().getPreparedStatement(mif, sql); broker.getStatementManager().bindStatement(stmt, query.getCriteria(), mif, 1); ResultSet rs = stmt.executeQuery(); // as we return the resultset for further operations, we cannot release the statement yet. // that has to be done by the JdbcAccess-clients (i.e. RsIterator, ProxyRsIterator and PkEnumeration.) return rs; } // if query has no criteria, perform select * from table else { Statement stmt = broker.getStatementManager ().getGenericStatement(mif); String sql = SqlGenerator.getInstance ().getSelectStatementDep(query, mif); ResultSet rs = stmt.executeQuery(sql); return rs; } } catch (Exception ex) { //!! System.out.println("execute query " + ex.getMessage()); // ex.printStackTrace(); // return null; throw new PersistenceBrokerException( ex) ; } } StatementManager public PreparedStatement getPreparedStatement(ClassDescriptor cds, String sql) throws PersistenceBrokerException { return getStatementsForClass(cds).getPreparedStmt(sql); } StatementForClass public PreparedStatement getPreparedStmt(String sql) //!!! throws PersistenceBrokerException { PreparedStatement stmt = null; try { stmt = prepareStatement(sql); } catch (java.sql.SQLException ex) { //!!! System.err.println(ex.getMessage()); ex.printStackTrace(); throw new PersistenceBrokerException( ex) ; } return stmt; } |