Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Java Transaction API: Performing Transaction

In this article we will talk about JTA and we will perform simple transaction with the help of Java Transaction Api.

A transaction can be defined as a series of actions performed one after another. If all the actions are successful then only the transaction is considered as complete. It any of the action fails then it is incomplete and all the previous actions are also rolled back. Java Transaction Application Programming Interface (API) known as JTA is one of the Java Enterprise Edition (EE) API and is the standard interface present in java that is available between the transaction manager and the transaction system which is present in a distributed form and includes an application server, resource manager and the applications based on transactions respectively.

The JTA is a service oriented specification of API and helps the applications in order to perform the distributed transactions and the transactions are able to access and update the data on the resources of two or more than two computers in a network in efficient manner. With transaction, it basically means the logical work which succeeds when the process and work is completed fully and when the work is done and the results are according to the correct assumption then this type of transaction can be called as a successful transaction.If there appears no result of the applied transaction then it can be called as an unsuccessful transaction. In the case of transaction from an application to the local database, there will be the need of an application, the resource manager and the resource adapter respectively in order to access the relational database system and perform the transaction.

The application will be required in order to create or send the requests to the database system and in the same manner also required in order to receive the requests and get data from the database system respectively. The resource manager in this case will be relational database system which can be either the MySQL database, Microsoft SQL Server, Oracle or any other depending on the availability and the need for the respective users. In the case of this article, it is considered that the embedded database system is considered in order to get the work done in an easy and an efficient manner so the component handles this actual database management system.

The resource adapter is basically the translator of request needed and in this considered case that is basically a driver of JDBC used in order to handle the database and perform the respective functionality. This resource adapter is required to get the access to an application and resource manager. The work is done in the following manner; first, an application sends the request in order to get data from the database to the driver of JDBC, second, this driver then translates the request and send the same across the network and this goes to the database system, third, the database that is connected gets the data from it and returns the same to the driver that is basically the resource manager and this resource manager translates the respective result to an application and this is the work done in a respective manner. An application server (AS) is the component present in the transaction process as well which is basically addressed by the JTA. This AS handles the complexity of application operations and takes the load on its own responsibility to get the work completed in an efficient manner.

There is a possibility that the different instances of a single type of database is available and online at different servers so in order to access them the work of distributed transaction and the transaction manager appears to be done.The distributed transaction involves the work done over different resource managers and this working process is the operation of transaction manager that is actually the fifth component. The application work is done by following these steps as discussed in the form of a case in this topic.

In this API, every resource consists of an API respectively like the databases access need JDBC API and the java message service need JMS API in order to access the messaging services in an application.The transaction manager decides to complete the transaction process successfully and make the decision either to commit or rollback the required distributed transaction.

The components which are basically present in the Distributed Transaction Processing (DTP) model and to complete the transaction in a successful manner involves the following which includes an application, an application server, the resource manager, the resource adapter and a transaction manager. The components involved in the transactions which are distributed should be considered as the processes that are independent from each other with respect to location. It is possible that many different components are available in one machine and there is also a possibility that these components are present among different machines.The packages of java should be imported in an application where this API is required to be used and these packages are javax.transaction.* and javax.transaction.xa.* respectively.

Importance of Java Transaction API

There are many services provided by java transaction API including the operations and functions in the client side applications, the transactions functionalities are performed in application servers which are used by the client side and the transaction manager of java deals with different resource manager which supports the transactions functionality like different messaging systems and the database servers. There are separate managers for every resource respectively.

The resource manager mainly consists of its own API in order to use the resource and access the functionality like there is a JDBC API for the access and in order to perform the create, read, update and delete (CRUD) functionalities related to the database systems respectively and similarly for other resources the respective API is used in order to completely get benefit from the used resource in the particular application where these functionalities and related operations are needed. There is an application which basically communicates with the transaction manager in order to begin, commit and rollback the transactions that are needed and to be performed to complete the work of application in a successful and an effective manner.All the related work of transactions is successfully done by the use of JTA which uses special interfaces available in the respective packages.

Following are the screenshots of the transaction work:

Database and Tables

Figure 1: Database and Tables

The Figure 1 shows the tables that are available in the database system which will be used in the application of JTA.

The files and libraries in this application of JTA

Figure 2: The files and libraries in this application of JTA

This Figure 2 shows the hierarchy of files and libraries in the application of JTA_Work.

Implementation & Code

Listing 1: Xid.java

packagejta_work;
public abstract class Xid {
privateintformat_id;
private byte global_transaction_id[];
private byte branch_qualifier_id[];    
}

This is an abstract class in which three attributes are declared.

Listing 2: JTA_Work.java

packagejta_work;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.logging.Level;
importjava.util.logging.Logger;
importjavax.sql.DataSource;
importjavax.sql.XAConnection;
importjavax.sql.XADataSource;
importjavax.transaction.xa.XAResource;
importjavax.transaction.xa.Xid;
importorg.apache.derby.jdbc.ClientDriver;
public class JTA_Work implements Xid{
protectedintformat_id;
protected byte global_transaction_id[];
protected byte branch_qualifier_id[];  
publicJTA_Work() {
    }
publicintgetFormat_id() {
returnformat_id;
    }
public void setFormat_id(intformat_id) {
this.format_id = format_id;
    }
public byte[] getGlobal_transaction_id() {
returnglobal_transaction_id;
    }
public void setGlobal_transaction_id(byte[] global_transaction_id) {
this.global_transaction_id = global_transaction_id;
    }
public byte[] getBranch_qualifier_id() {
returnbranch_qualifier_id;
    }
public void setBranch_qualifier_id(byte[] branch_qualifier_id) {
this.branch_qualifier_id = branch_qualifier_id;
    }
public static void main(String[] args) {
        // TODO code application logic here        
worktransaction();                        
    }
public static void worktransaction(){        
try {
ClientDriverdd=new org.apache.derby.jdbc.ClientDriver();            
XADataSourcexds;
XAConnectionxc;
XAResourcexrs;
Xidxxd;
            Connection co;
            Statement st;
xds=(XADataSource) dd;
xc=xds.getXAConnection("use", "system");
xrs=xc.getXAResource();
co=xc.getConnection();
st=co.createStatement();
xxd=new JTA_Work();
xrs.start(xxd, XAResource.TMNOFLAGS);
st.executeUpdate("insert into USE.STUDENT values (908,222,'Nawaz Yasir','dsfurjn jdnv','1989-08-17','LUMS','CS')");
xrs.end(xxd, XAResource.TMSUCCESS);
st.close();
co.close();
xc.close();
System.out.println("Success");
        } catch (Exception ex) {
Logger.getLogger(JTA_Work.class.getName()).log(Level.SEVERE, null, ex);
ex.getMessage();
        }
    }
    @Override
publicintgetFormatId() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    @Override
public byte[] getGlobalTransactionId() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    @Override
public byte[] getBranchQualifier() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

Conclusion:

The JTA is used and the available package as described in the explanation is used and this is the main class of the application through which the transactions are made in a successful manner available in the database system which can be accessed and allows to make the transactions related to the CRUD functionality respectively.

Hope you liked it. See you next time.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login