SQL Server 2012为JDBC驱动配置XA
来源:互联网 发布:2017店连店资产优化 编辑:程序博客网 时间:2024/06/05 16:26
想要在项目中使用sqlserver的xa datasource就必须先配置sql server使其可以支持jdbc xa连接。微软doc中给出了详细的配置步骤和注意事项,请参考https://msdn.microsoft.com/en-us/library/aa342335.aspx
大致分为以下几步:
- 确保sqlserver所在机器的MSDTC服务可用
- 下载并安装SQL Server JDBC Driver http://go.microsoft.com/fwlink/?LinkId=245496
- copy sqljdbc_xa.dll至sqlserver的Binn目录(需重启sqlserver)
- 以sa的身份登陆SSMS执行xa_install.sql
- 执行下面的sql给login授权使用XA
USE master GO EXEC sp_grantdbaccess 'login_name', 'login_name' GO EXEC sp_addrolemember [SqlJDBCXAUser], 'login_name'
还可以用下面这个java class测试配置是否成功
import java.net.Inet4Address; import java.sql.*; import java.util.Random; import javax.transaction.xa.*; import javax.sql.*; import com.microsoft.sqlserver.jdbc.*; public class testXA { public static void main(String[] args) throws Exception { // Create variables for the connection string. String prefix = "jdbc:sqlserver://"; String serverName = "localhost"; int portNumber = 1433; String databaseName = "AdventureWorks"; String user = "UserName"; String password = "*****"; String connectionUrl = prefix + serverName + ":" + portNumber + ";databaseName=" + databaseName + ";user=" + user + ";password=" + password; try { // Establish the connection. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con = DriverManager.getConnection(connectionUrl); // Create a test table. Statement stmt = con.createStatement(); try { stmt.executeUpdate("DROP TABLE XAMin"); } catch (Exception e) { } stmt.executeUpdate("CREATE TABLE XAMin (f1 int, f2 varchar(max))"); stmt.close(); con.close(); // Create the XA data source and XA ready connection. SQLServerXADataSource ds = new SQLServerXADataSource(); ds.setUser(user); ds.setPassword(password); ds.setServerName(serverName); ds.setPortNumber(portNumber); ds.setDatabaseName(databaseName); XAConnection xaCon = ds.getXAConnection(); con = xaCon.getConnection(); // Get a unique Xid object for testing. XAResource xaRes = null; Xid xid = null; xid = XidImpl.getUniqueXid(1); // Get the XAResource object and set the timeout value. xaRes = xaCon.getXAResource(); xaRes.setTransactionTimeout(0); // Perform the XA transaction. System.out.println("Write -> xid = " + xid.toString()); xaRes.start(xid,XAResource.TMNOFLAGS); PreparedStatement pstmt = con.prepareStatement("INSERT INTO XAMin (f1,f2) VALUES (?, ?)"); pstmt.setInt(1,1); pstmt.setString(2,xid.toString()); pstmt.executeUpdate(); // Commit the transaction. xaRes.end(xid,XAResource.TMSUCCESS); xaRes.commit(xid,true); // Cleanup. con.close(); xaCon.close(); // Open a new connection and read back the record to verify that it worked. con = DriverManager.getConnection(connectionUrl); ResultSet rs = con.createStatement().executeQuery("SELECT * FROM XAMin"); rs.next(); System.out.println("Read -> xid = " + rs.getString(2)); rs.close(); con.close(); } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } } } class XidImpl implements Xid { public int formatId; public byte[] gtrid; public byte[] bqual; public byte[] getGlobalTransactionId() {return gtrid;} public byte[] getBranchQualifier() {return bqual;} public int getFormatId() {return formatId;} XidImpl(int formatId, byte[] gtrid, byte[] bqual) { this.formatId = formatId; this.gtrid = gtrid; this.bqual = bqual; } public String toString() { int hexVal; StringBuffer sb = new StringBuffer(512); sb.append("formatId=" + formatId); sb.append(" gtrid(" + gtrid.length + ")={0x"); for (int i=0; i<gtrid.length; i++) { hexVal = gtrid[i]&0xFF; if ( hexVal < 0x10 ) sb.append("0" + Integer.toHexString(gtrid[i]&0xFF)); else sb.append(Integer.toHexString(gtrid[i]&0xFF)); } sb.append("} bqual(" + bqual.length + ")={0x"); for (int i=0; i<bqual.length; i++) { hexVal = bqual[i]&0xFF; if ( hexVal < 0x10 ) sb.append("0" + Integer.toHexString(bqual[i]&0xFF)); else sb.append(Integer.toHexString(bqual[i]&0xFF)); } sb.append("}"); return sb.toString(); } // Returns a globally unique transaction id. static byte [] localIP = null; static int txnUniqueID = 0; static Xid getUniqueXid(int tid) { Random rnd = new Random(System.currentTimeMillis()); txnUniqueID++; int txnUID = txnUniqueID; int tidID = tid; int randID = rnd.nextInt(); byte[] gtrid = new byte[64]; byte[] bqual = new byte[64]; if ( null == localIP) { try { localIP = Inet4Address.getLocalHost().getAddress(); } catch ( Exception ex ) { localIP = new byte[] { 0x01,0x02,0x03,0x04 }; } } System.arraycopy(localIP,0,gtrid,0,4); System.arraycopy(localIP,0,bqual,0,4); // Bytes 4 -> 7 - unique transaction id. // Bytes 8 ->11 - thread id. // Bytes 12->15 - random number generated by using seed from current time in milliseconds. for (int i=0; i<=3; i++) { gtrid[i+4] = (byte)(txnUID%0x100); bqual[i+4] = (byte)(txnUID%0x100); txnUID >>= 8; gtrid[i+8] = (byte)(tidID%0x100); bqual[i+8] = (byte)(tidID%0x100); tidID >>= 8; gtrid[i+12] = (byte)(randID%0x100); bqual[i+12] = (byte)(randID%0x100); randID >>= 8; } return new XidImpl(0x1234, gtrid, bqual); } }
0 0
- SQL Server 2012为JDBC驱动配置XA
- 在sql server中配置XA (by quqi99)
- Jboss as 7配置xa的datasource(Sql server)
- JDBC连接Sql Server 驱动
- 导入SQL Server驱动包(JDBC Eclipse)
- SQL Server的分布式XA事务
- SQL Server 2008/2012与JDBC完全配置
- SQL Server 2005 JDBC Driver 配置
- JDBC操作SQL Server连接配置
- jdbc连接SQL Server 2005配置
- SQL Server JDBC Driver配置方法
- SQL Server JDBC驱动中sqljdbc和sqljdbc4区别
- Java环境变量和Sql Server 2000 JDBC驱动变量设置
- Oracle、MySQL、SQL Server 2000调用JDBC驱动的方法
- SQL Server JDBC驱动中sqljdbc和sqljdbc4区别
- SQL Server JDBC 驱动中 sqljdbc 和 sqljdbc4 区别
- maven中安装SQL SERVER 和 Oracle JDBC驱动
- SQL Server JDBC驱动中sqljdbc和sqljdbc4区别
- Guava工具类学习
- 驴得水有感
- Ubuntu 下的命令汇总(不定时更新)
- fsmc 驱动lcd(二)地址的定义
- CentOS6.5自带jdk卸载和安装
- SQL Server 2012为JDBC驱动配置XA
- poj_1166 The Clocks(高斯消元)
- 线程间通信
- tufao安装过程中Could not find a package configuration file provided by "Qt5Core"解决办法
- 小马哥---高仿苹果6sp 主板型号W9200 V3.0新版机型刷机拆机图
- java获取键盘输入的数字,并进行排序
- Linux下的ACl
- Ping经过路由器的运行过程解析
- "=="和equals的区别