Apache Commons transcation Introduction and implementation of file transaction system

Source: Internet
Author: User
Tags log log

Apache Commons Transaction is designed to provide a lightweight, standardized, and efficient toolkit for multithreaded programming of Java transactions, enabling multilevel locks, transaction sets, and transaction-level file access.

The transaction processing system is very familiar to the database users, but if the transaction processing system is transferred from the database to the file system, it is estimated that many students will be helpless. In fact, support for file system operations transactions has been weak, regardless of library/framework, language, or file system level.

See some file system operations alone (such as file renaming, deletion, etc.), they are atomic, but from the current situation, there are few solutions to form a comprehensive set of APIs, all-round support for transactional file IO operations. If file operations (such as creating, modifying, renaming, deleting files) need to be executed consistently as part of a transaction, the application often has to rely on a self-designed scenario to reduce the likelihood of a system/application failure or inconsistent state when concurrent access occurs.

Apache Commonstransaction is doing some of the work in this area.

One of the goals of the Apache Commons transaction project is to provide transactional access to the file system, which is implemented independently of the specific file system provider/implementation. This Java library uses a pessimistic locking scheme to implement ACID transactions on the file system.

The core component of Apache Commonstransaction is the file operation of Fileresourcemanager,fileresourcemanager creating a transaction, coordinating the resources/files under its control-copy, create, delete, move, write, As well as preparing and committing transactions. To prepare for Fileresourcemanager during the initialization phase:

· The directory where the primary data is stored after submission

· The directory in which the transaction stores temporary data (working directory)

· Boolean flag indicating whether to URL-encode the path

· Fileresourcemanager used by the logger (logger)

After startup, Fileresourcemanager will then attempt to roll back all outstanding transactions, unless the transaction is in the process of being committed in the event of a system failure or fileresourcemanager encountering an irreversible problem, in which case Fileresourcemanager will attempt to roll forward the transaction. In the event that a transaction cannot be recovered, for example, it cannot be rolled back or rolled forward, the entire working directory is marked as dirty by Fileresourcemanger and is no longer allowed to be modified until the problem is resolved. The information in the log usually helps to recover manually from the dirty state.

The following code is simple to implement the file transaction system with Apache Commons transaction

Packagetest.ffm83.commons.transcation;

Importorg.apache.commons.logging.Log;

Importorg.apache.commons.logging.LogFactory;

Importorg.apache.commons.transaction.file.FileResourceManager;

Importorg.apache.commons.transaction.file.ResourceManagerException;

Importorg.apache.commons.transaction.util.CommonsLoggingLogger;

Importorg.apache.commons.transaction.util.LoggerFacade;

/**

* Simple File system transaction control via Commons Transaction

* The log here is the class in the Commons-transaction package

* This application needs to import additional JTA related jar packages

* @author Fan Fangming

*/

Publicclass Transactionusage {

public static void Main (string[] args) {

Log log =logfactory.getlog (Transactionusage.class);

Loggerfacade logger = newcommonslogginglogger (log);

Working directory

String Workdir = "d:/ffm83/work/";

Temporary Data storage Directory

String tempdir = "D:/ffm83/temp";

The third parameter of the constructor: false, which identifies whether the URL of the document is encoding, which is generally not required to be set to True

Fileresourcemanager frm = Newfileresourcemanager (Workdir, Tempdir,false, logger);

String txId = "";

try {

This identifies the state of the frm as start

Frm.start ();

TxId =frm.generateduniquetxid (); Get the Object ID

Open transaction

Frm.starttransaction (TXID);

Frm.deleteresource (TxId, "ffm83.txt");

System.out.println ("Delete files in this directory successfully");

Commit a transaction

Frm.committransaction (TXID);

} catch (Exception e) {

try {

Rolling back a transaction

Frm.rollbacktransaction (TXID);

System.out.println ("Delete files in this directory failed, rollback. ");

}catch (resourcemanagerexception E1) {

E1.printstacktrace ();

}

}

}

}

The results of the operation are as follows:

Delete files in this directory successfully

Apache Commons transcation Introduction and implementation of file transaction system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.