I am using TaskExecutor class of Springs to perform asynchronous deletion of a few entries in a database.So basically the execute method of the underlying class is run as a thread to perform asynchronous delete.
In this method I call bean methods that perform deletion of database entries. I have use default Transaction attribute that is PROPAGATION.REQUIRED
in the bean methods.underlying is the execute method i am talking about.Basically I need to rollback the entire database transaction once any exception occurs after callig bean methods or if someone cancels the deletion task.
package com.ibm.security.modeling.async.tasks;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;
import com.ibm.security.modeling.async.AsynchronousTask;
import com.ibm.security.modeling.async.AsynchronousTaskType;
import com.ibm.security.modeling.data.analysis.ModelingManager;
import com.ibm.security.modeling.data.analysis.ProjectManager;
import com.ibm.security.modeling.data.entity.Model;
import com.ibm.security.modeling.data.entity.ModelStatus;
import com.ibm.security.modeling.data.entity.Project;
import com.ibm.security.modeling.i18n.msgs.LogMessages;
public class DeleteProjectTask extends AbstractDeletionTask implements AsynchronousTask
{
private static final String CLASS_NAME = DeleteProjectTask.class.getName();
private static final Logger LOG = Logger.getLogger(CLASS_NAME);
private String projectName;
private Collection<Model> modelCol;
private ProjectManager pMgr = null;
private long projectId = 0L;
private Project project=null;
private PlatformTransactionManager txManager;
public DeleteProjectTask(ProjectManager pMgr, ModelingManager mMgr, long pid)
{
super(pMgr.getProject(pid).getName(), mMgr);
this.project =pMgr.getProject(pid);
this.projectName = project.getName();
this.pMgr = pMgr;
this.projectId = pid;
this.modelCol=project.getModels();
this.txManager=(PlatformTransactionManager)SpringUtils.getFactory().getBean("txManager");
}
public AsynchronousTaskType getTaskType()
{
return AsynchronousTaskType.PROJECT_DELETION;
}
public void execute()
{
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setName("SomeTxName");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
TransactionStatus status = txManager.getTransaction(def);
System.out.println(status.isRollbackOnly());
boolean success = false;
try
{
//recordMessage("execute", LogMessages.PROJECT_DELETE_UNDERGOING, new String[]{projectName},1);
for (Model mo:this.modelCol){
checkForCancellation();
//System.out.println("hello");
//recordMessage("execute", LogMessages.PROJECT_DELETE_UNDERGOING, new String[]{projectName},mo.getId());
//System.out.println("hello");
pMgr.deleteModel(projectId,mo.getId());
System.out.println("66666666666666666666");
}
checkForCancellation();
System.out.println("%%%%%%%%%%%%%%%%%%%%%%");
// pMgr.deleteModel(, modelId)
pMgr.deleteProject(projectId);
System.out.println("$$$$$$$$$$$$$$$$");
// recordMessage("execute", LogMessages.PROJECT_DELETE_COMPLETE_SUCCESS,
// new String[]{projectName},1L);
success = true;
//throw new Exception();
}
catch (TaskCanceledException e)
{
//Informational message that project creation was canceled
//recordMessage("execute", LogMessages.PROJECT_DELETE_CANCELED, new String[]{projectName},0L);
}
catch (Throwable t)
{
LOG.log(Level.INFO, "caught throwable while deleting project " + projectId, t);
if (t instanceof IncorrectResultSizeDataAccessException)
{
// runtime exception indicating that the project could not be located
// during the status update or copy process
// recordErrorMessage("execute", LogMessages.PROJECT_DELETE_FAILED_INTEGRITY, new String[]{projectName},0L);
}
else
{
// some other unexpected error occurred - log error
// recordErrorMessage("execute", LogMessages.PROJECT_DELETE_FAILED_UNKNOWN, new String[]{projectName},0L);
}
}
finally
{
//if not successful, attempt to mark the project as failed
if (!success)
{
System.out.println("i am here4:"+success);
txManager.rollback(status);
System.out.println("am");
}else{
System.out.println("i am here3:"+success);
txManager.commit(status);
}
}
}
}
But things are not working as expected..Infact the result each time is also not consistent i.e sometimes the code gets stuck in the bean methods.I am not able to understand whats going on..someone please help