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"; ThreadLocalContext.setSecurityContext(SecurityContext.createSystemUserContext()); TransactionFactory.beginTransactionRequired(); 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 { TransactionFactory.endTransactionRequired(); ThreadLocalContext.clearSecurityContext(); }
- See Also:
ThreadLocalContext
-
-
Constructor Summary
Constructors Constructor Description TransactionFactory()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
beginTransactionRequired()
Begin a transactionstatic void
endTransactionRequired()
End the transactionstatic void
failure()
Fail the transaction
-
-
-
Method Detail
-
failure
@ThingworxExtensionApiMethod(since={6,6}) public static void failure()
Fail the transaction
-
beginTransactionRequired
@ThingworxExtensionApiMethod(since={6,6}) public static void beginTransactionRequired() throws java.lang.Exception
Begin a transaction- Throws:
java.lang.Exception
- If an error occurs
-
endTransactionRequired
@ThingworxExtensionApiMethod(since={6,6}) public static void endTransactionRequired()
End the transaction
-
-