Apache Transaction:write file transactionally - how to use resourceId

梦想的初衷 提交于 2019-12-19 06:18:12

问题


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

Use case is following:
if writing to log file is failed,that appropriate DB transaction shoud be rolled back.

So the writinig to file should be performed in transactional way.

I've chosen Apache Commons Transaction lib.
And have issue,that doesn't let me go further,because haven't found appropriate documentation or examples.

I have created instance of FileResourceManager:

FileResourceManager frm = new FileResourceManager("c:\cur", "c:\cur", true, logger);

As I understand from this Apache Commons Transaction tutorial,i should implement following steps:

  1. start transaction:
    frm.start();

  2. get transaction Id for it:
    transactionId = frm.generatedUniqueTxId();

  3. call method, that is needed, e.g. writeResource with transactionId and resourceId:
    frm.writeResource(transactionId, resourceId);

And here is ambiguity:
a) how can I connect resourceId with real resource,that I should write transactioanally?
b) how do my file,that I will write transactionally will now about resourceId?

Thank you for advise.


回答1:


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");
    }
}


来源:https://stackoverflow.com/questions/4682584/apache-transactionwrite-file-transactionally-how-to-use-resourceid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!