Apache Transaction: writing a file transactionally - how to use resourceId

If someone completed the transactional letter to a file, please help me.
A related topic was discussed in an earlier thread ( transactional record ).

The use case is as follows:
if writing to the log file failed, cancel the corresponding DB transaction.

Thus, the recording file must be executed in a transactional manner.

I chose Apache Commons Transaction lib.
And there is a problem that does not allow me to go further, because I did not find the relevant documentation or examples.

I created an instance of FileResourceManager:

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

As I understand from this Apache Commons Transaction tutorial , I have to follow these steps:

  • start a transaction:
    frm.start();

  • get transaction ID for it:
    transactionId = frm.generatedUniqueTxId();

  • call method, which is necessary, for example. writeResource with transactionId and resourceId:
    frm.writeResource(transactionId, resourceId);

And here is the ambiguity:
a) how can I connect resourceId to a real resource, what should I write transactioanally?
b) how will my file, which I will write transactionally, now be near resourceId ?

Thanks for the tips.

+6
java file apache-commons transactional
source share
2 answers

As far as no one answers me, I try to do this from my last experience.

Useful documentation:
example2 (.ppt)

The simplified algorithm looks (actually, depicted in example 2):
1. initialize FileResourceManager
2. run FileResourceManager
3. Get Transaction ID from an instance of FileResourceManager
4. start the transaction with the transaction ID from step 3
5. write the resource you need - it is mentioned here, write it transactionally , so that it looks like an important step!
6. commit transaction or rollback

Note. resourceId , about my request, my question is just the name of the transaction file . This designation does not reflect this attribute very well.

The 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"); } } 
+4
source share

@sergionni I have the following error while executing code. Could you help me decide?

 org.apache.commons.transaction.file.ResourceManagerSystemException: 16ad53e99b9-0: Can not write to resource at 'C:/Temp/TransactedFileCopy/out/Assures.Service- 20180711.log' (ERR_SYSTEM) Caused by: java.io.IOException: The filename, directory name, or volume label syntax is incorrect at java.io.WinNTFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:1012) at org.apache.commons.transaction.util.FileHelper.createFile(FileHelper.java:75) at 
0
source share

All Articles