/** * Get new XAConnection connection, to be managed by a connection manager. */ public XAConnection getXAConnection() throws SQLException { // Comment out before public release: /* System.err.print("Executing " + getClass().getName() + ".getXAConnection()..."); */ // Use JDBCDriver directly so there is no need to register with DriverManager JDBCConnection connection = (JDBCConnection) JDBCDriver.getConnection(url, connectionProps); JDBCXAConnection xaConnection = new JDBCXAConnection(this, connection); return xaConnection; }
public void testBug46925() throws Exception { MysqlXADataSource xads1 = new MysqlXADataSource(); MysqlXADataSource xads2 = new MysqlXADataSource(); Xid txid = new MysqlXid(new byte[] { 0x1 }, new byte[] { 0xf }, 3306); xads1.setPinGlobalTxToPhysicalConnection(true); xads1.setUrl(dbUrl); xads2.setPinGlobalTxToPhysicalConnection(true); xads2.setUrl(dbUrl); XAConnection c1 = xads1.getXAConnection(); assertTrue(c1 instanceof SuspendableXAConnection); // start a transaction on one connection c1.getXAResource().start(txid, XAResource.TMNOFLAGS); c1.getXAResource().end(txid, XAResource.TMSUCCESS); XAConnection c2 = xads2.getXAConnection(); assertTrue(c2 instanceof SuspendableXAConnection); // prepare on another one. Since we are using a "pinned" connection we should have the same "currentXAConnection" for both SuspendableXAConnection c2.getXAResource().prepare(txid); // this will fail without the fix. c2.getXAResource().commit(txid, false); }
/** * Tests fix for BUG#69506 - XAER_DUPID error code is not returned when a duplicate XID is offered in Java. * * @throws Exception * if the test fails. */ public void testBug69506() throws Exception { MysqlXADataSource dataSource = new MysqlXADataSource(); dataSource.setUrl(dbUrl); XAConnection testXAConn1 = dataSource.getXAConnection(); XAConnection testXAConn2 = dataSource.getXAConnection(); Xid duplicateXID = new MysqlXid("1".getBytes(), "1".getBytes(), 1); testXAConn1.getXAResource().start(duplicateXID, 0); try { testXAConn2.getXAResource().start(duplicateXID, 0); fail("XAException was expected."); } catch (XAException e) { assertEquals("Wrong error code retured for duplicated XID.", XAException.XAER_DUPID, e.errorCode); } }
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(); } }
/** * Get new XAConnection connection, to be managed by a connection manager. */ public XAConnection getXAConnection() throws SQLException { // Comment out before public release: /* System.err.print("Executing " + getClass().getName() + ".getXAConnection()..."); */ // Use JDBCDriver directly so there is no need to regiser with DriverManager JDBCConnection connection = (JDBCConnection) JDBCDriver.getConnection(url, connectionProps); JDBCXAConnection xaConnection = new JDBCXAConnection(this, connection); return xaConnection; }
/** * Implementation of call back function from ConnectionEventListener interface. This callback will * be invoked on connection close event. * * @param event Connection event object */ public void connectionClosed(ConnectionEvent event) { if (isActive) { try { XAConnection conn = (XAConnection) event.getSource(); XAResource xar = (XAResource) xaResourcesMap.get(conn); xaResourcesMap.remove(conn); Transaction txn = transManager.getTransaction(); if (txn != null && xar != null) txn.delistResource(xar, XAResource.TMSUCCESS); provider.returnConnection(conn); } catch (Exception e) { String exception = "GemFireTransactionDataSource::connectionClosed: Exception occured due to " + e; if (logger.isDebugEnabled()) { logger.debug(exception, e); } } } }
/** * */ 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; } }
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(); }
/** * 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); }
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(); }
/** * 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(); }
@Test public void shouldGetConnectionAndCommit() throws SQLException { Connection mockConnection = mock(Connection.class); XAConnection mockXaConnection = mock(XAConnection.class); given(mockXaConnection.getConnection()).willReturn(mockConnection); given(this.dataSource.getXAConnection()).willReturn(mockXaConnection); Properties properties = new Properties(); properties.put(TransactionalDriver.XADataSource, this.dataSource); Connection connection = this.dataSourceBean.getConnection(); assertThat(connection).isInstanceOf(ConnectionImple.class); connection.commit(); verify(this.dataSource, times(1)).getXAConnection(); verify(mockXaConnection, times(1)).getConnection(); verify(mockConnection, times(1)).commit(); }
@Test public void shouldGetConnectionAndCommitWithCredentials() throws SQLException { String username = "testUsername"; String password = "testPassword"; Connection mockConnection = mock(Connection.class); XAConnection mockXaConnection = mock(XAConnection.class); given(mockXaConnection.getConnection()).willReturn(mockConnection); given(this.dataSource.getXAConnection(username, password)) .willReturn(mockXaConnection); Properties properties = new Properties(); properties.put(TransactionalDriver.XADataSource, this.dataSource); properties.put(TransactionalDriver.userName, username); properties.put(TransactionalDriver.password, password); Connection connection = this.dataSourceBean.getConnection(username, password); assertThat(connection).isInstanceOf(ConnectionImple.class); connection.commit(); verify(this.dataSource, times(1)).getXAConnection(username, password); verify(mockXaConnection, times(1)).getConnection(); verify(mockConnection, times(1)).commit(); }
public XAConnection getXAConnection(String user, String password) throws SQLException { final Properties info = new Properties(); info.putAll(properties); if (!isEmpty(user)) { info.put("user", user); } if (!isEmpty(password)) { info.put("password", password); } int currentLoginTimeout = DriverManager.getLoginTimeout(); try { DriverManager.setLoginTimeout(loginTimeout); final Connection con = DriverManager.getConnection(url, info); return new XAConnectionImpl(con); } finally { try { DriverManager.setLoginTimeout(currentLoginTimeout); } catch (Exception e) { logger.info("Failed to set login timeout: currentLoginTimeout=" + currentLoginTimeout, e); } } }
public void testBug46925() throws Exception { MysqlXADataSource xads1 = new MysqlXADataSource(); MysqlXADataSource xads2 = new MysqlXADataSource(); Xid txid = new MysqlXid(new byte[] { 0x1 }, new byte[] { 0xf }, 3306); xads1.setPinGlobalTxToPhysicalConnection(true); xads1.setUrl(dbUrl); xads2.setPinGlobalTxToPhysicalConnection(true); xads2.setUrl(dbUrl); XAConnection c1 = xads1.getXAConnection(); assertTrue(c1 instanceof SuspendableXAConnection); // start a transaction on one connection c1.getXAResource().start(txid, XAResource.TMNOFLAGS); c1.getXAResource().end(txid, XAResource.TMSUCCESS); XAConnection c2 = xads2.getXAConnection(); assertTrue(c2 instanceof SuspendableXAConnection); // prepare on another one. Since we are using a "pinned" connection // we should have the same "currentXAConnection" for both // SuspendableXAConnection c2.getXAResource().prepare(txid); // this will fail without the fix. c2.getXAResource().commit(txid, false); }