本文介绍了Apache事务:事务写文件 - 如何使用resourceId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如果有人执行事务性写入文件,请协助我。 相关话题在前面的讨论线程( > 如果写入日志文件失败,那么适当的数据库事务将被回滚。 所以写入文件应该以交易方式执行。 我选择了 Apache Commons Transaction lib。 如果没有找到合适的文档或示例,有问题,请不要过问。 我创建了FileResourceManager的实例: lockquote FileResourceManager frm = new $ b $ FileResourceManager(c:\cur ,c:\cur,true,logger); 据我所知,从这个 Apache Commons Transaction tutorial ,我应该执行以下步骤: $ b 开始交易: frm。 start(); 获取事务标识符: transactionId = frm.generatedUniqueTxId ); 调用方法,这是必需的,例如使用transactionId和resourceId写入资源: frm.writeResource(transactionId,resourceId); 这里是不明确的: a)如何将 resourceId 与实际资源连接起来,我应该写transactioanally? b)如何做我的文件,我将事务写入现在将关于 resourceId ? 感谢您的建议。 解决方案到目前为止没有人回答,我尝试从我的最新经验做到这一点。 $ b 有用的文档: example2(.ppt) b $ b 简化算法看起来像(实际上如example2所示): 1。初始化从文件资源管理器实例获取事务标识从文件资源管理器实例获取事务标识 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; 尝试{ 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); } } 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:As I understand from this Apache Commons Transaction tutorial,i should implement following steps:start transaction: frm.start();get transaction Id for it: transactionId = frm.generatedUniqueTxId();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 instance4. start transaction with transaction Id from step 35. write resource you need - here is mentioned write it transactionally,so looks like it's the major step!6. commit or rollback transactionNote: 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 10:11