本文整理汇总了Java中javax.sql.XAConnection.getXAResource方法的典型用法代码示例。如果您正苦于以下问题:Java XAConnection.getXAResource方法的具体用法?Java XAConnection.getXAResource怎么用?Java XAConnection.getXAResource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sql.XAConnection
的用法示例。
在下文中一共展示了XAConnection.getXAResource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: switchToXid
import javax.sql.XAConnection; //导入方法依赖的package包/类
private synchronized void switchToXid(Xid xid) throws XAException {
if (xid == null) {
throw new XAException();
}
try {
if (!xid.equals(this.currentXid)) {
XAConnection toSwitchTo = findConnectionForXid(this.underlyingConnection, xid);
this.currentXAConnection = toSwitchTo;
this.currentXid = xid;
this.currentXAResource = toSwitchTo.getXAResource();
}
} catch (SQLException sqlEx) {
throw new XAException();
}
}
示例2: registerTranxConnection
import javax.sql.XAConnection; //导入方法依赖的package包/类
/**
*
*/
void registerTranxConnection(XAConnection xaConn) throws Exception {
try {
synchronized (this) {
if (transManager == null) {
transManager = JNDIInvoker.getTransactionManager();
}
}
Transaction txn = transManager.getTransaction();
if (txn != null) {
XAResource xar = xaConn.getXAResource();
txn.enlistResource(xar);
// Add in the Map after successful registration of XAResource
this.xaResourcesMap.put(xaConn, xar);
}
} catch (Exception ex) {
Exception e = new Exception(
LocalizedStrings.GemFireTransactionDataSource_GEMFIRETRANSACTIONDATASOURCEREGISTERTRANXCONNECTION_EXCEPTION_IN_REGISTERING_THE_XARESOURCE_WITH_THE_TRANSACTIONEXCEPTION_OCCURED_0
.toLocalizedString(ex));
e.initCause(ex);
throw e;
}
}
示例3: testMixedXaNormal
import javax.sql.XAConnection; //导入方法依赖的package包/类
private void testMixedXaNormal() throws Exception {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:test");
ds.setUser("sa");
ds.setPassword("");
XAConnection xa = ds.getXAConnection();
Connection c = xa.getConnection();
assertTrue(c.getAutoCommit());
MyXid xid = new MyXid();
XAResource res = xa.getXAResource();
res.start(xid, XAResource.TMNOFLAGS);
assertTrue(!c.getAutoCommit());
res.end(xid, XAResource.TMSUCCESS);
res.commit(xid, true);
assertTrue(c.getAutoCommit());
res.start(xid, XAResource.TMNOFLAGS);
assertTrue(!c.getAutoCommit());
res.end(xid, XAResource.TMFAIL);
res.rollback(xid);
assertTrue(c.getAutoCommit());
c.close();
xa.close();
}
示例4: testCloseActiveConnection_XA_global
import javax.sql.XAConnection; //导入方法依赖的package包/类
/**
* Test that connections retrieved from {@code XADataSource} that are part
* of a global XA transaction, behave as expected when {@code close()} is
* called and the transaction is active.
*/
public void testCloseActiveConnection_XA_global()
throws SQLException, XAException
{
XADataSource ds = J2EEDataSource.getXADataSource();
XAConnection xa = ds.getXAConnection();
XAResource xar = xa.getXAResource();
Xid xid = new cdsXid(1, (byte) 2, (byte) 3);
xar.start(xid, XAResource.TMNOFLAGS);
// auto-commit is always off in XA transactions, so we expect
// getAutoCommit() to return false without having set it explicitly
testCloseActiveConnection(xa.getConnection(), false, true);
Connection c = xa.getConnection();
c.setAutoCommit(false);
testCloseActiveConnection(c, false, true);
xar.end(xid, XAResource.TMSUCCESS);
}
示例5: switchToXid
import javax.sql.XAConnection; //导入方法依赖的package包/类
private synchronized void switchToXid(Xid xid) throws XAException {
if (xid == null) {
throw new XAException();
}
try {
if (!xid.equals(this.currentXid)) {
XAConnection toSwitchTo = findConnectionForXid(this.underlyingConnection, xid);
this.currentXAConnection = toSwitchTo;
this.currentXid = xid;
this.currentXAResource = toSwitchTo.getXAResource();
}
} catch (SQLException sqlEx) {
throw new XAException();
}
}
示例6: createManagedConnection
import javax.sql.XAConnection; //导入方法依赖的package包/类
public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) throws ResourceException {
CredentialExtractor credentialExtractor = new CredentialExtractor(subject, connectionRequestInfo, this);
XAConnection sqlConnection = getPhysicalConnection(credentialExtractor);
try {
XAResource xares = sqlConnection.getXAResource();
Connection pc = wrap(sqlConnection.getConnection());
return new ManagedXAConnection(this, sqlConnection, xares, pc, credentialExtractor, exceptionSorter);
} catch (SQLException e) {
throw new ResourceAdapterInternalException("Could not set up ManagedXAConnection", e);
}
}
示例7: testEffectOfBlockingTimeoutOnXAConnection
import javax.sql.XAConnection; //导入方法依赖的package包/类
/**
* Tests if an XAresource obtained from an XAConnection which is already closed , can return null
* or not.
*/
@Ignore("TODO: test used to eat its own exception and it fails")
@Test
public void testEffectOfBlockingTimeoutOnXAConnection() throws Exception {
Map map = new HashMap();
map.put("init-pool-size", "2");
map.put("jndi-name", "TestXAPooledDataSource");
map.put("max-pool-size", "7");
map.put("idle-timeout-seconds", "20");
map.put("blocking-timeout-seconds", "2");
map.put("login-timeout-seconds", "5");
// map.put("xa-datasource-class","org.apache.derby.jdbc.EmbeddedXADataSource");
map.put("jdbc-driver-class", "org.apache.derby.jdbc.EmbeddedDriver");
map.put("user-name", "mitul");
map.put("password", "83f0069202c571faf1ae6c42b4ad46030e4e31c17409e19a");
map.put("connection-url", "jdbc:derby:newDB;create=true");
List props = new ArrayList();
props.add(new ConfigProperty("databaseName", "newDB", "java.lang.String"));
GemFireBasicDataSource gbds =
(GemFireBasicDataSource) DataSourceFactory.getSimpleDataSource(map, props);
map.put("xa-datasource-class", "org.apache.derby.jdbc.EmbeddedXADataSource");
map.put("connection-url", "jdbc:derby:newDB;create=true");
GemFireTransactionDataSource gtds =
(GemFireTransactionDataSource) DataSourceFactory.getTranxDataSource(map, props);
XAConnection xaconn = (XAConnection) gtds.provider.borrowConnection();
try {
Thread.sleep(4);
} catch (InterruptedException e) {
fail("interrupted");
}
for (int i = 0; i < 1000; ++i) {
XAResource xar = xaconn.getXAResource();
System.out.println("XAResource=" + xar);
assertNotNull(xar);
}
}
示例8: enlistConnection
import javax.sql.XAConnection; //导入方法依赖的package包/类
/** Enlists the given XAConnection and if a transaction is active in the current thread, returns a plain JDBC Connection */
public static Connection enlistConnection(XAConnection xacon) throws GenericTransactionException {
if (xacon == null) {
return null;
}
try {
XAResource resource = xacon.getXAResource();
TransactionUtil.enlistResource(resource);
return xacon.getConnection();
} catch (SQLException e) {
throw new GenericTransactionException("SQL error, could not enlist connection in transaction even though transactions are available", e);
}
}
示例9: testSimpleXATransaction
import javax.sql.XAConnection; //导入方法依赖的package包/类
public void testSimpleXATransaction() throws Exception {
Statement stm = getConnection().createStatement();
stm.execute("create table XATT2 (i int, text char(10))");
XADataSource xaDataSource = (XADataSource)TestUtil.getXADataSource(TestUtil.EmbeddedeXADsClassName);
// create large enough xid
byte[] gid = new byte[64];
byte[] bid = new byte[64];
for (int i = 0; i < 64; i++) {
gid[i] = (byte)i;
bid[i] = (byte)(64 - i);
}
Xid xid = new ClientXid(0x1234, gid, bid);
// get the stuff required to execute the global transaction
XAConnection xaConn = xaDataSource.getXAConnection();
XAResource xaRes = xaConn.getXAResource();
Connection conn = xaConn.getConnection();
conn.setTransactionIsolation(getIsolationLevel());
// start the transaction with that xid
xaRes.start(xid, XAResource.TMNOFLAGS);
// do some work
stm = conn.createStatement();
stm.execute("insert into XATT2 values (1234, 'Test_Entry')");
stm.close();
stm = getConnection().createStatement();
stm.execute("select * from XATT2");
ResultSet rs = stm.getResultSet();
assertFalse(rs.next());
// end the work on the transaction branch
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.prepare(xid);
xaRes.commit(xid, false);
stm.execute("select * from XATT2");
rs = stm.getResultSet();
assertTrue(rs.next());
}
示例10: testBug72890
import javax.sql.XAConnection; //导入方法依赖的package包/类
/**
* Tests fix for BUG#72890 - Java jdbc driver returns incorrect return code when it's part of XA transaction
*
* @throws Exception
* if the test fails.
*/
public void testBug72890() throws Exception {
MysqlXADataSource myDs = new MysqlXADataSource();
myDs.setUrl(BaseTestCase.dbUrl);
final Xid xid = new MysqlXid("72890".getBytes(), "72890".getBytes(), 1);
final XAConnection xaConn = myDs.getXAConnection();
final XAResource xaRes = xaConn.getXAResource();
final Connection dbConn = xaConn.getConnection();
final long connId = ((MySQLConnection) ((com.mysql.jdbc.Connection) dbConn).getConnectionMutex()).getId();
xaRes.start(xid, XAResource.TMNOFLAGS);
xaRes.end(xid, XAResource.TMSUCCESS);
assertEquals(XAResource.XA_OK, xaRes.prepare(xid));
// Simulate a connection hang
this.stmt.execute("KILL CONNECTION " + connId);
XAException xaEx = assertThrows(XAException.class, "Undetermined error occurred in the underlying Connection - check your data for consistency",
new Callable<Void>() {
public Void call() throws Exception {
xaRes.commit(xid, false);
return null;
}
});
assertEquals("XAException error code", XAException.XAER_RMFAIL, xaEx.errorCode);
dbConn.close();
xaConn.close();
}
示例11: ConnectionWrapperImpl
import javax.sql.XAConnection; //导入方法依赖的package包/类
public ConnectionWrapperImpl(final XAConnection xaConnection, final Connection physicalConnection, final ConnectionPool connectionPool,
final Transaction tx) throws SQLException {
this.xaConnection = xaConnection;
this.physicalConnection = physicalConnection;
this.xaResource = new XAResourceWrapperImpl(xaConnection.getXAResource(), this);
this.connectionPool = connectionPool;
this.tx = tx;
this.xaConnection.addConnectionEventListener(this);
}
示例12: testRecover
import javax.sql.XAConnection; //导入方法依赖的package包/类
@Test
public void testRecover() throws Exception {
XAConnection xaConnection = dataSource.getXAConnection();
try {
Connection connection = xaConnection.getConnection();
Xid xid = newXid();
XAResource xaResource = xaConnection.getXAResource();
xaResource.start(xid, XAResource.TMNOFLAGS);
connection.createStatement().executeQuery("SELECT 1");
xaResource.end(xid, XAResource.TMSUCCESS);
xaResource.prepare(xid);
Xid[] recoveredXids = xaResource.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN);
assertTrue(recoveredXids != null);
assertTrue(recoveredXids.length > 0);
boolean found = false;
for (Xid x : recoveredXids) {
if (x != null && x.equals(xid)) {
found = true;
break;
}
}
assertTrue(found);
} finally {
xaConnection.close();
}
}
示例13: testSuspendableTx
import javax.sql.XAConnection; //导入方法依赖的package包/类
public void testSuspendableTx() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
Connection conn1 = null;
MysqlXADataSource suspXaDs = new MysqlXADataSource();
suspXaDs.setUrl(BaseTestCase.dbUrl);
suspXaDs.setPinGlobalTxToPhysicalConnection(true);
suspXaDs.setRollbackOnPooledClose(true);
XAConnection xaConn1 = null;
Xid xid = createXid();
try {
/*
* -- works using RESUME
* xa start 0x123,0x456;
* select * from foo;
* xa end 0x123,0x456;
* xa start 0x123,0x456 resume;
* select * from foo;
* xa end 0x123,0x456;
* xa commit 0x123,0x456 one phase;
*/
xaConn1 = suspXaDs.getXAConnection();
XAResource xaRes1 = xaConn1.getXAResource();
conn1 = xaConn1.getConnection();
xaRes1.start(xid, XAResource.TMNOFLAGS);
conn1.createStatement().execute("SELECT 1");
xaRes1.end(xid, XAResource.TMSUCCESS);
xaRes1.start(xid, XAResource.TMRESUME);
conn1.createStatement().execute("SELECT 1");
xaRes1.end(xid, XAResource.TMSUCCESS);
xaRes1.commit(xid, true);
xaConn1.close();
/*
*
* -- fails using JOIN
* xa start 0x123,0x456;
* select * from foo;
* xa end 0x123,0x456;
* xa start 0x123,0x456 join;
* select * from foo;
* xa end 0x123,0x456;
* xa commit 0x123,0x456 one phase;
*/
xaConn1 = suspXaDs.getXAConnection();
xaRes1 = xaConn1.getXAResource();
conn1 = xaConn1.getConnection();
xaRes1.start(xid, XAResource.TMNOFLAGS);
conn1.createStatement().execute("SELECT 1");
xaRes1.end(xid, XAResource.TMSUCCESS);
xaRes1.start(xid, XAResource.TMJOIN);
conn1.createStatement().execute("SELECT 1");
xaRes1.end(xid, XAResource.TMSUCCESS);
xaRes1.commit(xid, true);
} finally {
if (xaConn1 != null) {
xaConn1.close();
}
}
}
示例14: testBug72890
import javax.sql.XAConnection; //导入方法依赖的package包/类
/**
* Tests fix for BUG#72890 - Java jdbc driver returns incorrect return code when it's part of XA transaction
*
* @throws Exception
* if the test fails.
*/
public void testBug72890() throws Exception {
MysqlXADataSource myDs = new MysqlXADataSource();
myDs.setUrl(BaseTestCase.dbUrl);
try {
final Xid xid = new MysqlXid("72890".getBytes(), "72890".getBytes(), 1);
final XAConnection xaConn = myDs.getXAConnection();
final XAResource xaRes = xaConn.getXAResource();
final Connection dbConn = xaConn.getConnection();
final long connId = ((MySQLConnection) ((com.mysql.jdbc.Connection) dbConn).getConnectionMutex()).getId();
xaRes.start(xid, XAResource.TMNOFLAGS);
xaRes.end(xid, XAResource.TMSUCCESS);
assertEquals(XAResource.XA_OK, xaRes.prepare(xid));
// Simulate a connection hang and make sure the connection really dies.
this.stmt.execute("KILL CONNECTION " + connId);
int connAliveChecks = 4;
while (connAliveChecks > 0) {
this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");
boolean connIsAlive = false;
while (!connIsAlive && this.rs.next()) {
connIsAlive = this.rs.getInt(1) == connId;
}
this.rs.close();
if (connIsAlive) {
connAliveChecks--;
System.out.println("Connection id " + connId + " is still alive. Checking " + connAliveChecks + " more times.");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
} else {
connAliveChecks = -1;
}
}
if (connAliveChecks == 0) {
fail("Failed to kill the Connection id " + connId + " in a timely manner.");
}
XAException xaEx = assertThrows(XAException.class, "Undetermined error occurred in the underlying Connection - check your data for consistency",
new Callable<Void>() {
public Void call() throws Exception {
xaRes.commit(xid, false);
return null;
}
});
assertEquals("XAException error code", XAException.XAER_RMFAIL, xaEx.errorCode);
dbConn.close();
xaConn.close();
} finally {
/*
* After MySQL 5.7.7 a prepared XA transaction is no longer rolled back at disconnect. It needs to be rolled back manually to prevent test failures
* in subsequent runs.
* Other MySQL versions won't have any transactions to recover.
*/
final XAConnection xaConnRecovery = myDs.getXAConnection();
final XAResource xaResRecovery = xaConnRecovery.getXAResource();
final Xid[] xidsToRecover = xaResRecovery.recover(XAResource.TMSTARTRSCAN);
for (Xid xidToRecover : xidsToRecover) {
xaResRecovery.rollback(xidToRecover);
}
xaConnRecovery.close();
}
}
示例15: testRollbackWithoutPrepare
import javax.sql.XAConnection; //导入方法依赖的package包/类
private void testRollbackWithoutPrepare() throws Exception {
if (config.memory) {
return;
}
Xid xid = new Xid() {
@Override
public int getFormatId() {
return 3145;
}
@Override
public byte[] getGlobalTransactionId() {
return new byte[] { 1, 2, 3, 4, 5, 6, 6, 7, 8 };
}
@Override
public byte[] getBranchQualifier() {
return new byte[] { 34, 43, 33, 3, 3, 3, 33, 33, 3 };
}
};
deleteDb("xa");
JdbcDataSource ds = new JdbcDataSource();
ds.setURL(getURL("xa", true));
ds.setPassword(getPassword());
Connection dm = ds.getConnection();
Statement stat = dm.createStatement();
stat.execute("CREATE TABLE IF NOT EXISTS TEST(ID INT PRIMARY KEY, VAL INT)");
stat.execute("INSERT INTO TEST(ID,VAL) VALUES (1,1)");
dm.close();
XAConnection c = ds.getXAConnection();
XAResource xa = c.getXAResource();
Connection connection = c.getConnection();
xa.start(xid, XAResource.TMJOIN);
PreparedStatement ps = connection.prepareStatement(
"UPDATE TEST SET VAL=? WHERE ID=?");
ps.setInt(1, new Random().nextInt());
ps.setInt(2, 1);
ps.close();
xa.rollback(xid);
connection.close();
c.close();
deleteDb("xa");
}