Apache 事务:以事务方式写入文件 - 如何使用 resourceId [英] Apache Transaction:write file transactionally - how to use resourceId

查看:19
本文介绍了Apache 事务:以事务方式写入文件 - 如何使用 resourceId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人实现了对文件的事务性写入,请帮助我.
相关主题在较早的线程中讨论过(事务写入)..>

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

所以写入文件应该以事务的方式进行.

我选择了 Apache Commons Transaction 库.
并且有问题,这并没有让我走得更远,因为还没有找到合适的文档或示例.

我已经创建了 FileResourceManager 的实例:

<块引用>

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

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

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

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

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

这里有歧义:
a) 如何将 resourceId 与真实资源连接起来,我应该以交易方式编写?
b) 我的文件,我将以事务方式编写的关于 resourceId 的文件如何处理?

谢谢你的建议.

解决方案

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

有用的文档:
example2(.ppt)

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

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

代码,我用过:

private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(FileAppender.class);私有静态 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);私有静态输出流输出流;public void writeOut(E event) 抛出 IOException {对象 txId = null;尝试 {frm.start();txId = frm.generatedUniqueTxId();frm.startTransaction(txId);outputStream = frm.writeResource(txId, fileName, true);frm.commitTransaction(txId);}捕获(异常 e){throw new IOException("数据库回滚");}}

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.

解决方案

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

这篇关于Apache 事务:以事务方式写入文件 - 如何使用 resourceId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