Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MODE-1861 Improved error handling and ensured result sets are always …

…closed
  • Loading branch information...
commit aee0a09d99f0502aa711da2dda4e6b51f8db6a5b 1 parent 382f738
@rhauch rhauch authored hchiorean committed
View
36 modeshape-jcr/src/main/java/org/modeshape/jcr/value/binary/Database.java
@@ -579,6 +579,7 @@ public static void executeUpdate( PreparedStatement sql ) throws BinaryStoreExce
* @throws BinaryStoreException
*/
public static InputStream asStream( ResultSet rs ) throws BinaryStoreException {
+ boolean error = false;
try {
boolean hasRaw = rs.first();
if (!hasRaw) {
@@ -586,7 +587,18 @@ public static InputStream asStream( ResultSet rs ) throws BinaryStoreException {
}
return rs.getBinaryStream(1);
} catch (SQLException e) {
+ error = true;
throw new BinaryStoreException(e);
+ } catch (RuntimeException e) {
+ error = true;
+ throw e;
+ } finally {
+ // Always close the result set ...
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ if (!error) throw new BinaryStoreException(e);
+ }
}
}
@@ -598,6 +610,7 @@ public static InputStream asStream( ResultSet rs ) throws BinaryStoreException {
* @throws BinaryStoreException
*/
public static String asString( ResultSet rs ) throws BinaryStoreException {
+ boolean error = false;
try {
boolean hasRaw = rs.first();
if (!hasRaw) {
@@ -605,7 +618,18 @@ public static String asString( ResultSet rs ) throws BinaryStoreException {
}
return rs.getString(1);
} catch (SQLException e) {
+ error = true;
throw new BinaryStoreException(e);
+ } catch (RuntimeException e) {
+ error = true;
+ throw e;
+ } finally {
+ // Always close the result set ...
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ if (!error) throw new BinaryStoreException(e);
+ }
}
}
@@ -617,13 +641,25 @@ public static String asString( ResultSet rs ) throws BinaryStoreException {
* @throws BinaryStoreException
*/
public static List<String> asStringList( ResultSet rs ) throws BinaryStoreException {
+ boolean error = false;
List<String> result = new ArrayList<String>();
try {
while (rs.next()) {
result.add(rs.getString(1));
}
} catch (SQLException e) {
+ error = true;
throw new BinaryStoreException(e);
+ } catch (RuntimeException e) {
+ error = true;
+ throw e;
+ } finally {
+ // Always close the result set ...
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ if (!error) throw new BinaryStoreException(e);
+ }
}
return result;
}
View
37 modeshape-jcr/src/main/java/org/modeshape/jcr/value/binary/DatabaseBinaryStore.java
@@ -153,14 +153,14 @@ public BinaryValue storeValue( InputStream stream ) throws BinaryStoreException
// check unused content
if (this.contentExists(key, UNUSED)) {
PreparedStatement sql = database.restoreContentSQL(key);
- Database.execute(sql);
+ Database.execute(sql); // doesn't produce a result set
return new StoredBinaryValue(this, key, temp.getSize());
}
// store content
try {
PreparedStatement sql = database.insertContentSQL(key, temp.getStream());
- Database.execute(sql);
+ Database.execute(sql); // doesn't produce a result set
return new StoredBinaryValue(this, key, temp.getSize());
} catch (Exception e) {
@@ -175,7 +175,7 @@ public BinaryValue storeValue( InputStream stream ) throws BinaryStoreException
@Override
public InputStream getInputStream( BinaryKey key ) throws BinaryStoreException {
ResultSet rs = Database.executeQuery(database.retrieveContentSQL(key, true));
- InputStream inputStream = Database.asStream(rs);
+ InputStream inputStream = Database.asStream(rs); // closes result set
if (inputStream == null) {
try {
throw new BinaryStoreException(JcrI18n.unableToFindBinaryValue.text(key, database.getConnection().getCatalog()));
@@ -190,7 +190,7 @@ public InputStream getInputStream( BinaryKey key ) throws BinaryStoreException {
public void markAsUnused( Iterable<BinaryKey> keys ) throws BinaryStoreException {
for (BinaryKey key : keys) {
PreparedStatement sql = database.markUnusedSQL(key);
- Database.executeUpdate(sql);
+ Database.executeUpdate(sql); // doesn't produce a result set
}
}
@@ -200,14 +200,14 @@ public void removeValuesUnusedLongerThan( long minimumAge,
// compute usage deadline (in past)
long deadline = now() - unit.toMillis(minimumAge);
PreparedStatement sql = database.removeExpiredContentSQL(deadline);
- Database.execute(sql);
+ Database.execute(sql); // doesn't produce a result set
}
@Override
protected String getStoredMimeType( BinaryValue source ) throws BinaryStoreException {
checkContentExists(source);
ResultSet rs = Database.executeQuery(database.retrieveMimeTypeSQL(source.getKey()));
- return Database.asString(rs);
+ return Database.asString(rs); // closes result set
}
private void checkContentExists( BinaryValue source ) throws BinaryStoreException {
@@ -225,21 +225,21 @@ private void checkContentExists( BinaryValue source ) throws BinaryStoreExceptio
protected void storeMimeType( BinaryValue source,
String mimeType ) throws BinaryStoreException {
PreparedStatement sql = database.updateMimeTypeSQL(source.getKey(), mimeType);
- Database.executeUpdate(sql);
+ Database.executeUpdate(sql); // doesn't produce a result set
}
@Override
public String getExtractedText( BinaryValue source ) throws BinaryStoreException {
checkContentExists(source);
ResultSet rs = Database.executeQuery(database.retrieveExtTextSQL(source.getKey()));
- return Database.asString(rs);
+ return Database.asString(rs); // closes result set
}
@Override
public void storeExtractedText( BinaryValue source,
String extractedText ) throws BinaryStoreException {
PreparedStatement sql = database.updateExtTextSQL(source.getKey(), extractedText);
- Database.executeUpdate(sql);
+ Database.executeUpdate(sql); // doesn't produce a result set
}
@Override
@@ -270,7 +270,7 @@ protected Database doCreateDatabase( Connection connection ) throws BinaryStoreE
Set<BinaryKey> keys = new HashSet<BinaryKey>();
try {
PreparedStatement sql = database.retrieveBinaryKeys(keys);
- List<String> keysString = Database.asStringList(Database.executeQuery(sql));
+ List<String> keysString = Database.asStringList(Database.executeQuery(sql)); // closes result set
Set<BinaryKey> binaryKeys = new HashSet<BinaryKey>(keysString.size());
for (String keyString : keysString) {
@@ -300,11 +300,26 @@ public void shutdown() {
*/
private boolean contentExists( BinaryKey key,
boolean alive ) throws BinaryStoreException {
+ ResultSet rs = null;
+ boolean error = false;
try {
- ResultSet rs = Database.executeQuery(database.retrieveContentSQL(key, alive));
+ rs = Database.executeQuery(database.retrieveContentSQL(key, alive));
return rs.next();
} catch (SQLException e) {
+ error = true;
throw new BinaryStoreException(e);
+ } catch (RuntimeException e) {
+ error = true;
+ throw e;
+ } finally {
+ if (rs != null) {
+ // Always close the result set ...
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ if (!error) throw new BinaryStoreException(e);
+ }
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.