@ThingworxExtensionApiClass(since={6,6}, canInstantiate=true) public class TransactionFactory extends java.lang.Object
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(); }
ThreadLocalContext
Constructor and Description |
---|
TransactionFactory() |
Modifier and Type | Method and Description |
---|---|
static void |
beginTransactionRequired()
Begin a transaction |
static void |
endTransactionRequired()
End the transaction |
static void |
failure()
Fail the transaction |
@ThingworxExtensionApiMethod(since={6,6}) public static void failure()
@ThingworxExtensionApiMethod(since={6,6}) public static void beginTransactionRequired() throws java.lang.Exception
java.lang.Exception
- If an error occurs@ThingworxExtensionApiMethod(since={6,6}) public static void endTransactionRequired()