Announcement Announcement Module
Collapse
No announcement yet.
Can transaction mangement apply to File operation progrmatically using TaskletStep Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can transaction mangement apply to File operation progrmatically using TaskletStep

    I am working on project where we have requirement to do transaction management programmatically for both DB operation and File I/O operation. I am using TaskletStep and have successfully implemented the transaction management for DB operation and but no luck on file operation.

    I want to do DB operation first if DB operation successful then records will be written to multiple files. Here If writing successful then commit code otherwise do roll back on DB as well as records which are write on files. Can this is possible??

    Below is code snippet.
    Code:
    private ItemWriter<BaseCbrsRecVO> delinquentFile;
    private ItemWriter<BaseCbrsRecVO> rejectFile;
    private ItemWriter<BaseCbrsRecVO> manualFile;
    
    List delinquentRecList = new ArrayList();
    List rejectRecList = new ArrayList();
    List manualStmtList = new ArrayList();
    
    
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("TxnManager");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    
    TransactionStatus status = txnManager.getTransaction(def);
    
    //BUSINESS LOGIC
    
    //PERFORM DB OPEARTION  -- Insert records
    	insert(delinquentRecList); //Jdbc call to insert records
    	insert(rejectRecList);	//Jdbc call to insert records
    	insert(manualStmtList); //Jdbc call to insert records
    
    boolean writeSuccessful = performWriteOperation(delinquentRecList,rejectRecList,manualStmtList);
    if(writeSuccessful){
    	txnManager.commit(transactionStatus);
    }else{
    	//Here want to rollback all data written on any of the file delinquentFile,rejectFile,manualFile. Can this is possible???
    	txnManager.rollback(transactionStatus);
    }
    
    //This method will perform i/o operation
    private boolean performWriteOperation(ArrayList delinquentRecList,ArrayList rejectRecList,ArrayList manualStmtList){
    		try{
    			if(delinquentRecList.size() > 0){
    				delinquentFile.write(delinquentRecList);
    			}
    			if(rejectRecList.size() > 0){
    				rejectFile.write(rejectRecList);
    			}
    			if(manualStmtList.size() > 0){
    				manualFile.write(manualStmtList);
    			}
    			return true;
    		}catch (Exception e) {
    			return false;
    		}
    }


    Any help would be greatly appreciated

  • #2
    Transaction managment is not supported for file operations.

    However you could write code is a way that behaviour is close to file operation being part of transaction.
    start transaction
    perform db operation
    perform file operation
    if (exception in file operation)
    throw runtime exception
    end transaction

    The db operation is done before file operation as the db operation can be rolled back in case of exception with file operation. The file operation throws a runtime exception as it causes Spring to rollback the transaction.

    Comment


    • #3
      Use XA transactions alongwith XADisk.

      Hi,

      Using XA transactions you can combine operations across multiple resources in a single global transaction. This would guarantee that either all of the operations commit or none of them commit. You need to configure your database's data-source to enable XA with database. As file-systems do not support XA, you can use XADisk to perform the file operations inside XA transactions.

      Hope that helps.

      Thanks,
      Nitin

      Comment

      Working...
      X