Click here to Skip to main content
15,881,381 members
Articles / Programming Languages / SQL

An Introduction to Bitronix JTA Transaction Manager with MySQL

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Jan 2011CPOL2 min read 28.3K   1  
An Introduction to Bitronix JTA Transaction Manager with MySQL

The Java Transaction API (JTA) allows applications to perform distributed transactions —to access and update systems having multiple transactional resources: databases, message queues, custom resource, or resources accessed from multiple processes, perhaps on multiple hosts, as participants in a single transaction.

The purpose of this article is to provide a high-level overview of Bitronix JTA Transaction Manager with mySQL XA datasource.

Steps involve creating a very basic JTA application using Bitronixs Transaction Manager (BTM) are as follows:

  • Step 1 – System Setup: You need to perform predefine set of operations before writing code.
    1. Make sure you have the latest version of Java installed on your system.
    2. Install MySQL 5.1 or higher version if you can download MySQL from their web site.
    3. Download list of JAR files mentioned below:
      1. btm-2.1.0.jar (essential library for BTM)
      2. slf4j-api-1.5.10.jar
      3. slf4j-jdk14-1.5.10.jar
      4. geronimo-jta_1.1_spec-1.1.1.jar
      5. mysql-connector-java-5.1.10-bin.jar
  • Step 2 – MySQL Setup: The following steps need to be performed in order to run your application:
    1. Create a database using the given command:
      SQL
      CREATE DATABASE javatest;
    2. Create a user using the following command:
      SQL
      CREATE USER 'javauser'@'%' IDENTIFIED BY 'javadude';
    3. Create a student table in javatest database like mentioned below:
      SQL
      CREATE TABLE student
      (
        roll_number INT,
        name VARCHAR(20),
        class VARCHAR(5),
        section VARCHAR(5)
      );
  • Step 3 – Setup project: The example I have shown here is developed in Eclipse IDE. Steps required to setup project in Eclipse are pretty straightforward:
    1. Create a new Java project.
    2. Add all the downloaded JAR files mentioned above in its classpath.
  • Step 4 – Write code: Now as you are done with all these steps, finally you are ready to write code and test the application:
    1. Create a Java class BTMWithMySQL:
      Java
      package jtatest;
      
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import bitronix.tm.BitronixTransactionManager;
      import bitronix.tm.TransactionManagerServices;
      import bitronix.tm.resource.jdbc.PoolingDataSource;
      
      /**
       * @author Bikash
       *
       */
      public class BTMWithMySQL {
      
      	private static final String DATABASE = "javatest";
      	private static final String USER_NAME = "javauser";
      	private static final String PASSWORD = "javadude";
      
      	private static final String INSERT_QUERY = _
      		"insert into student(roll_number,name,class,section) _
      		values (?,?,?,?)";
      	private static final String SELECT_QUERY = "select * from student";
      
      	public static void main(String[] args) {
      
      		PoolingDataSource mySQLDS = new PoolingDataSource();
      		mySQLDS.setClassName_
      		("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
      		mySQLDS.setUniqueName("mySqlBtm");
      		mySQLDS.setMaxPoolSize(3);
      		mySQLDS.getDriverProperties().setProperty_
      		("databaseName", DATABASE);
      		mySQLDS.init();
      
      		BitronixTransactionManager btm = _
      		TransactionManagerServices.getTransactionManager();
      
      		try		{
      			btm.begin();
      			Connection connection = _
      			mySQLDS.getConnection(USER_NAME, PASSWORD);
      
      			PreparedStatement pstmt = _
      			connection.prepareStatement(INSERT_QUERY);
      			for(int index = 1; index <= 5; index++) {
      				pstmt.setInt(1,index);
      				pstmt.setString(2, "student_" + index);
      				pstmt.setString(3, "" + (4 + index));
      				pstmt.setString(4, "A");
      				pstmt.executeUpdate();
      			}
      			pstmt.close();
      
      			connection.close();
      
      			btm.commit();
      
      		} catch (Exception ex) {
      			ex.printStackTrace();
                  try {
      				btm.rollback();
      			} catch (Exception e) {
      				e.printStackTrace();
      			}
      		}
      
      		try {
      			btm.begin();
      			Connection connection = _
      			mySQLDS.getConnection(USER_NAME, PASSWORD);
      			PreparedStatement pstmt = _
      			connection.prepareStatement(SELECT_QUERY);
      			ResultSet rs = pstmt.executeQuery();
      			while(rs.next()){
      				System.out.println("Student: " + _
      				rs.getInt("roll_number") + " " +_
      				rs.getString("name") + " " + _
      				rs.getString("class")
      				+ " " + rs.getString("section"));
      			}
      			rs.close();
      			pstmt.close();
      			connection.close();
      			btm.commit();
      		}
      		catch (Exception ex) {
      			ex.printStackTrace();
      			try {
      				btm.rollback();
      			} catch (Exception e) {
      				e.printStackTrace();
      			}
      		}
      		mySQLDS.close();
      		btm.shutdown();
      	}
      }

      Here you can see I have used com.mysql.jdbc.jdbc2.optional.MysqlXADataSource for datasource. But this example is limited to a single database and a single table. By using BitronixTransactionManager, you can perform similar operations on multiple databases.

      Before each insert or select operation, you need to begin transaction by invoking btm.begin() and at the end of it, you commit your transaction by invoking btm.commit().

      Once you perform all your work, you have to shutdown Bitronix Transaction Manager by calling btm.shutdown() as cited in the example above.

    2. Run your code to see how it works.

However, it is a very basic example to Bitronix JTA Transaction, now you are free to explore your own. For more information, you can visit the official Bitronix site at http://www.bitronix.be.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Société Générale
India India
Summary:

1. Extensive working experience in web and windows application development, database programming using Java and .Net technologies.

2. Good knowledge in SDLC and Processes like project planning, requirement gathering, development, test planning, release management and production support.

3. Good knowledge and working experience in following methodologies:
a. Agile Development Approach
b. Test Driven Development
c. Behavior Driven Development
d. Continuous Integration & Delivery Model.

4. Excellent communication & analytical skills, good team player, great mentoring capability.

5. Interaction with customer and team spread over different geographical area.

Technologies / Languages: J2SE 5/6, J2EE, .Net 2.0/3.5, C#, ASP.NET, AJAX, XHTML, CSS, JavaScript, jQuery, PL/SQL, Web Services (SOAP based), Winforms, Hibernate, Spring, GWT, XML, XSD, XPath, SAX, JAXB, JUnit, JBehave, Mockito, Selenium, StringTemplate, Log4J, Apache Commons API

Database: Oracle, SQL Server, MySQL, Sybase

Application Server: Tomcat, Sun Application Server, JBoss, GlassFish, Jetty, IIS

Development Environments: Eclipse, NetBeans, Visual Studio.

Designing Tools: Enterprise Architect, Microsoft Visio

Version Control: SVN, Perforce

Build Management: Hudson & JCruisemonitor, TeamCity

Bug Tracking Tools: HP Quality Center, Bugzilla, JIRA

Specialties:
1. Agile Test and Behavior Driven Development
2. Continuous Delivery Approach
2. Spring IOC and MVC
3. Web Services

Comments and Discussions

 
-- There are no messages in this forum --