Class JdbcCpoXaAdapter

All Implemented Interfaces:
Serializable, AutoCloseable, XAResource, CpoAdapter, CpoTrxAdapter, CpoXaResource

public class JdbcCpoXaAdapter extends CpoBaseXaResource<JdbcCpoAdapter> implements CpoTrxAdapter
A JdbcCpoXaAdapter wraps the XAResource processing around a JdbcCpoTrxAdapter.

It manages a local and global CpoTrxAdapter and manages the association of XIDs to transactions

The XAResource interface is a Java mapping of the industry standard XA interface based on the X/Open CAE Specification (Distributed Transaction Processing: The XA Specification).

The XA interface defines the contract between a Resource Manager and a Transaction Manager in a distributed transaction processing (DTP) environment. A JDBC driver or a JMS provider implements this interface to support the association between a global transaction and a database or message service connection.

The XAResource interface can be supported by any transactional resource that is intended to be used by application programs in an environment where transactions are controlled by an external transaction manager. An example of such a resource is a database management system. An application may access data through multiple database connections. Each database connection is enlisted with the transaction manager as a transactional resource. The transaction manager obtains an XAResource for each connection participating in a global transaction. The transaction manager uses the start method to associate the global transaction with the resource, and it uses the end method to disassociate the transaction from the resource. The resource manager is responsible for associating the global transaction to all work performed on its data between the start and end method invocations.

At transaction commit time, the resource managers are informed by the transaction manager to prepare, commit, or rollback a transaction according to the two-phase commit protocol.

