X-Draft-From: ("nnml+mail:apache.turbine" 21343)
To: "Turbine Developers List" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Subject: Re: [PATCH] Standardizing OM for throwing TorqueException
References: <0E428003BBF3D511873100104B9821CB71BF@PROWLER>
Gcc: nnfolder+archive:mail.sent
Mime-Version: 1.0 (generated by tm-edit 1.7)
Content-Type: text/plain; charset=US-ASCIIHere's a version of TorqueException integration (using Mathieu's patch as a base) which applies against current CVS HEAD. I had to re-do the BasePeer patch (which took a long time, damn it ;-). Dan Index: java/org/apache/torque/Torque.java =================================================================== RCS file: /home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/Torque.java,v retrieving revision 1.43 diff -u -r1.43 Torque.java --- java/org/apache/torque/Torque.java 6 Feb 2002 15:18:56 -0000 1.43 +++ java/org/apache/torque/Torque.java 26 Feb 2002 18:48:27 -0000 @@ -54,8 +54,7 @@ * <http://www.apache.org/>. */ -import java.io.File; -import java.io.FileInputStream; +import java.io.*; import java.util.HashMap; import java.util.Enumeration; import java.util.Iterator; @@ -154,14 +153,16 @@ * initialize Torque * * @see org.apache.stratum.lifecycle.Initializable + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. */ - public void initialize() throws Exception + public void initialize() throws TorqueException { // FIX ME!! // duplicated code init(Configuration) if (configuration == null) { - throw new Exception("Torque cannot be initialized without " + + throw new TorqueException("Torque cannot be initialized without " + "a valid configuration. Please check the log files " + "for further details."); } @@ -220,8 +221,10 @@ * configure torque * * @see org.apache.stratum.lifecycle.Configurable + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. */ - public void configure(Configuration config) throws NestableException + public void configure(Configuration config) throws TorqueException { configuration = config; } @@ -230,26 +233,37 @@ * Initialization of Torque with a properties file. * * @param configFile The absolute path to the configuration file. + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. */ public static void init(String configFile) - throws Exception + throws TorqueException { - Configuration c = (Configuration)new PropertiesConfiguration(configFile); - init(c); + try + { + Configuration c = (Configuration)new PropertiesConfiguration(configFile); + init(c); + } + catch (IOException e) + { + throw new TorqueException(e); + } } /** * Initialization of Torque with a properties file. * * @param c The Torque configuration. + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. */ public static void init(Configuration c) - throws Exception + throws TorqueException { Torque.setConfiguration(c); if (configuration == null) { - throw new Exception("Torque cannot be initialized without " + + throw new TorqueException("Torque cannot be initialized without " + "a valid configuration. Please check the log files " + "for further details."); } @@ -477,7 +491,7 @@ * Returns the default database map information. * * @return A DatabaseMap. - * @throws TorqueException Any exceptions caught during procssing will be + * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static DatabaseMap getDatabaseMap() @@ -493,8 +507,8 @@ * @param name The name of the database corresponding to the * <code>DatabaseMap</code> to retrieve. * @return The named <code>DatabaseMap</code>. - * @throws TorqueException Any exceptions caught during procssing - * will be rethrown wrapped into a <code>TorqueException</code>. + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. */ public static DatabaseMap getDatabaseMap(String name) throws TorqueException @@ -527,6 +541,8 @@ * * @param name The name of the database to map. * @return The desired map. + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. */ private static final DatabaseMap initDatabaseMap(String name) throws TorqueException @@ -610,7 +626,7 @@ * rethrown wrapped into a TorqueException. */ public static DBConnection getConnection() - throws Exception + throws TorqueException { return getConnection(getDefaultDB()); } @@ -635,10 +651,17 @@ * rethrown wrapped into a TorqueException. */ public static DBConnection getConnection(String name) - throws Exception + throws TorqueException { - // The getPool method ensures the validity of the returned pool. - return getPool(name).getConnection(); + try + { + // The getPool method ensures the validity of the returned pool. + return getPool(name).getConnection(); + } + catch (Exception e) + { + throw new TorqueException(e); + } } /** @@ -660,7 +683,7 @@ String url, String username, String password) - throws Exception + throws TorqueException { ConnectionPool pool = null; url = url.trim(); @@ -675,7 +698,14 @@ pool = (ConnectionPool) pools.get(url + username); } - return pool.getConnection(); + try + { + return pool.getConnection(); + } + catch (Exception e) + { + throw new TorqueException(e); + } } /** @@ -684,17 +714,23 @@ * * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. - * @exception Exception A generic exception. */ public static void releaseConnection(DBConnection dbconn) - throws Exception + throws TorqueException { if ( dbconn != null ) { ConnectionPool pool = dbconn.getPool(); if ( pools.containsValue( pool ) ) { - pool.releaseConnection( dbconn ); + try + { + pool.releaseConnection( dbconn ); + } + catch (Exception e) + { + throw new TorqueException(e); + } } } } @@ -708,7 +744,7 @@ * @param username The name of the database user. * @param password The password of the database user. * - * @throws Exception Any exceptions caught during processing will be + * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static void registerPool( String name, @@ -716,7 +752,7 @@ String url, String username, String password ) - throws Exception + throws TorqueException { /** * Added so that the configuration file can define maxConnections & @@ -746,7 +782,8 @@ * @param url The URL of the database to use. * @param username The name of the database user. * @param password The password of the database user. - * @exception Exception A generic exception. + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. */ public static void registerPool( String name, String driver, @@ -757,7 +794,7 @@ long expiryTime, long maxConnectionAttempts, long connectionWaitTimeout) - throws Exception + throws TorqueException { // Quick (non-sync) check for the pool we want. @@ -795,7 +832,7 @@ * rethrown wrapped into a TorqueException. */ public static DB getDB() - throws Exception + throws TorqueException { return getDB(getDefaultDB()); } @@ -809,9 +846,16 @@ * rethrown wrapped into a TorqueException. */ public static DB getDB(String name) - throws Exception + throws TorqueException { - return getPool(name).getDB(); + try + { + return getPool(name).getDB(); + } + catch (Exception e) + { + throw new TorqueException(e); + } } /////////////////////////////////////////////////////////////////////////// @@ -820,11 +864,12 @@ * This method returns the default pool. * * @return The default pool. - * @exception Exception A generic exception. + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. * @see #getPool(String name) */ private static ConnectionPool getPool() - throws Exception + throws TorqueException { return getPool(getDefaultDB()); } @@ -838,11 +883,11 @@ * * @param name The name of the pool to get. * @return The requested pool. - * - * @exception Exception A generic exception. + * @throws TorqueException Any exceptions caught during processing will be + * rethrown wrapped into a TorqueException. */ private static ConnectionPool getPool(String name) - throws Exception + throws TorqueException { if (name == null) { Index: java/org/apache/torque/util/BasePeer.java =================================================================== RCS file: /home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/util/BasePeer.java,v retrieving revision 1.25 diff -u -r1.25 BasePeer.java --- java/org/apache/torque/util/BasePeer.java 23 Feb 2002 05:49:20 -0000 1.25 +++ java/org/apache/torque/util/BasePeer.java 26 Feb 2002 18:48:33 -0000 @@ -61,9 +61,11 @@ import com.workingdogs.village.Record; import com.workingdogs.village.Schema; import com.workingdogs.village.TableDataSet; + import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.FilterOutputStream; +import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; import java.math.BigDecimal; @@ -71,6 +73,7 @@ import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; import java.util.Date; import java.util.Enumeration; @@ -78,6 +81,8 @@ import java.util.Iterator; import java.util.List; import java.util.Vector; + +import org.apache.torque.TorqueException; import org.apache.torque.om.NumberKey; import org.apache.torque.om.ObjectKey; import org.apache.torque.om.SimpleKey; @@ -94,11 +99,12 @@ import org.apache.torque.util.Query; import org.apache.torque.util.SqlExpression; -//!! no good. -import org.apache.commons.util.StringStack; -import org.apache.torque.TorqueException; import org.apache.log4j.Category; +// StringStack has been moved from util to collections (dlr) +import org.apache.commons.util.StringStack; +//import org.apache.commons.collections.StringStack; + /** * This is the base class for all Peer classes in the system. Peer * classes are responsible for isolating all of the database access @@ -141,10 +147,10 @@ * * @param hash The Hashtable to convert. * @return A byte[] with the converted Hashtable. - * @exception Exception, a generic exception. + * @exception TorqueException */ public static byte[] hashtableToByteArray( Hashtable hash ) - throws Exception + throws TorqueException { Hashtable saveData = new Hashtable(hash.size()); String key = null; @@ -175,11 +181,44 @@ bos.flush(); byteArray = baos.toByteArray(); } + catch (Exception e) + { + throw new TorqueException(e); + } finally { - if (out != null) out.close(); - if (bos != null) bos.close(); - if (baos != null) baos.close(); + if (out != null) + { + try + { + out.close(); + } + catch (IOException ignored) + { + } + } + + if (bos != null) + { + try + { + bos.close(); + } + catch (IOException ignored) + { + } + } + + if (baos != null) + { + try + { + baos.close(); + } + catch (IOException ignored) + { + } + } } return byteArray; } @@ -318,15 +357,22 @@ * * @param dbName Name of database. * @return The DBConnection for the transaction. - * @exception Exception, a generic exception. + * @exception TorqueException */ public static DBConnection beginTransaction(String dbName) - throws Exception + throws TorqueException { DBConnection dbCon = Torque.getConnection( dbName ); - if ( dbCon.getConnection().getMetaData().supportsTransactions() ) + try { - dbCon.setAutoCommit(false); + if ( dbCon.getConnection().getMetaData().supportsTransactions() ) + { + dbCon.setAutoCommit(false); + } + } + catch (SQLException e) + { + throw new TorqueException(e); } return dbCon; @@ -338,10 +384,10 @@ * transactions, it only returns the connection. * * @param dbCon The DBConnection for the transaction. - * @exception Exception, a generic exception. + * @exception TorqueException */ public static void commitTransaction(DBConnection dbCon) - throws Exception + throws TorqueException { try { @@ -351,6 +397,10 @@ dbCon.setAutoCommit(true); } } + catch (SQLException e) + { + throw new TorqueException(e); + } finally { // Release the connection to the pool. @@ -365,10 +415,10 @@ * connection. * * @param dbCon The DBConnection for the transaction. - * @exception Exception, a generic exception. + * @exception TorqueException */ public static void rollBackTransaction(DBConnection dbCon) - throws Exception + throws TorqueException { try { @@ -383,6 +433,10 @@ + " database did not allow the operation to be rolled back."); } } + catch (SQLException e) + { + throw new TorqueException(e); + } finally { // Release the connection to the pool. @@ -400,34 +454,46 @@ * @param table The table to delete records from. * @param column The column in the where clause. * @param value The value of the column. - * @exception Exception, a generic exception. + * @exception TorqueException */ public static void deleteAll( DBConnection dbCon, String table, String column, int value ) - throws Exception + throws TorqueException { - Connection conn = dbCon.getConnection(); Statement statement = null; - try { + Connection conn = dbCon.getConnection(); statement = conn.createStatement(); StringBuffer query = new StringBuffer(); query.append( "DELETE FROM " ) - .append( table ) - .append( " WHERE " ) - .append( column ) - .append( " = " ) - .append( value ); + .append( table ) + .append( " WHERE " ) + .append( column ) + .append( " = " ) + .append( value ); statement.executeUpdate( query.toString() ); } + catch (SQLException e) + { + throw new TorqueException(e); + } finally { - if (statement != null) statement.close(); + if (statement != null) + { + try + { + statement.close(); + } + catch (SQLException ignored) + { + } + } } } @@ -440,12 +506,12 @@ * @param table The table to delete records from. * @param column The column in the where clause. * @param value The value of the column. - * @exception Exception, a generic exception. + * @exception TorqueException */ public static void deleteAll( String table, String column, int value ) - throws Exception + throws TorqueException { DBConnection dbCon = null; try @@ -466,10 +532,10 @@ * Criteria. * * @param criteria The criteria to use. - * @exception Exception, a generic exception. + * @exception TorqueException */ public static void doDelete(Criteria criteria) - throws Exception + throws TorqueException { DBConnection dbCon = null; String dbName = criteria.getDbName(); @@ -510,24 +576,32 @@ * * @param criteria The criteria to use. * @param dbCon A DBConnection. - * @exception Exception, a generic exception. + * @exception TorqueException */ public static void doDelete(Criteria criteria, DBConnection dbCon) - throws Exception + throws TorqueException { DB db = Torque.getDB( criteria.getDbName() ); DatabaseMap dbMap = Torque.getDatabaseMap( criteria.getDbName() ); - Connection connection = dbCon.getConnection(); + Connection connection = null; + try + { + connection = dbCon.getConnection(); + } + catch (SQLException e) + { + throw new TorqueException(e); + } // Set up a list of required tables and add extra entries to // criteria if directed to delete all related records. // StringStack.add() only adds element if it is unique. StringStack tables = new StringStack(); - Enumeration e = criteria.keys(); - while(e.hasMoreElements()) + Enumeration enum = criteria.keys(); + while (enum.hasMoreElements()) { - String key = (String)e.nextElement(); + String key = (String) enum.nextElement(); Criteria.Criterion c = criteria.getCriterion(key); String[] tableNames = c.getAllTables(); for (int i=0; i<tableNames.length; i++) @@ -627,9 +701,22 @@ rec.save(); } } + catch (Exception e) + { + throw new TorqueException(e); + } finally { - if (tds != null) tds.close(); + if (tds != null) + { + try + { + tds.close(); + } + catch (Exception ignored) + { + } + } } } } @@ -663,10 +750,10 @@ * @return An Object which is the id of the row that was inserted * (if the table has a primary key) or null (if the table does not * have a primary key). - * @exception Exception, a generic exception. + * @exception TorqueException */ public static ObjectKey doInsert(Criteria criteria) - throws Exception + throws TorqueException { DBConnection dbCon = null; ObjectKey id = null; @@ -732,11 +819,11 @@ * @return An Object which is the id of the row that was inserted * (if the table has a primary key) or null (if the table does not * have a primary key). - * @exception Exception, a generic exception. + * @exception TorqueException */ public static ObjectKey doInsert(Criteria criteria, DBConnection dbCon) - throws Exception + throws TorqueException { SimpleKey id = null; @@ -750,7 +837,7 @@ } else { - throw new Exception("Database insert attempted without " + + throw new TorqueException("Database insert attempted without " + "anything specified to insert"); } @@ -773,23 +860,30 @@ { if (keyGen == null) { - throw new Exception("IdGenerator for table '" + tableName + - "' is null"); + throw new TorqueException("IdGenerator for table '" + + tableName + "' is null"); } // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id // before the insert. if (keyGen.isPriorToInsert()) { - if ( pk.getType() instanceof Number ) + try { - id = new NumberKey( keyGen - .getIdAsBigDecimal(dbCon.getConnection(), keyInfo) ); + if ( pk.getType() instanceof Number ) + { + id = new NumberKey(keyGen.getIdAsBigDecimal + (dbCon.getConnection(), keyInfo)); + } + else + { + id = new StringKey(keyGen.getIdAsString + (dbCon.getConnection(), keyInfo)); + } } - else + catch (Exception e) { - id = new StringKey( keyGen - .getIdAsString(dbCon.getConnection(), keyInfo) ); + throw new TorqueException(e); } criteria.add( pk.getFullyQualifiedName(), id ); } @@ -803,27 +897,45 @@ Record rec = tds.addRecord(); BasePeer.insertOrUpdateRecord(rec, tableName, criteria); } + catch (Exception e) + { + throw new TorqueException(e); + } finally { if (tds != null) { - tds.close(); + try + { + tds.close(); + } + catch (Exception e) + { + throw new TorqueException(e); + } } } // If the primary key column is auto-incremented, get the id // now. - if ((keyGen != null) && (keyGen.isPostInsert())) + if (keyGen != null && keyGen.isPostInsert()) { - if ( pk.getType() instanceof Number ) + try { - id = new NumberKey( keyGen - .getIdAsBigDecimal(dbCon.getConnection(), keyInfo) ); + if ( pk.getType() instanceof Number ) + { + id = new NumberKey(keyGen.getIdAsBigDecimal + (dbCon.getConnection(), keyInfo)); + } + else + { + id = new StringKey(keyGen.getIdAsString + (dbCon.getConnection(), keyInfo)); + } } - else + catch (Exception e) { - id = new StringKey( keyGen - .getIdAsString(dbCon.getConnection(), keyInfo) ); + throw new TorqueException(e); } } @@ -837,12 +949,12 @@ * @param rec A Record. * @param tableName Name of table. * @param criteria A Criteria. - * @exception Exception, a generic exception. + * @exception TorqueException */ private static void insertOrUpdateRecord(Record rec, String tableName, Criteria criteria) - throws Exception + throws TorqueException { DatabaseMap dbMap = Torque.getDatabaseMap( criteria.getDbName() ); @@ -862,64 +974,71 @@ { obj = ((SimpleKey)obj).getValue(); } - if (obj == null) - { - rec.setValueNull(colMap.getColumnName()); - } - else if ( obj instanceof String ) - { - rec.setValue( colMap.getColumnName(), - (String)obj ); - } - else if ( obj instanceof Integer) - { - rec.setValue( colMap.getColumnName(), - criteria.getInt(key) ); - } - else if ( obj instanceof BigDecimal) - { - rec.setValue( colMap.getColumnName(), - (BigDecimal)obj ); - } - else if ( obj instanceof Boolean) - { - rec.setValue( colMap.getColumnName(), - criteria.getBoolean(key) ? 1 : 0); - } - else if ( obj instanceof java.util.Date) - { - rec.setValue( colMap.getColumnName(), - (java.util.Date)obj ); - } - else if ( obj instanceof Float) - { - rec.setValue( colMap.getColumnName(), - criteria.getFloat(key) ); - } - else if ( obj instanceof Double) - { - rec.setValue( colMap.getColumnName(), - criteria.getDouble(key) ); - } - else if ( obj instanceof Byte) - { - rec.setValue( colMap.getColumnName(), - ((Byte) obj).byteValue() ); - } - else if ( obj instanceof Long) + try { - rec.setValue( colMap.getColumnName(), - criteria.getLong(key) ); - } - else if ( obj instanceof Hashtable ) - { - rec.setValue( colMap.getColumnName(), - hashtableToByteArray( (Hashtable)obj ) ); + if (obj == null) + { + rec.setValueNull(colMap.getColumnName()); + } + else if ( obj instanceof String ) + { + rec.setValue( colMap.getColumnName(), + (String)obj ); + } + else if ( obj instanceof Integer) + { + rec.setValue( colMap.getColumnName(), + criteria.getInt(key) ); + } + else if ( obj instanceof BigDecimal) + { + rec.setValue( colMap.getColumnName(), + (BigDecimal)obj ); + } + else if ( obj instanceof Boolean) + { + rec.setValue( colMap.getColumnName(), + criteria.getBoolean(key) ? 1 : 0); + } + else if ( obj instanceof java.util.Date) + { + rec.setValue( colMap.getColumnName(), + (java.util.Date)obj ); + } + else if ( obj instanceof Float) + { + rec.setValue( colMap.getColumnName(), + criteria.getFloat(key) ); + } + else if ( obj instanceof Double) + { + rec.setValue( colMap.getColumnName(), + criteria.getDouble(key) ); + } + else if ( obj instanceof Byte) + { + rec.setValue( colMap.getColumnName(), + ((Byte) obj).byteValue() ); + } + else if ( obj instanceof Long) + { + rec.setValue( colMap.getColumnName(), + criteria.getLong(key) ); + } + else if ( obj instanceof Hashtable ) + { + rec.setValue( colMap.getColumnName(), + hashtableToByteArray( (Hashtable)obj ) ); + } + else if ( obj instanceof byte[]) + { + rec.setValue( colMap.getColumnName(), + (byte[])obj); + } } - else if ( obj instanceof byte[]) + catch (Exception e) { - rec.setValue( colMap.getColumnName(), - (byte[])obj); + throw new TorqueException(e); } shouldSave = true; } @@ -927,11 +1046,18 @@ if ( shouldSave ) { - rec.save(); + try + { + rec.save(); + } + catch (Exception e) + { + throw new TorqueException(e); + } } else { - throw new Exception ( "BasePeer.doInsert() - Nothing to insert" ); + throw new TorqueException("No changes to save"); } } @@ -942,7 +1068,7 @@ * @exception Exception Trouble creating the query string. */ public static String createQueryString( Criteria criteria ) - throws Exception + throws TorqueException { Query query = new Query(); DB db = Torque.getDB( criteria.getDbName() ); @@ -970,7 +1096,7 @@ String columnName = select.get(i); if (columnName.indexOf('.') == -1) { - throw getMalformedColumnNameException("select", columnName); + throwMalformedColumnNameException("select", columnName); } String tableName = null; selectClause.add(columnName); @@ -1063,11 +1189,11 @@ String join2 = (String)criteria.getJoinR().get(i); if (join1.indexOf('.') == -1) { - throw getMalformedColumnNameException("join",join1); + throwMalformedColumnNameException("join",join1); } if (join2.indexOf('.') == -1) { - throw getMalformedColumnNameException("join",join2); + throwMalformedColumnNameException("join",join2); } String tableName = join1.substring(0, join1.indexOf('.')); @@ -1122,7 +1248,7 @@ String orderByColumn = orderBy.get(i); if (orderByColumn.indexOf('.') == -1) { - throw getMalformedColumnNameException("order by",orderByColumn); + throwMalformedColumnNameException("order by",orderByColumn); } String table = orderByColumn.substring(0,orderByColumn.indexOf('.') ); // See if there's a space (between the column list and sort @@ -1198,10 +1324,10 @@ * * @param criteria A Criteria. * @return Vector of Record objects. - * @exception Exception, a generic exception. + * @exception TorqueException */ public static Vector doSelect(Criteria criteria) - throws Exception + throws TorqueException { Vector results = null; if (criteria.containsObjectColumn(criteria.getDbName())) @@ -1217,7 +1343,7 @@ { // make sure to return connection rollBackTransaction(dbCon); -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
