Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for JBRULES-3138 #38

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public static ReteooStatefulSession readSession(ReteooStatefulSession session,

readAgenda( context,
agenda );

// RuleFlowGroups need to reference the session
for ( RuleFlowGroup group : agenda.getRuleFlowGroupsMap().values() ) {
((RuleFlowGroupImpl) group).setWorkingMemory( session );
}

context.wm = session;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package org.drools.persistence.session;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import junit.framework.TestCase;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.command.Context;
import org.drools.command.impl.CommandBasedStatefulKnowledgeSession;
import org.drools.command.impl.GenericCommand;
import org.drools.command.impl.KnowledgeCommandContext;
import org.drools.io.impl.ClassPathResource;
import org.drools.persistence.jpa.JPAKnowledgeService;
import org.drools.runtime.Environment;
import org.drools.runtime.EnvironmentName;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.impl.InternalAgenda;

import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jdbc.PoolingDataSource;

public class RuleFlowGroupRollbackTest extends TestCase {

PoolingDataSource ds1;

@Override
protected void setUp() throws Exception {
ds1 = new PoolingDataSource();
ds1.setUniqueName( "jdbc/testDS1" );
ds1.setClassName( "org.h2.jdbcx.JdbcDataSource" );
ds1.setMaxPoolSize( 3 );
ds1.setAllowLocalTransactions( true );
ds1.getDriverProperties().put( "user",
"sa" );
ds1.getDriverProperties().put( "password",
"sasa" );
ds1.getDriverProperties().put( "URL",
"jdbc:h2:mem:mydb" );
ds1.init();

}

@Override
protected void tearDown() {
ds1.close();
}

public void testRuleFlowGroupRollback() throws Exception {

CommandBasedStatefulKnowledgeSession ksession = createSession();

List<String> list = new ArrayList<String>();
list.add("Test");

ksession.insert(list);
ksession.execute(new ActivateRuleFlowCommand("ruleflow-group"));
assertEquals(1, ksession.fireAllRules());

try {
ksession.execute(new ExceptionCommand());
fail("Process must throw an exception");
} catch (Exception e) {
e.printStackTrace();
}

ksession.insert(list);
ksession.execute(new ActivateRuleFlowCommand("ruleflow-group"));
assertEquals(1, ksession.fireAllRules());

}

private CommandBasedStatefulKnowledgeSession createSession() {

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( new ClassPathResource("ruleflowgroup_rollback.drl"), ResourceType.DRL );
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

if ( kbuilder.hasErrors() ) {
fail( kbuilder.getErrors().toString() );
}

kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );

EntityManagerFactory emf = Persistence.createEntityManagerFactory( "org.drools.persistence.jpa" );
Environment env = KnowledgeBaseFactory.newEnvironment();
env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf );
env.set( EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager() );

return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env );

}

@SuppressWarnings("serial")
public class ActivateRuleFlowCommand implements GenericCommand<Object> {

private String ruleFlowGroupName;

public ActivateRuleFlowCommand(String ruleFlowGroupName){
this.ruleFlowGroupName = ruleFlowGroupName;
}

public Void execute(Context context) {
StatefulKnowledgeSession ksession = ((KnowledgeCommandContext) context).getStatefulKnowledgesession();
((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup(ruleFlowGroupName);
return null;
}

}

@SuppressWarnings("serial")
public class ExceptionCommand implements GenericCommand<Object> {

public Void execute(Context context) {
throw new RuntimeException();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jbpm;

import java.util.List;

rule "Test Process Rule"
ruleflow-group "ruleflow-group"
when
$list : List()
then
System.out.println("list size: " + $list.size());
end