Skip navigation links
Thingworx Platform API
8.5.0
com.thingworx.persistence

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 or changing values of a property. 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 {
          TransactionFactory.beginTransactionRequired();
          ThreadLocalContext.setSecurityContext(SecurityContext.createSystemUserContext());
          Thing exampleThing = ThingUtilities.findThing("ExampleThing");
          exampleThing.setPropertyValue("prop", new StringPrimitive("new value"));
          ThreadLocalContext.setTransactionSuccess(true);
      } catch (Exception e) {
          _logger.error("Could not set property.", e);
          ThreadLocalContext.setTransactionSuccess(false);
          TransactionFactory.failure();
      } finally {
          ThreadLocalContext.clearSecurityContext();
          TransactionFactory.endTransactionRequired();
      }
      
    See Also:
    ThreadLocalContext
    • Constructor Detail

      • TransactionFactory

        public TransactionFactory()