Apache Transaction:write file transactionally - how to use resourceId

后端 未结 1 432
陌清茗
陌清茗 2021-01-12 06:40

If anybody implemented transactional writing to file,please, assist me.
Related topic was discussed in earlier thread(transactional write).

Use case is following

相关标签:
1条回答
  • 2021-01-12 06:56

    As far nobody answers, I try do that from my latest experience.

    Useful documentataion:
    example2(.ppt)

    Simplified algorithm looks like(actually,depicted in example2):
    1. initialize FileResourceManager
    2. start FileResourceManager
    3. get transaction Id from FileResourceManager instance
    4. start transaction with transaction Id from step 3
    5. write resource you need - here is mentioned write it transactionally
    ,so looks like it's the major step!
    6. commit or rollback transaction

    Note: resourceId,about i asked in ,my question, is simply name of transactional file. This naming doesn't depict very good this attribute.

    Code, I used:

    private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(FileAppender.class);
    private static LoggerFacade loggerFacade = new Log4jLogger(logger);
    
    private static String tempDir = (String) System.getProperties().get("java.io.tmpdir");
    
    private FileResourceManager frm = new FileResourceManager(tempDir, tempDir, false, loggerFacade);
    private static OutputStream outputStream;
    
    public void writeOut(E event) throws IOException {
        Object txId = null;
        try {
            frm.start();
            txId = frm.generatedUniqueTxId();
            frm.startTransaction(txId);
            outputStream = frm.writeResource(txId, fileName, true);
            frm.commitTransaction(txId);
    
        }
    
        catch (Exception e) {
            throw new IOException("DB rollback");
        }
    }
    
    0 讨论(0)
提交回复
热议问题