cancel
Showing results for 
Search instead for 
Did you mean: 

Issues with creating user inside Java task

butik
Champ in-the-making
Champ in-the-making
Hello,
I'm implementing "Create user process" - at first step initiator enters some user details and at second stet Java service task is called:

<java>
public class CreateUser implements ActivityBehavior {

  private static final long serialVersionUID = -7993850756520475229L;

  @Override
  public void execute(final ActivityExecution execution) throws Exception {
    System.out.println("Start creating user");
    PvmTransition transition = null;

    try {
      IdentityServiceImpl identityService = (IdentityServiceImpl) execution.getEngineServices().getIdentityService();

      String userId = (String) execution.getVariable("screenName");
      String firstName = (String) execution.getVariable("firstName");
      String lastName = (String) execution.getVariable("lastName");
      String email = (String) execution.getVariable("email");
      String password = (String) execution.getVariable("password");

      User newUser = identityService.newUser(userId);

      newUser.setEmail(email);
      newUser.setFirstName(firstName);
      newUser.setLastName(lastName);
      newUser.setPassword(password);

      identityService.saveUser(newUser);
     
      transition = execution.getActivity().findOutgoingTransition("userStored");

      System.out.println("User:" + userId + "was created");
    } catch (Throwable e) {
      transition = execution.getActivity().findOutgoingTransition("userNotStored");

      System.out.println("User not created: " + e.getMessage());
    }

    execution.take(transition);
  }
}
<java>

I want to handle duplicate id exception  at  identityService.saveUser(newUser); , but the problem is that I'm getting this exception after whole Java service task executed.

How can I force identityService to persist newUser exaclty at a call?
2 REPLIES 2

jbarrez
Star Contributor
Star Contributor
Hmmm interesting problem. The issue here is that, since the call is part of the  java delegate all database calls are executed during the flush (at the end of the command).

What you actually want, is that the call is executed in its own transaction…. What you could do is create your own process engine configuration, however this will make all the service calls being executed in a new transaction:

If this is ok, you can set the default command config on the process engine configuration:
<code>
@Override
  protected void initDefaultCommandConfig() {
    if (defaultCommandConfig == null) {
      defaultCommandConfig = new CommandConfig().setContextReusePossible(true);
    }
  }
</code>

butik
Champ in-the-making
Champ in-the-making
Thanks for reply,
Will try your solution.

BR,
Paul