BATCH-1640: fixed resource cleanup in buffered writer

This commit is contained in:
dsyer
2010-10-14 00:16:10 +00:00
parent 5545925e17
commit 2b21e39556
2 changed files with 31 additions and 19 deletions

View File

@@ -72,34 +72,40 @@ public class TransactionAwareBufferedWriter extends Writer {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
if (status == STATUS_COMMITTED) {
complete();
try {
if (status == STATUS_COMMITTED) {
complete();
}
}
catch (IOException e) {
throw new FlushFailedException("Could not write to output buffer", e);
}
finally {
clear();
}
}
private void complete() {
private void complete() throws IOException {
StringBuffer buffer = (StringBuffer) TransactionSynchronizationManager.getResource(bufferKey);
if (buffer != null) {
try {
writer.write(buffer.toString());
writer.flush();
if (TransactionSynchronizationManager.hasResource(closeKey)) {
writer.close();
closeCallback.run();
}
}
catch (IOException e) {
throw new FlushFailedException("Could not write to output buffer", e);
}
finally {
TransactionSynchronizationManager.unbindResource(bufferKey);
if (TransactionSynchronizationManager.hasResource(closeKey)) {
TransactionSynchronizationManager.unbindResource(closeKey);
}
writer.write(buffer.toString());
writer.flush();
if (TransactionSynchronizationManager.hasResource(closeKey)) {
writer.close();
closeCallback.run();
}
}
}
private void clear() {
if (TransactionSynchronizationManager.hasResource(bufferKey)) {
TransactionSynchronizationManager.unbindResource(bufferKey);
}
if (TransactionSynchronizationManager.hasResource(closeKey)) {
TransactionSynchronizationManager.unbindResource(closeKey);
}
}
});
}