From: <svn...@os...> - 2012-03-03 14:14:51
|
Author: aaime Date: 2012-03-03 06:14:45 -0800 (Sat, 03 Mar 2012) New Revision: 38597 Modified: trunk/modules/library/referencing/src/main/java/org/geotools/referencing/factory/epsg/DirectEpsgFactory.java Log: Fixing build by working around a H2 bug, reworking some atrocious code handling resultset closing in the process Modified: trunk/modules/library/referencing/src/main/java/org/geotools/referencing/factory/epsg/DirectEpsgFactory.java =================================================================== --- trunk/modules/library/referencing/src/main/java/org/geotools/referencing/factory/epsg/DirectEpsgFactory.java 2012-03-03 11:04:54 UTC (rev 38596) +++ trunk/modules/library/referencing/src/main/java/org/geotools/referencing/factory/epsg/DirectEpsgFactory.java 2012-03-03 14:14:45 UTC (rev 38597) @@ -2492,6 +2492,7 @@ { ensureNonNull("code", code); CoordinateOperation returnValue = null; + ResultSet result = null; try { final String primaryKey = toPrimaryKey(CoordinateOperation.class, code, "[Coordinate_Operation]", "COORD_OP_CODE", "COORD_OP_NAME"); @@ -2510,8 +2511,8 @@ + " FROM [Coordinate_Operation]" + " WHERE COORD_OP_CODE = ?"); stmt.setInt(1, Integer.parseInt(primaryKey)); - ResultSet result = stmt.executeQuery(); - while (result.next()) { + result = stmt.executeQuery(); + while (hasNext(result)) { final String epsg = getString(result, 1, code); final String name = getString(result, 2, code); final String type = getString(result, 3, code).trim().toLowerCase(); @@ -2580,7 +2581,6 @@ try { num = Integer.parseInt(methodCode); } catch (NumberFormatException exception) { - result.close(); throw new FactoryException(exception); } isBursaWolf = (num>=BURSA_WOLF_MIN_CODE && num<=BURSA_WOLF_MAX_CODE); @@ -2655,7 +2655,6 @@ * to avoid loading the quite large Geotools's implementation of this factory, * and also because it is not part of FactoryGroup anyway. */ - result.close(); result = null; final PreparedStatement cstmt = prepareStatement("ConcatenatedOperation", "SELECT SINGLE_OPERATION_CODE" @@ -2709,7 +2708,6 @@ parameters.parameter("tgt_dim").setValue(targetCRS.getCoordinateSystem().getDimension()); } } catch (ParameterNotFoundException exception) { - result.close(); throw new FactoryException(Errors.format( ErrorKeys.GEOTOOLS_EXTENSION_REQUIRED_$1, method.getName().getCode(), exception)); @@ -2724,7 +2722,6 @@ } else if (isConversion) { expected = Conversion.class; } else { - result.close(); throw new FactoryException(Errors.format(ErrorKeys.UNKNOW_TYPE_$1, type)); } final MathTransform mt = factories.getMathTransformFactory().createBaseToDerived( @@ -2734,15 +2731,17 @@ mt, method, expected); } returnValue = ensureSingleton(operation, returnValue, code); - if (result == null) { - // Bypass the 'result.close()' line below: - // the ResultSet has already been closed. - return returnValue; - } } - result.close(); } catch (SQLException exception) { throw databaseFailure(CoordinateOperation.class, code, exception); + } finally { + if(result != null) { + try { + result.close(); + } catch(Exception e) { + // fine, we tried + } + } } if (returnValue == null) { throw noSuchAuthorityCode(CoordinateOperation.class, code); @@ -2750,6 +2749,22 @@ return returnValue; } + private boolean hasNext(ResultSet result) throws SQLException { + // this stuff works around a cross issue between h2 and hsql + // hsql does not have the isClosed method, h2 apparently caches + // and returns the ResultSet of a previous call even if the + // result was closed (crazy) + try { + return result.next(); + } catch(SQLException e) { + if(result.isClosed()) { + return false; + } else { + throw e; + } + } + } + /** * Creates operations from coordinate reference system codes. * The returned set is ordered with the most accurate operations first. |