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

JBPM-3832 - Provide API method to delete session from db #178

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
public interface SingleSessionCommandService extends CommandService {
int getSessionId();
void dispose();
void destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,7 @@
import org.drools.command.CommandService;
import org.drools.command.GetSessionClockCommand;
import org.drools.command.Interceptor;
import org.drools.command.runtime.AddEventListenerCommand;
import org.drools.command.runtime.DisposeCommand;
import org.drools.command.runtime.GetCalendarsCommand;
import org.drools.command.runtime.GetChannelsCommand;
import org.drools.command.runtime.GetEnvironmentCommand;
import org.drools.command.runtime.GetGlobalCommand;
import org.drools.command.runtime.GetGlobalsCommand;
import org.drools.command.runtime.GetIdCommand;
import org.drools.command.runtime.GetKnowledgeBaseCommand;
import org.drools.command.runtime.RegisterChannelCommand;
import org.drools.command.runtime.RemoveEventListenerCommand;
import org.drools.command.runtime.SetGlobalCommand;
import org.drools.command.runtime.UnregisterChannelCommand;
import org.drools.command.runtime.*;
import org.drools.command.runtime.process.AbortProcessInstanceCommand;
import org.drools.command.runtime.process.AbortWorkItemCommand;
import org.drools.command.runtime.process.CompleteWorkItemCommand;
Expand Down Expand Up @@ -247,6 +235,10 @@ public void dispose() {
commandService.execute( new DisposeCommand() );
}

public void destroy() {
commandService.execute( new DestroySessionCommand(commandService));
}

public int fireAllRules() {
return this.commandService.execute( new FireAllRulesCommand() );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2013 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.command.runtime;

import org.drools.command.CommandService;
import org.drools.command.SingleSessionCommandService;
import org.drools.command.impl.GenericCommand;
import org.kie.command.Context;

public class DestroySessionCommand extends DisposeCommand {

private CommandService commandService;

public DestroySessionCommand() {

}

public DestroySessionCommand(CommandService commandService) {
this.commandService = commandService;
}

public Void execute(Context context) {
if (commandService != null && commandService instanceof SingleSessionCommandService) {
((SingleSessionCommandService) commandService).destroy();
}
super.execute(context);
return null;
}

public String toString() {
return "Destroy session command";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ public void dispose() {
this.session = DisposedReteooWorkingMemory.INSTANCE;
}

public void destroy() {
dispose();
}

public FactHandle insert(Object object) {
return this.session.insert( object );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface PersistenceContext {

public SessionInfo findSessionInfo(Integer id);

void remove(SessionInfo sessionInfo);

boolean isOpen();

void joinTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,33 @@ public void dispose() {
}
}

@Override
public void destroy() {
PersistenceContext persistenceContext = this.jpm.getApplicationScopedPersistenceContext();

boolean transactionOwner = false;
try {
transactionOwner = txm.begin();

persistenceContext.joinTransaction();
initExistingKnowledgeSession( this.sessionInfo.getId(),
this.marshallingHelper.getKbase(),
this.marshallingHelper.getConf(),
persistenceContext );

persistenceContext.remove(this.sessionInfo);

txm.commit( transactionOwner );

} catch ( RuntimeException re ) {
rollbackTransaction( re, transactionOwner );
throw re;
} catch ( Exception t1 ) {
rollbackTransaction( t1, transactionOwner );
throw new RuntimeException( "Wrapped exception see cause", t1 );
}
}

public int getSessionId() {
return sessionInfo.getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public SessionInfo findSessionInfo(Integer id) {
return this.em.find( SessionInfo.class, id );
}

@Override
public void remove(SessionInfo sessionInfo) {
em.remove( sessionInfo );
}

public boolean isOpen() {
return this.em.isOpen();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public SessionInfo findSessionInfo(Integer sessionId) {
return sessionInfo;
}

@Override
public void remove(SessionInfo sessionInfo) {
this.ksessions.remove(sessionInfo.getId());
}

public boolean isOpen() {
return open;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import static org.drools.persistence.util.PersistenceUtil.DROOLS_PERSISTENCE_UNIT_NAME;
import static org.drools.persistence.util.PersistenceUtil.createEnvironment;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -390,4 +388,102 @@ public void testMergeConfig() {

assertEquals("com.example.CustomJPAProcessInstanceManagerFactory", sessionConfig.getProcessInstanceManagerFactory());
}

@Test
public void testCreateAndDestroySession() {
String str = "";
str += "package org.kie.test\n";
str += "global java.util.List list\n";
str += "rule rule1\n";
str += "when\n";
str += " Integer(intValue > 0)\n";
str += "then\n";
str += " list.add( 1 );\n";
str += "end\n";
str += "\n";

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ),
ResourceType.DRL );
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

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

kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );

StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env );
List<?> list = new ArrayList<Object>();

ksession.setGlobal( "list",
list );

ksession.insert( 1 );
ksession.insert( 2 );
ksession.insert( 3 );

ksession.fireAllRules();

assertEquals( 3, list.size() );

int ksessionId = ksession.getId();
ksession.destroy();

try {
JPAKnowledgeService.loadStatefulKnowledgeSession(ksessionId, kbase, null, env);
fail("There should not be any session with id " + ksessionId);
} catch (Exception e) {

}
}

@Test
public void testCreateAndDestroyNonPersistentSession() {
String str = "";
str += "package org.kie.test\n";
str += "global java.util.List list\n";
str += "rule rule1\n";
str += "when\n";
str += " Integer(intValue > 0)\n";
str += "then\n";
str += " list.add( 1 );\n";
str += "end\n";
str += "\n";

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ),
ResourceType.DRL );
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

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

kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );

StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
List<?> list = new ArrayList<Object>();

ksession.setGlobal( "list",
list );

ksession.insert( 1 );
ksession.insert( 2 );
ksession.insert( 3 );

ksession.fireAllRules();

assertEquals( 3, list.size() );

int ksessionId = ksession.getId();
ksession.destroy();

try {
ksession.fireAllRules();
fail("Session should already be disposed " + ksessionId);
} catch (IllegalStateException e) {

}
}
}