Java Code Examples for javax.transaction.xa.Xid#equals()

The following examples show how to use javax.transaction.xa.Xid#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SuspendableXAConnection.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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();
    }
}
 
Example 2
Source File: LocalXAResourceImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void commit(Xid xid, boolean onePhase) throws XAException
{
   if (!xid.equals(currentXid))
   {
      throw new LocalXAException(bundle.wrongXidInCommit(currentXid, xid), XAException.XAER_PROTO);
      
   }
   
   currentXid = null;

   try
   {
      cl.getManagedConnection().getLocalTransaction().commit();
   }
   catch (LocalResourceException lre)
   {
      connectionManager.returnManagedConnection(cl, true);
      throw new LocalXAException(bundle.couldNotCommitLocalTx(), XAException.XAER_RMFAIL, lre);
   }
   catch (ResourceException re)
   {
      connectionManager.returnManagedConnection(cl, true);
      throw new LocalXAException(bundle.couldNotCommitLocalTx(), XAException.XA_RBROLLBACK, re);
   }
}
 
Example 3
Source File: LocalXAResourceImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void rollback(Xid xid) throws XAException
{
   if (!xid.equals(currentXid))
   {
      throw new LocalXAException(bundle.wrongXidInRollback(currentXid, xid), XAException.XAER_PROTO);  
   }
   currentXid = null;
   try
   {
      cl.getManagedConnection().getLocalTransaction().rollback();
   }
   catch (LocalResourceException lre)
   {
      connectionManager.returnManagedConnection(cl, true);
      throw new LocalXAException(bundle.couldNotRollbackLocalTx(), XAException.XAER_RMFAIL, lre);
   }
   catch (ResourceException re)
   {
      connectionManager.returnManagedConnection(cl, true);
      throw new LocalXAException(bundle.couldNotRollbackLocalTx(), XAException.XAER_RMERR, re);
   }
}
 
Example 4
Source File: SuspendableXAConnection.java    From r-course with MIT License 6 votes vote down vote up
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();
    }
}
 
Example 5
Source File: JDBCXAResource.java    From evosql with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @throws XAException if the given Xid is the not the Xid of the current
 *   transaction for this XAResource object.
 * @param xid Xid
 */
private void validateXid(Xid xid) throws XAException {

    if (xid == null) {
        throw new XAException("Null Xid");
    }

    if (this.xid == null) {
        throw new XAException(
            "There is no live transaction for this XAResource");
    }

    if (!xid.equals(this.xid)) {
        throw new XAException(
            "Given Xid is not that associated with this XAResource object");
    }
}
 
Example 6
Source File: SuspendableXAConnection.java    From Komondor with GNU General Public License v3.0 6 votes vote down vote up
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();
    }
}
 
Example 7
Source File: SuspendableXAConnection.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
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();
    }
}
 
Example 8
Source File: LocalXAResourceImpl.java    From ironjacamar with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void commit(Xid xid, boolean onePhase) throws XAException
{
   if (!xid.equals(currentXid))
   {
      throw new LocalXAException(bundle.wrongXidInCommit(currentXid, xid), XAException.XAER_PROTO);
      
   }
   
   currentXid = null;

   try
   {
      cl.getManagedConnection().getLocalTransaction().commit();
   }
   catch (LocalResourceException lre)
   {
      connectionManager.returnConnectionListener(cl, true);
      throw new LocalXAException(bundle.couldNotCommitLocalTx(), XAException.XAER_RMFAIL, lre);
   }
   catch (ResourceException re)
   {
      connectionManager.returnConnectionListener(cl, true);
      throw new LocalXAException(bundle.couldNotCommitLocalTx(), XAException.XA_RBROLLBACK, re);
   }
}
 
Example 9
Source File: LocalXAResourceImpl.java    From ironjacamar with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void rollback(Xid xid) throws XAException
{
   if (!xid.equals(currentXid))
   {
      throw new LocalXAException(bundle.wrongXidInRollback(currentXid, xid), XAException.XAER_PROTO);  
   }
   currentXid = null;
   try
   {
      cl.getManagedConnection().getLocalTransaction().rollback();
   }
   catch (LocalResourceException lre)
   {
      connectionManager.returnConnectionListener(cl, true);
      throw new LocalXAException(bundle.couldNotRollbackLocalTx(), XAException.XAER_RMFAIL, lre);
   }
   catch (ResourceException re)
   {
      connectionManager.returnConnectionListener(cl, true);
      throw new LocalXAException(bundle.couldNotRollbackLocalTx(), XAException.XAER_RMERR, re);
   }
}
 
Example 10
Source File: DistributedTransactionTest.java    From mariadb-connector-j with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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();
  }
}
 
Example 11
Source File: LocalXAConnectionFactory.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
/**
 * Clears the currently associated transaction if it is the specified xid.
 *
 * @param xid
 *            the id of the transaction to forget
 */
@Override
public synchronized void forget(final Xid xid) {
    if (xid != null && xid.equals(currentXid)) {
        currentXid = null;
    }
}
 
Example 12
Source File: LocalXAConnectionFactory.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
/**
 * Signals that a the connection has been enrolled in a transaction. This method saves off the current auto
 * commit flag, and then disables auto commit. The original auto commit setting is restored when the transaction
 * completes.
 *
 * @param xid
 *            the id of the transaction branch for this connection
 * @param flag
 *            either XAResource.TMNOFLAGS or XAResource.TMRESUME
 * @throws XAException
 *             if the connection is already enlisted in another transaction, or if auto-commit could not be
 *             disabled
 */
