Backendless Support
 
Solved

afterCreate: Duplicate entry

We started receiving these errors starting around 3AM pacific time today. We haven't made any modifications to the server code and it started giving "Duplicate entry" error messages and none of my table item got created.

Did something change on Backendless?

Thanks,

Leave a Comment

Comments (9)

photo
1

Here is the server code. It hasn't been touched and was working fine until this morning.

  1. @Asset( "Notification" )

    public class NotificationTableEventHandler extends com.backendless.servercode.extension.PersistenceExtender<Notification>

    {

    @Override

    public void beforeCreate( RunnerContext context, Notification notification) throws Exception {

    System.out.println("before create");

    Category category = notification.getCategory();

    if (category != null && category.getObjectId() == null) {

    String channel = category.getChannel();

    BackendlessDataQuery dataQuery = new BackendlessDataQuery();

    dataQuery.setWhereClause("channel = '" + channel + "'");

    BackendlessCollection<Category> result = Backendless.Persistence.of(Category.class).find(dataQuery);

    List<Category> categories = result.getCurrentPage();

    if (categories.isEmpty()) {

    category = Backendless.Persistence.of(Category.class).findById("09881500-C33F-04CA-FF04-AFA8CDA02700");

    String title = notification.getTitle();

    notification.setTitle("[Channel Not Found: " + channel + "] " + title);

    System.out.println("Category not found, setting it to apps_notifyr_push_err");

    }

    else {

    category = categories.get(0);

    System.out.println("Found category: " + category.getObjectId());

    }

    notification.setCategory(category);

    }

    }

    @Override

    public void afterCreate( RunnerContext context, Notification notification, ExecutionResult<Notification> result ) throws Exception

    {

    System.out.println("after create!");

    String channel = "apps_notifyr_servercode_err";

    String title;

    ExceptionWrapper ex = result.getException();

    if (ex != null) {

    System.out.println("Exception: " + ex.getExceptionMessage());

    title = "afterCreate(Notification) : " + ex.getExceptionMessage();

    }

    else {

    Category category = notification.getCategory();

    channel = category.getChannel();

    title = notification.getTitle();

    }

    DeliveryOptions deliveryOptions = new DeliveryOptions();

    deliveryOptions.setPushPolicy(PushPolicyEnum.ONLY);

    PublishOptions publishOptions = new PublishOptions();

    publishOptions.putHeader("ios-alert", title);

    publishOptions.putHeader("ios-sound", "default");

    publishOptions.putHeader("n", notification.getObjectId());

    MessageStatus status = Backendless.Messaging.publish(channel, title, publishOptions, deliveryOptions);

    System.out.println(status.toString());

    }

    }

photo
1

What is your application id?

photo
1

1B9E33EE-FD10-CB5B-FFB1-337C3C8C0200

photo
1

Thanks. Could you provide more details when you get the error message and how we can reproduce the problem?

photo
1

The error occurs on the afterCreate server code. There is an exception for some reason now and the code catches it in the if statement. Not sure what is causing it because we didn't change that code for 1 year now and it was working fine until this morning.

  1. ExceptionWrapper ex = result.getException();

    if (ex != null) {

    System.out.println("Exception: " + ex.getExceptionMessage());

    title = "afterCreate(Notification) : " + ex.getExceptionMessage();

    }

Also, the item does not even get created.

photo
1

I just tried creating a notification object and was able to do it without a problem:

cf5e70d53a08a22bafea0391bec0f621

What are the steps to duplicate the issue?

photo
1

I can replicate the issue with the REST API.

curl \

-H application-id:1B9E33EE-FD10-CB5B-FFB1-337C3C8C0200 \

-H secret-key:55FD719B-9FB2-5F94-FF8A-2003DDEA3200 \

-H Content-Type:application/json \

-X POST \

-d "{\"title\":\"This is a test\",\"details\":\"testing\",\"category\":{\"___class\":\"Category\",\"channel\":\"apps_sync_adpemp_err\"}}" \

-v https://api.backendless.com/v1/data/Notification

It looks like it is trying to create the category. But it already exists. Why is it trying to create the Category? This was working yesterday and we haven't made any changes to it.

photo
1

Looks like it is bypassing the beforeCreate handler, which is were the category substitution happens. We're looking into it.

photo
1

The problem has been fixed. I apologize for the inconvenience this has caused you.

photo