XA使用簡例

package com.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.PostConstruct;
import javax.sql.XAConnection;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

import org.postgresql.xa.PGXADataSource;

public class ManyXADataSource {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ManyXADataSource mdt = new ManyXADataSource();
		try{
			mdt.test();
		}catch(Exception e){
			System.out.println(e);
		}
	}

	public void test() {
		Connection con_1 = null;
		Connection con_2 = null;
		
		PGXADataSource ds_1 = null;
		PGXADataSource ds_2 = null;
		
		XAConnection xaCon_1 = null;
		XAConnection xaCon_2 = null;
		
		XAResource xaRes_1 = null;
		XAResource xaRes_2 = null;
		
		Xid xid_1 = null;
		Xid xid_2 = null;
		
		Statement stmt_1 = null;
		Statement stmt_2 = null;
		
		try{
			ds_1 = new PGXADataSource();
			ds_1.setServerName("localhost");
			ds_1.setPortNumber(5432);
			ds_1.setDatabaseName("model_design");
			ds_1.setUser("pg");
			ds_1.setPassword("1");
			xaCon_1 = ds_1.getXAConnection();
			con_1 = xaCon_1.getConnection();
			stmt_1 = con_1.createStatement();
			xaRes_1 = xaCon_1.getXAResource();
			xid_1 = new MyXid(0, new byte[]{0x01}, new byte[]{0x02});
			
			ds_2 = new PGXADataSource();
			ds_2.setServerName("localhost");
			ds_2.setPortNumber(5431);
			ds_2.setDatabaseName("model_design");
			ds_2.setUser("pg");
			ds_2.setPassword("1");
			xaCon_2 = ds_2.getXAConnection();
			con_2 = xaCon_2.getConnection();
			stmt_2 = con_2.createStatement();
			xaRes_2 = xaCon_2.getXAResource();
			xid_2 = new MyXid(0, new byte[]{0x01}, new byte[]{0x03});
			
			xaRes_1.start(xid_1, XAResource.TMNOFLAGS);
			stmt_1.executeUpdate("insert into t_user values('1', '234')");
			xaRes_1.end(xid_1, XAResource.TMSUCCESS);
			
			xaRes_2.start(xid_2, XAResource.TMNOFLAGS);
			stmt_2.executeUpdate("insert into t_user values('2', '12345678')");
			xaRes_2.end(xid_2, XAResource.TMSUCCESS);
			
			int prep_1 = xaRes_1.prepare(xid_1);
			int prep_2 = xaRes_2.prepare(xid_2);
			if(prep_1 == XAResource.XA_OK && prep_2 == XAResource.XA_OK){
				xaRes_1.commit(xid_1, false);
				System.out.println("T1 commit success!");
				xaRes_2.commit(xid_2, false);
				System.out.println("T2 commit success!");
			}else{
				xaRes_1.rollback(xid_1);
				System.out.println("T1 rollback success!");
				xaRes_2.rollback(xid_2);
				System.out.println("T2 rollback success!");
			}
		}catch(SQLException e){
			e.printStackTrace();
			try{
				xaRes_1.rollback(xid_1);
				xaRes_2.rollback(xid_2);
			}catch(XAException ex){
				ex.printStackTrace();
			}
		}catch(XAException ex){
			ex.printStackTrace();
		}finally{
			try{
				stmt_1.close();
				con_1.close();
				xaCon_1.close();
				stmt_2.close();
				con_2.close();
				xaCon_2.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}

}

來源:http://blog.csdn.net/kevin_luan/article/details/8631294

發佈了158 篇原創文章 · 獲贊 75 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章