Skip to content

Commit

Permalink
Code modified to release connection from pool when an exception is th…
Browse files Browse the repository at this point in the history
…rown.
  • Loading branch information
jalessandro committed Jun 16, 2013
1 parent e78915d commit bf3457b
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/main/java/com/nanlabs/images/ImageImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,36 @@ public void doImport(String sourceURL, int... widths) throws IOException {
String encodedURL = normalize(sourceURL);
String urlPath = new URL(sourceURL).getPath();

URLConnectionFactory connectionFactory;
URLConnectionFactory connectionFactory = null;
try {
connectionFactory = connectionFactoryPool.borrowObject();
processImage(connectionFactory.open(encodedURL), urlPath, widths);
connectionFactoryPool.returnObject(connectionFactory);
}catch (IOException ioe){
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unnable to borrow connectionFactory from the pool");
}
throw new RuntimeException("Unable to borrow connectionFactory from the pool");
} finally {
if(connectionFactory != null) {
try {
connectionFactoryPool.returnObject(connectionFactory);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to return connectionFactory to the pool");
}
}
}
}

private void processImage(URLConnection connection, String urlPath, int... widths) throws IOException {
InputStream imageData = connection.getInputStream();
try{
try {
for (Integer width : widths) {
byte[] processedImage = imageProcessor.doResize(imageData, width);
Image image = new Image(urlPath, processedImage, width);
storageRepository.store(image);
}
}finally{
} finally {
imageData.close();
}
}
Expand Down

0 comments on commit bf3457b

Please sign in to comment.