public TransactionTestCommand(Object mainObject, Object subObject, HashMap<String, Object> env) { this.mainObject = mainObject; this.subObject = subObject; setPersistenceFields(env); }
public Void execute(Context context ) { //cache.joinTransaction();TODO TransactionTestObject obj = (TransactionTestObject) mainObject; if (obj.getId() == null) { obj.setId(generateId()); } cache.put(InfinispanPlaceholderResolverStrategy.getClassIdValue(mainObject), mainObject); if( subObject != null ) { KieSession ksession = ((RegistryContext) context).lookup( KieSession.class ); // THe following 3 lines are the important ones! (See below for an explanation) InternalKnowledgeBase cleanKBase = KnowledgeBaseFactory.newKnowledgeBase(); cleanKBase.addPackages(((InternalKnowledgeBase)ksession.getKieBase()).getKiePackages()); StatefulKnowledgeSession commandKSession = InfinispanKnowledgeService.newStatefulKnowledgeSession( cleanKBase, null, initializeEnvironment() ); /** * Here's what's going on: * If the SingleSessionCommandService (SSCS) & JtaTransactionManager (JTM) were _not_ aware of transactions, * -> then inserting the mainObject _before_ having inserted the subObject * would cause the operation/persist to fail and the transaction to fail. * - This is because the mainObject contains a foreign key referring to the subObject. * * However, the SSCS & JTM check whether or not they're owners of the transaction * when starting and when committing the transaction they use. * -> So that when we insert the mainObject here (via a _new_ CommandBasedStatefulKnowledgeSession), * it does _not_ mess with the transaction state and the operation succeeds. */ TransactionTestCommand transactionTestSubCommand = new TransactionTestCommand(this.subObject, getPersistenceEnvironment()); commandKSession.execute(transactionTestSubCommand); } return null; }
@Test public void testSingleSessionCommandServiceAndJtaTransactionManagerTogether() { // Initialize drools environment stuff Environment env = createEnvironment(context); KieBase kbase = initializeKnowledgeBase(simpleRule); StatefulKnowledgeSession commandKSession = InfinispanKnowledgeService.newStatefulKnowledgeSession( kbase, null, env ); PersistableRunner commandService = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) commandKSession).getRunner(); InfinispanPersistenceContextManager jpm = (InfinispanPersistenceContextManager) getValueOfField("jpm", commandService); jpm.getApplicationScopedPersistenceContext(); @SuppressWarnings("unchecked") Cache<String, EntityHolder> cache = (Cache<String, EntityHolder>) getValueOfField("appScopedCache", jpm); TransactionTestObject mainObject = new TransactionTestObject(); mainObject.setName("mainCommand"); TransactionTestObject subObject = new TransactionTestObject(); subObject.setName("subCommand"); mainObject.setSubObject(subObject); HashMap<String, Object> emEnv = new HashMap<String, Object>(); emEnv.put(COMMAND_ENTITY_MANAGER_FACTORY, cm); emEnv.put(COMMAND_ENTITY_MANAGER, cache); TransactionTestCommand txTestCmd = new TransactionTestCommand(mainObject, subObject, emEnv); commandKSession.execute(txTestCmd); }
public TransactionTestCommand(Object mainObject, HashMap<String, Object> env) { this.mainObject = mainObject; this.subObject = null; setPersistenceFields(env); }