@Override
public synchronized void start(final Xid xid, final int flag) throws XAException {
    if (flag == XAResource.TMNOFLAGS) {
        // first time in this transaction

        // make sure we aren't already in another tx
        if (this.currentXid != null) {
            throw new XAException("Already enlisted in another transaction with xid " + xid);
        }

        // save off the current auto commit flag so it can be restored after the transaction completes
        try {
            originalAutoCommit = connection.getAutoCommit();
        } catch (final SQLException ignored) {
            // no big deal, just assume it was off
            originalAutoCommit = true;
        }

        // update the auto commit flag
        try {
            connection.setAutoCommit(false);
        } catch (final SQLException e) {
            throw (XAException) new XAException("Count not turn off auto commit for a XA transaction")
                    .initCause(e);
        }

        this.currentXid = xid;
    } else if (flag == XAResource.TMRESUME) {
        if (!xid.equals(this.currentXid)) {
            throw new XAException("Attempting to resume in different transaction: expected " + this.currentXid
                    + ", but was " + xid);
        }
    } else {
        throw new XAException("Unknown start flag " + flag);
    }
}
 
Example 13
Source File: JDBCXAResource.java    From evosql with Apache License 2.0 4 votes vote down vote up
public void start(Xid xid, int flags) throws XAException {

        // Comment out following debug statement before public release:
/*
        System.err.println("STARTING NEW Xid: " + xid);
*/
        if (state != XA_STATE_INITIAL && state != XA_STATE_DISPOSED
                && state != XA_STATE_ENDED) {
            throw new XAException("Invalid XAResource state");
        }

        if (xaDataSource == null) {
            throw new XAException(
                "JDBCXAResource has not been associated with a XADataSource");
        }

        if (xid == null) {

            // This block asserts that all JDBCXAResources with state
            // >= XA_STATE_STARTED have a non-null xid.
            throw new XAException("Null Xid");
        }

        try {
            if (connection.getAutoCommit()) {
                originalAutoCommitMode = true;      // real/phys.

                connection.setAutoCommit(false);    // real/phys.
            }
        } catch (SQLException se) {
            throw new XAException(se.toString());
        }

        if (!xid.equals(this.xid)) {
            this.xid = xid;

            xaDataSource.addResource(this.xid, this);
        }

        state = XA_STATE_STARTED;

        // N.b.  The DataSource does not have this XAResource in its list
        // until right here.  We can't tell DataSource before our start()
        // method, because we don't know our Xid before now.
    }
 
Example 14
Source File: ClientAbstract.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public final void connect() {
   while (running) {
      try {
         disconnect();

         session = sf.createXASession();

         if (activeXid != null) {
            synchronized (ClientAbstract.class) {
               Xid[] xids = session.recover(XAResource.TMSTARTRSCAN);
               boolean found = false;
               for (Xid recXid : xids) {
                  if (recXid.equals(activeXid)) {
                     // System.out.println("Calling commit after a prepare on " + this);
                     found = true;
                     callCommit();
                  }
               }

               if (!found) {
                  if (pendingCommit) {
                     onCommit();
                  } else {
                     onRollback();
                  }

                  activeXid = null;
                  pendingCommit = false;
               }
            }
         }

         connectClients();

         break;
      } catch (Exception e) {
         ClientAbstract.log.warn("Can't connect to server, retrying");
         disconnect();
         try {
            Thread.sleep(1000);
         } catch (InterruptedException ignored) {
            // if an interruption was sent, we will respect it and leave the loop
            break;
         }
      }
   }
}
 
Example 15
Source File: TestFBXAResource.java    From jaybird with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testRecover() throws Exception {
    try (Connection connection = getConnectionViaDriverManager();
         Statement stmt = connection.createStatement()) {
        try {
            stmt.execute("DROP TABLE test_reconnect");
        } catch (SQLException ex) {
            // empty
        }

        stmt.execute("CREATE TABLE test_reconnect(id INTEGER)");
    }

    FBManagedConnectionFactory mcf = initMcf();

    Xid xid1 = new XidImpl();

    FBManagedConnection mc1 = mcf.createManagedConnection();
    try {
        XAResource xa1 = mc1.getXAResource();

        xa1.start(xid1, XAResource.TMNOFLAGS);

        Connection fbc1 = mc1.getConnection();
        try (Statement fbstmt1 = fbc1.createStatement()) {
            fbstmt1.execute("INSERT INTO test_reconnect(id) VALUES(1)");
        }

        xa1.end(xid1, XAResource.TMSUCCESS);
        xa1.prepare(xid1);
    } finally {
        // kill connection after prepare.
        mc1.destroy();
    }

    FBManagedConnectionFactory mcf2 = initMcf();

    FBManagedConnection mc2 = mcf2.createManagedConnection();
    try {
        XAResource xa2 = mc2.getXAResource();

        Xid xid2 = new XidImpl();
        xa2.start(xid2, XAResource.TMNOFLAGS);

        Xid[] xids = xa2.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN);

        xa2.end(xid2, XAResource.TMSUCCESS);
        xa2.commit(xid2, true);

        assertNotNull("Should recover non-null array", xids);
        assertTrue("Should recover at least one transaction", xids.length > 0);

        boolean found = false;
        for (Xid xid : xids) {
            if (xid.equals(xid1)) {
                found = true;
                break;
            }
        }

        assertTrue("Should find our transaction", found);

        xa2.commit(xid1, false);
    } finally {
        mc2.destroy();
    }

    try (Connection connection = getConnectionViaDriverManager();
         Statement stmt = connection.createStatement()) {
        ResultSet rs = stmt.executeQuery("SELECT * FROM test_reconnect");
        assertTrue("Should find at least one row.", rs.next());
        assertEquals("Should read correct value", 1, rs.getInt(1));
        assertFalse("Should select only one row", rs.next());
    }
}