Class TransactionFactory

  • java.lang.Object
    • com.thingworx.persistence.TransactionFactory

  • @ThingworxExtensionApiClass(since={6,6},
                                canInstantiate=true)
    public class TransactionFactory
    extends java.lang.Object
    A front-end class that provides convenience methods for managing persistence transactions, particularly in the context of a request.

    The TransactionFactory exposes a number of convenience methods specifically for managing transactions in a persistence store. This is necessary when making changes to a resource that is persisted, such as the creation of entities. In some situations the transaction is generated automatically by the Platform (i.e., when servicing a request), in which case the default transaction can be used. For any code that executes outside of a request or in a separate thread, it is necessary to create your own transaction.

    Transactions can be nested safely. The transaction success status owned by ThreadLocalContext is separate from this nesting logic and should indicate overall success of the process and not the individual transaction.

    The following is an example of using transactions:

      try {
          String username = "user";
          String groupName = "group";
          TransactionFactory.beginTransactionRequired();
          ThreadLocalContext.setSecurityContext(SecurityContext.createSystemUserContext());
          EntityServices entityServices = (EntityServices) EntityUtilities.findEntity("EntityServices", ThingworxRelationshipTypes.Resource);
          entityServices.CreateUser(username, null, null, username);
          User user = (User) EntityUtilities.findEntity(username, ThingworxRelationshipTypes.User);
          Group grp = (Group) EntityUtilities.findEntity(groupName, ThingworxRelationshipTypes.Group);
          if (grp != null) {
              user.addGroup(grp);
          }
      } catch (Exception e) {
          ThreadLocalContext.setTransactionSuccess(false);
          TransactionFactory.failure();
      } finally {
          ThreadLocalContext.clearSecurityContext();
          TransactionFactory.endTransactionRequired();
      }
      
    See Also:
    ThreadLocalContext
    • Constructor Detail

      • TransactionFactory

        public TransactionFactory()