See Also:
  • Constructor Details

    • JdbcCpoXaAdapter

      public JdbcCpoXaAdapter(JdbcCpoAdapterFactory jdbcCpoAdapterFactory) throws CpoException
      Construct a JdbcCpoXaAdapter
      Parameters:
      jdbcCpoAdapterFactory - - The adapter factory to use to create the JdbcCpoXaAdapter
      Throws:
      CpoException - - An error getting the JdbcCpoXaAdapter
  • Method Details

    • commit

      public void commit() throws CpoException
      Commits the current transaction behind the CpoTrxAdapter
      Specified by:
      commit in interface CpoTrxAdapter
      Throws:
      CpoException - An error occurred
    • rollback

      public void rollback() throws CpoException
      Rollback the current transaction behind the CpoTrxAdapter
      Specified by:
      rollback in interface CpoTrxAdapter
      Throws:
      CpoException - An error occurred
    • close

      public void close() throws CpoException
      Closes the current transaction behind the CpoTrxAdapter. All subsequent calls to the CpoTrxAdapter will throw an exception.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface CpoTrxAdapter
      Throws:
      CpoException
    • isClosed

      public boolean isClosed() throws CpoException
      Returns true if the TrxAdapter has been closed, false if it is still active
      Specified by:
      isClosed in interface CpoTrxAdapter
      Returns:
      true if it is closed
      Throws:
      CpoException - An error occurred
    • insertBean

      public <T> long insertBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Creates the bean in the datasource using the query's CREATE function group. The assumption is that the bean does not exist in the datasource.
      
       SomeBean bean = new SomeBean();
       bean.setId(1);
       bean.setName("SomeName");
       cpo.insertBean(CpoQuery.group("createBean"), bean);
       
      Specified by:
      insertBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource
      Returns:
      The number of beans created in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • insertBeans

      public <T> long insertBeans(CpoQuery query, List<T> beans) throws CpoException
      Description copied from interface: CpoAdapter
      Creates the beans in the datasource using the query's CREATE function group, batching where the datasource supports it. This is an all-or-nothing transaction: if one bean fails, no beans are created.
      
       List<SomeBean> beans = List.of(bean1, bean2);
       long created = cpo.insertBeans(CpoQuery.group("createBean"), beans);
       
      Specified by:
      insertBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      beans - The beans to create
      Returns:
      The number of beans created in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • deleteBean

      public <T> long deleteBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Removes the bean from the datasource using the query's DELETE function group. The assumption is that the bean exists in the datasource.
      
       SomeBean bean = new SomeBean();
       bean.setId(1);
       cpo.deleteBean(CpoQuery.group("deleteBean"), bean);
       
      Specified by:
      deleteBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource
      Returns:
      The number of beans deleted from the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • deleteBeans

      public <T> long deleteBeans(CpoQuery query, List<T> beans) throws CpoException
      Description copied from interface: CpoAdapter
      Removes the beans from the datasource using the query's DELETE function group, batching where the datasource supports it. This is an all-or-nothing transaction: if one bean fails, no beans are deleted.
      
       long deleted = cpo.deleteBeans(CpoQuery.group("deleteBean"), beans);
       
      Specified by:
      deleteBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      beans - The beans to delete
      Returns:
      The number of beans deleted from the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • updateBean

      public <T> long updateBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Updates the bean in the datasource using the query's UPDATE function group. The assumption is that the bean exists in the datasource.
      
       SomeBean bean = new SomeBean();
       bean.setId(1);
       bean.setName("NewName");
       cpo.updateBean(CpoQuery.group("updateBean"), bean);
       
      Specified by:
      updateBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource
      Returns:
      The number of beans updated in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • updateBeans

      public <T> long updateBeans(CpoQuery query, List<T> beans) throws CpoException
      Description copied from interface: CpoAdapter
      Updates the beans in the datasource using the query's UPDATE function group, batching where the datasource supports it. This is an all-or-nothing transaction: if one bean fails, no beans are updated.
      
       long updated = cpo.updateBeans(CpoQuery.group("updateBean"), beans);
       
      Specified by:
      updateBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      beans - The beans to update
      Returns:
      The number of beans updated in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • upsertBean

      public <T> long upsertBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Inserts or updates the bean using the query's UPSERT function group: the EXIST function decides whether the CREATE or UPDATE function is executed.
      
       SomeBean bean = new SomeBean();
       bean.setId(1);
       bean.setName("SomeName");
       cpo.upsertBean(CpoQuery.group("upsertBean"), bean); // inserts or updates as needed
       
      Specified by:
      upsertBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource
      Returns:
      The number of beans upserted in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource, or if the EXIST function matches more than one record
    • upsertBeans

      public <T> long upsertBeans(CpoQuery query, List<T> beans) throws CpoException
      Description copied from interface: CpoAdapter
      Inserts or updates the beans using the query's UPSERT function group: for each bean the EXIST function decides whether the CREATE or UPDATE function is executed.
      
       long upserted = cpo.upsertBeans(CpoQuery.group("upsertBean"), beans);
       
      Specified by:
      upsertBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      beans - The beans to upsert
      Returns:
      The number of beans upserted in the datasource
      Throws:
      CpoException - Thrown if there are errors accessing the datasource, or if the EXIST function matches more than one record
    • existsBean

      public <T> long existsBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Checks whether beans matching the given bean exist in the datasource, using the query's EXIST function group and where constraints.
      
       CpoWhere where = cpo.newWhere(Logical.NONE, "id", Comparison.EQ, 1);
       long count = cpo.existsBean(CpoQuery.group("existsBean").where(where), bean);
       if (count > 0) {
         // the bean exists
       }
       
      Specified by:
      existsBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - The bean to search for in the datasource
      Returns:
      The number of beans that exist in the datasource that match the specified bean. An EXIST function must either return a count as a single row with a single numeric column (count(*) style) or return one row per matching bean; a single-row result with one numeric column is always interpreted as a count
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • executeBean

      public <T, C> T executeBean(CpoQuery query, C criteria, T result) throws CpoException
      Description copied from interface: CpoAdapter
      Executes the EXECUTE function group identified by the query — typically a stored procedure — using the criteria bean to populate the IN arguments and the result bean type for the OUT arguments.
      
       SomeResult result =
           cpo.executeBean(CpoQuery.group("calculateTotals"), criteria, new SomeResult());
       
      Specified by:
      executeBean in interface CpoAdapter
      Type Parameters:
      T - The type of the result JavaBean
      C - The type of the criteria JavaBean
      Parameters:
      query - The function group to execute
      criteria - A bean defined within the metadata of the datasource used to populate the IN arguments
      result - A bean defined within the metadata of the datasource that defines the bean type populated with the OUT arguments
      Returns:
      A result bean populated with the OUT arguments
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • retrieveBean

      public <T> T retrieveBean(CpoQuery query, T bean) throws CpoException
      Description copied from interface: CpoAdapter
      Retrieves a single bean from the datasource using the query's RETRIEVE function group, with the given bean supplying the search criteria and receiving the result. If the function returns more than one row, an exception is thrown.
      
       SomeBean criteria = new SomeBean();
       criteria.setId(1);
       SomeBean bean = cpo.retrieveBean(CpoQuery.group("retrieveBean"), criteria);
       
      Specified by:
      retrieveBean in interface CpoAdapter
      Type Parameters:
      T - The type of the JavaBean
      Parameters:
      query - The function group and clauses to apply
      bean - A bean defined within the metadata of the datasource whose attributes supply the search criteria
      Returns:
      A populated bean of the same type as the bean passed in, or null if no beans match
      Throws:
      CpoException - Thrown if there are errors accessing the datasource or more than one row is returned
    • retrieveBean

      public <T, C> T retrieveBean(CpoQuery query, C criteria, T result) throws CpoException
      Description copied from interface: CpoAdapter
      Retrieves the first bean produced by the query's RETRIEVE function group, using separate criteria and result beans.
      
       SomeResult result =
           cpo.retrieveBean(CpoQuery.group("retrieveResult"), criteria, new SomeResult());
       
      Specified by:
      retrieveBean in interface CpoAdapter
      Type Parameters:
      T - The type of the result JavaBean
      C - The type of the criteria JavaBean
      Parameters:
      query - The function group and clauses to apply
      criteria - A bean defined within the metadata of the datasource that supplies the retrieval parameters
      result - A bean defined within the metadata of the datasource that specifies the returned bean type
      Returns:
      A bean of the same type as the result parameter, or null if no beans match
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • retrieveBeans

      public <T, C> Stream<T> retrieveBeans(CpoQuery query, C criteria, T result) throws CpoException
      Description copied from interface: CpoAdapter
      Retrieves beans from the datasource using the query's LIST function group, with separate criteria and result beans.
      
       CpoWhere where = cpo.newWhere(Logical.NONE, "dept", Comparison.EQ, "sales");
       CpoOrderBy orderBy = cpo.newOrderBy("name", true);
       try (Stream<SomeResult> beans =
           cpo.retrieveBeans(
               CpoQuery.group("listResults").where(where).orderBy(orderBy),
               criteria,
               new SomeResult())) {
         beans.forEach(...);
       }
       
      Specified by:
      retrieveBeans in interface CpoAdapter
      Type Parameters:
      T - The type of the result JavaBean
      C - The type of the criteria JavaBean
      Parameters:
      query - The function group and clauses to apply
      criteria - A bean defined within the metadata of the datasource that supplies the retrieval parameters
      result - A bean defined within the metadata of the datasource that specifies the returned bean type
      Returns:
      A stream of beans that meet the criteria; empty if none match. The stream is backed by open datastore resources (statement, result set, and connection) that are released only when the stream is closed; terminal operations do not close it. Always close the returned stream, preferably with try-with-resources.
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • newOrderBy

      public CpoOrderBy newOrderBy(String attribute, boolean ascending) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoOrderBy for the attribute and direction.
      
       CpoOrderBy byNameAscending = cpo.newOrderBy("name", true);
       
      Specified by:
      newOrderBy in interface CpoAdapter
      Parameters:
      attribute - The metadata attribute name to order by
      ascending - true for ascending, false for descending
      Returns:
      A CpoOrderBy
      Throws:
      CpoException - An error occurred creating the CpoOrderBy
    • newOrderBy

      public CpoOrderBy newOrderBy(String marker, String attribute, boolean ascending) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoOrderBy bound to a marker within the expression.
      Specified by:
      newOrderBy in interface CpoAdapter
      Parameters:
      marker - The marker in the expression that this order-by replaces
      attribute - The metadata attribute name to order by
      ascending - true for ascending, false for descending
      Returns:
      A CpoOrderBy
      Throws:
      CpoException - An error occurred creating the CpoOrderBy
    • newOrderBy

      public CpoOrderBy newOrderBy(String attribute, boolean ascending, String function) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoOrderBy applying a datasource function to the attribute.
      Specified by:
      newOrderBy in interface CpoAdapter
      Parameters:
      attribute - The metadata attribute name to order by
      ascending - true for ascending, false for descending
      function - A datasource function to apply to the attribute
      Returns:
      A CpoOrderBy
      Throws:
      CpoException - An error occurred creating the CpoOrderBy
    • newOrderBy

      public CpoOrderBy newOrderBy(String marker, String attribute, boolean ascending, String function) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoOrderBy bound to a marker, applying a datasource function to the attribute.
      Specified by:
      newOrderBy in interface CpoAdapter
      Parameters:
      marker - The marker in the expression that this order-by replaces
      attribute - The metadata attribute name to order by
      ascending - true for ascending, false for descending
      function - A datasource function to apply to the attribute
      Returns:
      A CpoOrderBy
      Throws:
      CpoException - An error occurred creating the CpoOrderBy
    • newWhere

      public CpoWhere newWhere() throws CpoException
      Description copied from interface: CpoAdapter
      Creates an empty CpoWhere.
      
       CpoWhere where = cpo.newWhere();
       
      Specified by:
      newWhere in interface CpoAdapter
      Returns:
      A CpoWhere
      Throws:
      CpoException - An error occurred creating the CpoWhere
    • newWhere

      public <T> CpoWhere newWhere(Logical logical, String attr, Comparison comp, T value) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoWhere comparing the attribute to the value.
      
       CpoWhere where = cpo.newWhere(Logical.NONE, "id", Comparison.EQ, 42);
       
      Specified by:
      newWhere in interface CpoAdapter
      Type Parameters:
      T - The type of the value
      Parameters:
      logical - How this where combines with the preceding where (AND, OR, NONE)
      attr - The metadata attribute name to constrain
      comp - The comparison operator
      value - The value to compare against
      Returns:
      A CpoWhere
      Throws:
      CpoException - An error occurred creating the CpoWhere
    • newWhere

      public <T> CpoWhere newWhere(Logical logical, String attr, Comparison comp, T value, boolean not) throws CpoException
      Description copied from interface: CpoAdapter
      Creates a CpoWhere comparing the attribute to the value, optionally negated.
      Specified by:
      newWhere in interface CpoAdapter
      Type Parameters:
      T - The type of the value
      Parameters:
      logical - How this where combines with the preceding where (AND, OR, NONE)
      attr - The metadata attribute name to constrain
      comp - The comparison operator
      value - The value to compare against
      not - true to negate the comparison
      Returns:
      A CpoWhere
      Throws:
      CpoException - An error occurred creating the CpoWhere
    • getCpoMetaDescriptor

      public CpoMetaDescriptor getCpoMetaDescriptor()
      Description copied from interface: CpoAdapter
      Get the CpoMetaDescriptor
      Specified by:
      getCpoMetaDescriptor in interface CpoAdapter
      Returns:
      The CpoMetaDescriptor
    • getDataSourceName

      public String getDataSourceName()
      Description copied from interface: CpoAdapter
      Get the name of the datasource
      Specified by:
      getDataSourceName in interface CpoAdapter
      Returns:
      The name of the datasource
    • getFetchSize

      public int getFetchSize()
      Description copied from interface: CpoAdapter
      Get the fetch size for the datasource
      Specified by:
      getFetchSize in interface CpoAdapter
      Returns:
      The fetchsize
    • setFetchSize

      public void setFetchSize(int fetchSize)
      Description copied from interface: CpoAdapter
      set the fetch size for the datasource
      Specified by:
      setFetchSize in interface CpoAdapter
      Parameters:
      fetchSize - The fetchsize to set for retrieving data
    • getBatchSize

      public int getBatchSize()
      Description copied from interface: CpoAdapter
      Get the batch size for updating the datasource
      Specified by:
      getBatchSize in interface CpoAdapter
      Returns:
      The batchsize
    • setBatchSize

      public void setBatchSize(int batchSize)
      Description copied from interface: CpoAdapter
      set the batch size for updating the datasource
      Specified by:
      setBatchSize in interface CpoAdapter
      Parameters:
      batchSize - The batchsize for updating the datasource
    • getCpoAttributes

      public List<CpoAttribute> getCpoAttributes(String expression) throws CpoException
      Description copied from interface: CpoAdapter
      Gets the CpoAttribute definitions matching a comma-separated expression of attribute names, as declared in the meta data for this bean's class.
      Specified by:
      getCpoAttributes in interface CpoAdapter
      Parameters:
      expression - An expression defining the CpoAttributes that you want
      Returns:
      A list of CpoAttributes
      Throws:
      CpoException - Thrown if there are errors accessing the datasource
    • isSameRM

      public boolean isSameRM(XAResource xaResource) throws XAException
      Specified by:
      isSameRM in interface XAResource
      Throws:
      XAException
    • commitResource

      public void commitResource(JdbcCpoAdapter jdbcCpoAdapter) throws XAException
      Throws:
      XAException
    • rollbackResource

      public void rollbackResource(JdbcCpoAdapter jdbcCpoAdapter) throws XAException
      Throws:
      XAException
    • createNewResource

      public JdbcCpoTrxAdapter createNewResource() throws XAException
      Throws:
      XAException
    • closeResource

      public void closeResource(JdbcCpoAdapter jdbcCpoAdapter) throws XAException
      Throws:
      XAException