xatest.java
来自「jtds的源码 是你学习java的好东西」· Java 代码 · 共 429 行 · 第 1/2 页
JAVA
429 行
//jTDS JDBC Driver for Microsoft SQL Server and Sybase
//Copyright (C) 2004 The jTDS Project
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
package net.sourceforge.jtds.test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import net.sourceforge.jtds.jdbc.Messages;
import net.sourceforge.jtds.jdbc.Driver;
import net.sourceforge.jtds.jdbcx.JtdsDataSource;
import net.sourceforge.jtds.jdbcx.JtdsXid;
/**
* Test suite for XA Distributed Transactions. These tests are derived from
* examples found in the following article at
* <a href="http://archive.devx.com/java/free/articles/dd_jta/jta-2.asp">DevX</a>.
*
* @version $Id: XaTest.java,v 1.5 2004/12/03 16:52:09 alin_sinpalean Exp $
*/
public class XaTest extends DatabaseTestCase {
public XaTest(String name) {
super(name);
}
/**
* Obtain an XADataSource.
*
* @return the <code>XADataSource.
* @throws SQLException if an error condition occurs
*/
public XADataSource getDataSource() throws SQLException {
JtdsDataSource xaDS = new JtdsDataSource();
String user = props.getProperty(Messages.get(Driver.USER));
String pwd = props.getProperty(Messages.get(Driver.PASSWORD));
String host = props.getProperty(Messages.get(Driver.SERVERNAME));
String port = props.getProperty(Messages.get(Driver.PORTNUMBER));
String database = props.getProperty(Messages.get(Driver.DATABASENAME));
String xaMode = props.getProperty(Messages.get(Driver.XAEMULATION));
String tds = props.getProperty(Messages.get(Driver.TDS));
String serverType = props.getProperty(Messages.get(Driver.SERVERTYPE));
int portn;
try {
portn = Integer.parseInt(port);
} catch (NumberFormatException e) {
portn = 1433;
}
xaDS.setServerName(host);
xaDS.setPortNumber(portn);
xaDS.setUser(user);
xaDS.setPassword(pwd);
xaDS.setDatabaseName(database);
xaDS.setXaEmulation(xaMode.equalsIgnoreCase("true"));
xaDS.setTds(tds);
xaDS.setServerType("2".equals(serverType)? 2: 1);
return xaDS;
}
/**
* Test to demonstrate the XA_COMMIT function.
*
* @throws Exception if an error condition occurs
*/
public void testXaCommit() throws Exception {
Connection con2 = null;
XAConnection xaCon = null;
try {
dropTable("jTDS_XATEST");
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE jTDS_XATEST (id int primary key, data varchar(255))");
assertNotNull(stmt.executeQuery("SELECT * FROM jTDS_XATEST"));
stmt.close();
XADataSource xaDS = getDataSource();
XAResource xaRes;
Xid xid;
xaCon = xaDS.getXAConnection();
xaRes = xaCon.getXAResource();
con2 = xaCon.getConnection();
stmt = con2.createStatement();
xid = new JtdsXid(new byte[]{0x01}, new byte[]{0x02});
xaRes.start(xid, XAResource.TMNOFLAGS);
stmt.executeUpdate("INSERT INTO jTDS_XATEST VALUES (1, 'TEST LINE')");
xaRes.end(xid, XAResource.TMSUCCESS);
int ret = xaRes.prepare(xid);
if (ret == XAResource.XA_OK) {
xaRes.commit(xid, false);
}
stmt.close();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM jTDS_XATEST");
assertNotNull(rs);
assertTrue(rs.next());
stmt.close();
} finally {
if (con2 != null) {
con2.close();
}
if (xaCon != null) {
xaCon.close();
}
dropTable("jTDS_XATEST");
}
}
/**
* Test to demonstrate the single phase XA_COMMIT function.
*
* @throws Exception if an error condition occurs
*/
public void testXaOnePhaseCommit() throws Exception {
Connection con2 = null;
XAConnection xaCon = null;
try {
dropTable("jTDS_XATEST");
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE jTDS_XATEST (id int primary key, data varchar(255))");
assertNotNull(stmt.executeQuery("SELECT * FROM jTDS_XATEST"));
stmt.close();
XADataSource xaDS = getDataSource();
XAResource xaRes;
Xid xid;
xaCon = xaDS.getXAConnection();
xaRes = xaCon.getXAResource();
con2 = xaCon.getConnection();
stmt = con2.createStatement();
xid = new JtdsXid(new byte[]{0x01}, new byte[]{0x02});
xaRes.start(xid, XAResource.TMNOFLAGS);
stmt.executeUpdate("INSERT INTO jTDS_XATEST VALUES (1, 'TEST LINE')");
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.commit(xid, true);
stmt.close();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM jTDS_XATEST");
assertNotNull(rs);
assertTrue(rs.next());
stmt.close();
} finally {
if (con2 != null) {
con2.close();
}
if (xaCon != null) {
xaCon.close();
}
dropTable("jTDS_XATEST");
}
}
/**
* Test to demonstrate the use of the XA_ROLLBACK command.
*
* @throws Exception if an error condition occurs
*/
public void testXaRollback() throws Exception {
Connection con2 = null;
XAConnection xaCon = null;
try {
dropTable("jTDS_XATEST");
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE jTDS_XATEST (id int primary key, data varchar(255))");
assertNotNull(stmt.executeQuery("SELECT * FROM jTDS_XATEST"));
stmt.close();
XADataSource xaDS = getDataSource();
XAResource xaRes;
Xid xid;
xaCon = xaDS.getXAConnection();
xaRes = xaCon.getXAResource();
con2 = xaCon.getConnection();
stmt = con2.createStatement();
xid = new JtdsXid(new byte[]{0x01}, new byte[]{0x02});
xaRes.start(xid, XAResource.TMNOFLAGS);
stmt.executeUpdate("INSERT INTO jTDS_XATEST VALUES (1, 'TEST LINE')");
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.rollback(xid);
stmt.close();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM jTDS_XATEST");
assertNotNull(rs);
assertFalse(rs.next());
stmt.close();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?