4

I getting

ERROR: cannot execute UPDATE in a read-only transaction

in the following method at the line with user.update() call:

    User user;
    try {
        user = User.finder.where().eq("phoneNumber", PhoneHelper.normalizePhoneNumber(phoneNumber)).findUnique();
    } catch (NumberFormatException e) {
        final ErrorMessage errorMessage = new ErrorMessage("Invalid phone number");
        errorMessage.addField("phoneNumber");
        return badRequest(Json.toJson(errorMessage));
    }

    ...
    some checks
    ...

    final String newApiKey;
    try {
        newApiKey = UUID.randomUUID().toString();
        user.addApiKey(newApiKey);
        if (!isDemoUser(phoneNumber)) user.setSecurityCode(null);
        user.update();
    } catch (Exception e) {
        Logger.warn("Can't create session", e);
        return internalServerError(Json.toJson(new ErrorMessage("Can't create api_key. Please contact service administrator")));
    }

    return ok(Json.toJson(new ApiKey(newApiKey)));

What could be a reason of this error? I didn't set readOnly value of the transaction to 'true' by myself in this method. Please point me at the direction to search.

User it's just an entity:

@Entity
@Table(name = "userr")
public class User extends Model {
   ...fields...

   public static Finder<Long, User> finder = new Finder<>(User.class);

}

Version of Ebean ORM is

"org.avaje.ebeanorm" % "avaje-ebeanorm" % "6.10.4"
Arkady
  • 1,178
  • 14
  • 35
  • 1
    Is `User` a custom class or one from a library? If it's the first, we'll need to see, at least, the `update()` method. If it's the second, we need to know which library it's from. – Michael Jan 25 '17 at 11:35
  • @Michael sure, thank you. I'v updated my post – Arkady Jan 25 '17 at 11:39

1 Answers1

1

It was just a missing @Transactional on service layer in my case..

Ismail Yavuz
  • 6,727
  • 6
  • 29
  • 50