本文整理汇总了Java中javax.sql.XAConnection.getConnection方法的典型用法代码示例。如果您正苦于以下问题:Java XAConnection.getConnection方法的具体用法?Java XAConnection.getConnection怎么用?Java XAConnection.getConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sql.XAConnection
的用法示例。
在下文中一共展示了XAConnection.getConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testConnectionFlowCommit
import javax.sql.XAConnection; //导入方法依赖的package包/类
/**
* check whether commit without statement will flow by checking its transaction id
* on client. This test is run only for client where commits without an
* active transactions will not flow to the server.
* DERBY-4653
*
* @throws SQLException
**/
public void testConnectionFlowCommit()
throws SQLException {
ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
PooledConnection pc = ds.getPooledConnection();
Connection conn = pc.getConnection();
testConnectionFlowCommitWork(conn, 1);
conn.close();
//Test for XADataSource
XADataSource xs = J2EEDataSource.getXADataSource();
XAConnection xc = xs.getXAConnection();
conn = xc.getConnection();
testConnectionFlowCommitWork(conn, 1);
conn.close();
//Test for DataSource
DataSource jds = JDBCDataSource.getDataSource();
conn = jds.getConnection();
testConnectionFlowCommitWork(conn, 1);
conn.close();
}
示例2: 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);
}
示例3: testSetSchemaInXAConnection
import javax.sql.XAConnection; //导入方法依赖的package包/类
public void testSetSchemaInXAConnection() throws SQLException {
// tests that set schema works correctly in an XA connection.
XADataSource dsx = J2EEDataSource.getXADataSource();
XAConnection xac3 = dsx.getXAConnection();
Connection conn3 = xac3.getConnection();
Statement st3 = conn3.createStatement();
st3.execute("SET SCHEMA SCHEMA_Patricio");
st3.close();
PreparedStatement ps3 =
conn3.prepareStatement("INSERT INTO Patricio VALUES (?, ?)");
ps3.setString(1, "Patricio");
ps3.setInt(2, 3);
ps3.executeUpdate();
assertEquals(1, ps3.getUpdateCount());
ps3.close();
conn3.close();
xac3.close();
}
示例4: testXAAutoCommit
import javax.sql.XAConnection; //导入方法依赖的package包/类
private void testXAAutoCommit() throws Exception {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:test");
ds.setUser("sa");
ds.setPassword("");
XAConnection xa = ds.getXAConnection();
MyXid xid = new MyXid();
xa.getXAResource().start(xid,
XAResource.TMNOFLAGS);
Connection c = xa.getConnection();
assertTrue(!c.getAutoCommit());
c.close();
xa.close();
}
示例5: 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);
}
}
示例6: 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());
}
示例7: embeddedTestAttributesAsPasswordWithoutPassword_xa
import javax.sql.XAConnection; //导入方法依赖的package包/类
/**
* Tests that the default password is not sent as an attribute string when
* <code>attributesAsPassword</code> is <code>true</code>. The test is run
* with an <code>XADataSource</code>.
*/
public void embeddedTestAttributesAsPasswordWithoutPassword_xa()
throws Exception
{
XADataSource ds = J2EEDataSource.getXADataSource();
JDBCDataSource.setBeanProperty(ds, "password", "mypassword");
JDBCDataSource.setBeanProperty(ds, "attributesAsPassword", Boolean.TRUE);
XAConnection xa = ds.getXAConnection();
Connection c = xa.getConnection();
c.close();
}
示例8: testCloseActiveConnection_XA_local
import javax.sql.XAConnection; //导入方法依赖的package包/类
/**
* Test that connections retrieved from {@code XADataSource} that are not
* part of a global XA transaction, behave as expected when {@code close()}
* is called and the transaction is active.
*/
public void testCloseActiveConnection_XA_local() throws SQLException {
XADataSource ds = J2EEDataSource.getXADataSource();
XAConnection xa = ds.getXAConnection();
testCloseActiveConnection(xa.getConnection(), true, false);
Connection c = xa.getConnection();
c.setAutoCommit(false);
testCloseActiveConnection(c, false, false);
}
示例9: testClosedXADSConnection
import javax.sql.XAConnection; //导入方法依赖的package包/类
public void testClosedXADSConnection() throws SQLException, Exception {
// verify that outstanding updates from a closed connection, obtained
// from an XADataSource, are not committed, but rolled back.
XADataSource dsx = J2EEDataSource.getXADataSource();
XAConnection xac = dsx.getXAConnection();
Connection c1 = xac.getConnection();
Statement s = c1.createStatement();
c1.setAutoCommit(false);
// this update should be rolled back
s.executeUpdate("insert into intTable values(2)");
c1 = xac.getConnection();
ResultSet rs = c1.createStatement().executeQuery(
"select count(*) from intTable");
rs.next();
assertEquals(0, rs.getInt(1));
rs.close();
c1.close();
xac.close();
xac = null;
PoolReset("XADataSource", dsx.getXAConnection());
}
示例10: 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();
}
}
示例11: runSql
import javax.sql.XAConnection; //导入方法依赖的package包/类
private static void runSql(XAConnection xamysql, String username) throws SQLException {
try(Connection conn = xamysql.getConnection()){
try(PreparedStatement stmt = conn.prepareStatement("insert into person(id, name) select max(id)+1, ? from person")){
stmt.setString(1, username);
stmt.executeUpdate();
}
}
}
示例12: createConnection
import javax.sql.XAConnection; //导入方法依赖的package包/类
protected ConnectionWrapper createConnection(Transaction tx) throws SQLException {
final XAConnection xaConn = xaDataSource.getXAConnection();
final Connection conn = xaConn.getConnection();
final ConnectionWrapper wrapper = createTransactionalConnectionWrapper(xaConn, conn, tx);
if (logger.isDebugEnabled()) {
logger.debug("Created physical connection: tx={}, conn={}", tx, conn);
}
return wrapper;
}
示例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: testReadOnlyToWritableTran
import javax.sql.XAConnection; //导入方法依赖的package包/类
public void testReadOnlyToWritableTran() throws SQLException, Exception
{
// This fixture will run twice, once with embedded, once with client,
// and insert 2 rows in addition to the 5 rows inserted during setup.
// The fixture tests a commit, so before running, try to remove row
// 6 and 7 in case this is the second run of the fixture.
Statement s = createStatement();
s.executeUpdate("delete from autocommitxastart where i = 6");
s.executeUpdate("delete from autocommitxastart where i = 7");
// TESTING READ_ONLY TRANSACTION FOLLOWED BY WRITABLE TRANSACTION
// Test following sequence of steps
// 1)start a read-only global transaction
// 2)finish that read-only transaction
// 3)start another global transaction
XADataSource dsx = J2EEDataSource.getXADataSource();
XAConnection xac5 = dsx.getXAConnection();
Xid xid5a = new cdsXid(5, (byte) 119, (byte) 129);
Connection conn5 = xac5.getConnection();
Statement sru5a = conn5.createStatement();
XAResource xar = xac5.getXAResource();
xar.start(xid5a, XAResource.TMNOFLAGS);
conn5.setReadOnly(true);
// Read-Only XA transaction;
// holdability: (hold, or close cursors over commit) ,
// transaction isolation: read-committed,
// auto-commit false, read-only true (with embedded)
if (usingEmbedded())
{
assertConnectionState(
ResultSet.CLOSE_CURSORS_AT_COMMIT,
Connection.TRANSACTION_READ_COMMITTED,
false, true, conn5);
}
// Note: the original test had no comments about this difference
// between Embedded and DerbyNetClient, this has apparently
// been accepted behavior.
else if (usingDerbyNetClient())
{
assertConnectionState(
ResultSet.CLOSE_CURSORS_AT_COMMIT,
Connection.TRANSACTION_READ_COMMITTED,
false, false, conn5);
}
ResultSet rs5 = sru5a.executeQuery(
"select count(*) from autocommitxastart");
rs5.next();
assertEquals(5, rs5.getInt(1));
rs5.close();
xar.end(xid5a, XAResource.TMSUCCESS);
xar.commit(xid5a, true);
conn5.close();
//now start a new transaction
conn5 = xac5.getConnection();
sru5a = conn5.createStatement();
xar.start(xid5a, XAResource.TMNOFLAGS);
// Writeable XA transaction
// holdability: (hold, or close cursors over commit) ,
// transaction isolation: read-committed,
// auto-commit false, read-only false
assertConnectionState(
ResultSet.CLOSE_CURSORS_AT_COMMIT,
Connection.TRANSACTION_READ_COMMITTED,
false, false, conn5);
sru5a.executeUpdate("insert into autocommitxastart values 6,7");
rs5 = sru5a.executeQuery("select count(*) from autocommitxastart");
rs5.next();
assertEquals(7, rs5.getInt(1));
xar.end(xid5a, XAResource.TMSUCCESS);
xar.commit(xid5a, true);
conn5.close();
xac5.close();
sru5a.close();
}