Announcement Announcement Module
Collapse
No announcement yet.
Write exception when Rollback=false in test and FlatFileItemWriter closed in code! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Write exception when Rollback=false in test and FlatFileItemWriter closed in code!

    We are trying to test our spring batch file generation code through transactional TestNG class. We have used the FlatFileItemWriter to write the output file.

    The situation is, if we close the FlatFileItemWriter in our code and execute the test with @Rollback(value=false) so that wee can see output in the generated file, we get the below exception:
    Code:
    2010-04-26 10:14:53,138 ERROR [main] {TransactionSynchronizationUtils.java:171) - TransactionSynchronization.afterCompletion threw exception
    org.springframework.batch.support.transaction.FlushFailedException: Could not write to output buffer
    	at org.springframework.batch.support.transaction.TransactionAwareBufferedWriter$1.afterCompletion(TransactionAwareBufferedWriter.java:71)
    	at org.springframework.transaction.support.TransactionSynchronizationUtils.invokeAfterCompletion(TransactionSynchronizationUtils.java:168)
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.invokeAfterCompletion(AbstractPlatformTransactionManager.java:996)
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerAfterCompletion(AbstractPlatformTransactionManager.java:971)
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:799)
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    	at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:505)
    	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:278)
    	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:171)
    	at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:426)
    	at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextAfterTestMethod(AbstractTestNGSpringContextTests.java:179)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:607)
    	at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:417)
    	at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:154)
    	at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:669)
    	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:956)
    	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
    	at org.testng.TestRunner.runWorkers(TestRunner.java:759)
    	at org.testng.TestRunner.privateRun(TestRunner.java:592)
    	at org.testng.TestRunner.run(TestRunner.java:486)
    	at org.testng.SuiteRunner.runTest(SuiteRunner.java:332)
    	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:327)
    	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:299)
    	at org.testng.SuiteRunner.run(SuiteRunner.java:204)
    	at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:877)
    	at org.testng.TestNG.runSuitesLocally(TestNG.java:842)
    	at org.testng.TestNG.run(TestNG.java:751)
    	at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
    	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)
    Caused by: java.io.IOException: Stream closed
    	at sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.java:26)
    	at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:99)
    	at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:116)
    	at java.io.Writer.write(Writer.java:140)
    	at org.springframework.batch.support.transaction.TransactionAwareBufferedWriter$1.afterCompletion(TransactionAwareBufferedWriter.java:67)
    	... 34 more
    2010-04-26 10:14:53,138 INFO  [main] {TransactionalTestExecutionListener.java:280) - Committed transaction after test execution for test context
    However, if we run using @Rollback(value=true), everything works, however, obviously nothing is written in the file generated!

    Our query is, are we not supposed to explicitly close the FlatFileItemWriter in our code and live it on the Transaction to do that?

    Will very much appreciate your help.

  • #2
    On further inspection, it appears to be an issue exits outside the TestNG test framework purview and in actual Transaction. So the problem is, if we use FlatFileItemWriter within a transaction and close the FlatFileItemWriter in code, an exception is thrown while the transaction finishes and the framework actually try to write the data into the file. So the question is, is it a bug? Or is this a intended behaviour?

    Help please? anyone?

    Comment


    • #3
      What version of Spring Batch is this (did you try 2.1.*)? If you use a FlatFileItemWriter outside a transaction it will behave as you expect, irrespective. So why do you need to call close() in a transaction?

      Comment


      • #4
        I have the same problem but am using batch version 2.0.3 and am not trying to close the file explicitly.
        Is this a bug in version 2.0.x?

        Comment


        • #5
          how are you using the FlatFileItemWriter? standalone or as part of a Spring Batch job?

          Comment

          Working...
          X