Apache Transaction:以事务方式写入文件 - 如何使用 resourceId

发布于 2024-10-12 01:00:57 字数 1151 浏览 3 评论 0原文

如果有人实施了事务写入文件,请帮助我。
相关主题已在之前的线程中讨论过(事务写入)。

用例如下:
如果写入日志文件失败,则应回滚相应的数据库事务。

因此,应以事务方式执行写入文件。

我选择了 Apache Commons Transaction lib。
并且有问题,这不允许我进一步讨论,因为还没有找到适当的文档或示例。

我已经创建了 FileResourceManager 的实例:

FileResourceManager frm = 新 FileResourceManager("c:\cur", "c:\cur", true, 记录器);

据我从这个 Apache Commons Transaction 教程中了解到,我应该实施以下步骤:

  1. 开始交易:
    frm.start();

  2. 获取交易它的 ID:
    transactionId = frm. generatedUniqueTxId();

  3. 调用方法,这是需要的,例如 writeResource 与 transactionId 和资源Id:
    frm.writeResource(transactionId, resourceId);

这里有歧义:
a) 如何将 resourceId 与真实资源连接起来,我应该以事务方式编写?
b) 我现在将以事务方式编写的关于 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-10-19 01:00:57

到目前为止没有人回答,我尝试根据我的最新经验这样做。

有用的文档:
example2(.ppt)

简化算法看起来像(实际上,如示例2所示):
1.初始化FileResourceManager
2.启动FileResourceManager
3.从FileResourceManager实例获取事务Id
4. 使用第 3 步中的交易 ID 开始交易
5.编写您需要的资源 - 这里提到以事务方式编写
,所以看起来这是主要的一步!
6.提交或回滚事务

注意:resourceId,关于我在我的问题中提出的问题,只是 事务文件的名称
这个命名并没有很好地描述这个属性。

代码,我用过:

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

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文