BATCH-1640: fixed resource cleanup in buffered writer
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user