sergionni sergionni - 2 years ago 92
Java Question

Apache Transaction:write file transactionally - how to use resourceId

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.

Answer Source

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");
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download