EntityManager.refresh takes a long time

#1

EntityManager.refresh takes a long time for entity object Person in this code:

    logger.log(Level.INFO, "begin select for person " + new Date().toString());
          
    TypedQuery<Person> personQuery =
        em.createQuery("SELECT m FROM Person m", Person.class);
    List<Person> persons = personQuery.getResultList();
          
    logger.log(Level.INFO, "end select for person " + new Date().toString());
          
    for(Person person : persons) {
        logger.log(Level.INFO, "begin refresh for person = " +
            person + "  "+ new Date().toString());
        em.refresh(person);
        logger.log(Level.INFO, "end refresh for person = " +
            person + "  "+ new Date().toString());
    }

 

INFO: begin select for person Thu Aug 08 19:39:45 GMT+04:00 2013
INFO: end select for person Thu Aug 08 19:39:45 GMT+04:00 2013

INFO: begin refresh for person = Лесов Вася  Thu Aug 08 19:39:45 GMT+04:00 2013
INFO: end refresh for person = Лесов Вася  Thu Aug 08 19:40:14 GMT+04:00 2013
INFO: begin refresh for person = Власов Ваня  Thu Aug 08 19:40:14 GMT+04:00 2013
INFO: end refresh for person = Власов Ваня  Thu Aug 08 19:40:34 GMT+04:00 2013
INFO: begin refresh for person = Щёлокова Альбина  Thu Aug 08 19:40:34 GMT+04:00 2013
INFO: end refresh for person = Щёлокова Альбина  Thu Aug 08 19:40:53 GMT+04:00 2013
INFO: begin refresh for person = Керчев Дмитрий   Thu Aug 08 19:40:53 GMT+04:00 2013
INFO: end refresh for person = Керчев Дмитрий   Thu Aug 08 19:41:12 GMT+04:00 2013
INFO: begin refresh for person = Гайкова Юлия  Thu Aug 08 19:41:12 GMT+04:00 2013
INFO: end refresh for person = Гайкова Юлия  Thu Aug 08 19:41:32 GMT+04:00 2013
INFO: begin refresh for person = Царьков Максим  Thu Aug 08 19:41:32 GMT+04:00 2013
INFO: end refresh for person = Царьков Максим  Thu Aug 08 19:41:51 GMT+04:00 2013
INFO: begin refresh for person = Молчанова Ольга   Thu Aug 08 19:41:51 GMT+04:00 2013
INFO: end refresh for person = Молчанова Ольга   Thu Aug 08 19:41:51 GMT+04:00 2013
INFO: begin refresh for person = Логинова Алина  Thu Aug 08 19:41:51 GMT+04:00 2013
INFO: end refresh for person = Логинова Алина  Thu Aug 08 19:42:11 GMT+04:00 2013
INFO: begin refresh for person = Антипенко Олег  Thu Aug 08 19:42:11 GMT+04:00 2013
INFO: end refresh for person = Антипенко Олег  Thu Aug 08 19:42:30 GMT+04:00 2013
INFO: begin refresh for person = Иванова Ольга   Thu Aug 08 19:42:30 GMT+04:00 2013
INFO: end refresh for person = Иванова Ольга   Thu Aug 08 19:42:31 GMT+04:00 2013
INFO: begin refresh for person = Попова Аня  Thu Aug 08 19:42:31 GMT+04:00 2013
INFO: end refresh for person = Попова Аня  Thu Aug 08 19:42:50 GMT+04:00 2013
INFO: begin refresh for person = Бурд Александра  Thu Aug 08 19:42:50 GMT+04:00 2013
INFO: end refresh for person = Бурд Александра  Thu Aug 08 19:43:09 GMT+04:00 2013
INFO: begin refresh for person = Гаврин Игорь  Thu Aug 08 19:43:09 GMT+04:00 2013
INFO: end refresh for person = Гаврин Игорь  Thu Aug 08 19:43:28 GMT+04:00 2013
INFO: begin refresh for person = Селезнёва Катя  Thu Aug 08 19:43:28 GMT+04:00 2013
INFO: end refresh for person = Селезнёва Катя  Thu Aug 08 19:43:47 GMT+04:00 2013
INFO: begin refresh for person = Тоболева Настя  Thu Aug 08 19:43:47 GMT+04:00 2013
INFO: end refresh for person = Тоболева Настя  Thu Aug 08 19:44:06 GMT+04:00 2013
INFO: begin refresh for person = Климов Миша   Thu Aug 08 19:44:06 GMT+04:00 2013
INFO: end refresh for person = Климов Миша   Thu Aug 08 19:44:25 GMT+04:00 2013
INFO: begin refresh for person = Филиппова Альбина  Thu Aug 08 19:44:25 GMT+04:00 2013
INFO: end refresh for person = Филиппова Альбина  Thu Aug 08 19:44:26 GMT+04:00 2013
INFO: begin refresh for person = Сергеев Вася  Thu Aug 08 19:44:26 GMT+04:00 2013
INFO: end refresh for person = Сергеев Вася  Thu Aug 08 19:44:45 GMT+04:00 2013
INFO: begin refresh for person = Хомкина Кристина  Thu Aug 08 19:44:45 GMT+04:00 2013
INFO: end refresh for person = Хомкина Кристина  Thu Aug 08 19:44:46 GMT+04:00 2013
INFO: begin refresh for person = Карпов Лёня  Thu Aug 08 19:44:46 GMT+04:00 2013
INFO: end refresh for person = Карпов Лёня  Thu Aug 08 19:45:05 GMT+04:00 2013
INFO: begin refresh for person = Королева Ольга   Thu Aug 08 19:45:05 GMT+04:00 2013
INFO: end refresh for person = Королева Ольга   Thu Aug 08 19:45:24 GMT+04:00 2013
INFO: begin refresh for person = Диденко Оксана  Thu Aug 08 19:45:24 GMT+04:00 2013
INFO: end refresh for person = Диденко Оксана  Thu Aug 08 19:45:43 GMT+04:00 2013
INFO: begin refresh for person = Смирнова Светлана  Thu Aug 08 19:45:43 GMT+04:00 2013
INFO: end refresh for person = Смирнова Светлана  Thu Aug 08 19:46:03 GMT+04:00 2013
INFO: begin refresh for person = Шелест Ольга   Thu Aug 08 19:46:03 GMT+04:00 2013

 

Person has a large number of relations, including relationsto itself with a cascade refresh:

@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
    private List<OpenIdAuth> openIdAuths = new ArrayList<>();
   
    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
    private List<PersonOperation> operations = new ArrayList<>();
   
    @ManyToMany(cascade = CascadeType.REFRESH) @NotNull
    private List<Person> selectedPersons = new ArrayList<>();
   
    @ManyToMany(mappedBy = "selectedPersons", cascade = CascadeType.REFRESH) @NotNull
    private List<Person> selectedMePersons = new ArrayList<>();
   
    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) @NotNull
    private List<PersonView> personViews = new ArrayList<>();
   
    @OneToMany(mappedBy = "viewedPerson", cascade = CascadeType.ALL) @NotNull
    private List<PersonView> meViews = new ArrayList<>();
   
    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) @NotNull
    private List<PersonLike> personLikes = new ArrayList<>();
   
    @OneToMany(mappedBy = "likedPerson", cascade = CascadeType.ALL) @NotNull
    private List<PersonLike> meLikes = new ArrayList<>();
   
    @OneToMany(mappedBy = "person", cascade = CascadeType.REFRESH)
    private List<PersonObject> userObjects = new ArrayList<>();
   
    @OneToMany(mappedBy = "person", cascade = CascadeType.REFRESH)
    private List<Comment> comments = new ArrayList<>();
   
    @OneToOne(cascade = CascadeType.ALL)
    private SiteAuth siteAuth;
   
    private DialogAction currentDialogAction;
   
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH) @NotNull
    private List<Answer> answers = new ArrayList<Answer>();
   
    @ManyToOne
    private City city;
    @OneToOne(cascade = {CascadeType.PERSIST})
    private Image mainImage;
   
    @OneToOne(cascade = {CascadeType.PERSIST})
    private Album mainAlbum;
   
    @OneToMany(mappedBy="person", fetch = FetchType.EAGER, cascade = CascadeType.ALL) @NotNull
    private List<Album> albums = new ArrayList<>();
   
    @ManyToMany(cascade = CascadeType.REFRESH) @NotNull
    private List<Interest> interests = new ArrayList<>();
   
    @ManyToOne(cascade = CascadeType.REFRESH)
    private City preferredCity; //City preferredCity;
   
    @OneToMany(mappedBy = "recipient", cascade = CascadeType.ALL) @NotNull
    private List<GiftOperation> giftOperations = new ArrayList<>();
   
    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) @NotNull
    private List<GiftOperation> myGiftOperations = new ArrayList<>();
   
    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) @NotNull
    private List<LaraGiftOperation> laraGiftOperations = new ArrayList<>();
   
    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) @NotNull
    private List<AwardOperation> awardOperations = new ArrayList<>();
   
    private int credits;
    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) @NotNull
    private List<PaymentOperation> paymentOperations = new ArrayList<>();
   
    @ManyToOne
    private PersonLevel level;

 

Does i understand this term correctly, if try to refresh Entitymanager updates only objects in his context?

Operation time reaches 30 - 40 seconds. Please help to understand why. 

Thanks for your help.

#2

The refresh operation in this object model includes running many queries in order to refresh all the inverse (mappedBy) collections, and then recursively other objects. Apparently you refresh a large graph of objects.

Here are some tips for improving performance:

  • Avoid CascadeType.ALL and CascadeType.REFRESH for these relationships, as possible.
  • Index the owner side, i.e. add indexes to the fields that are specified in mappedBy (e.g. recipient), so the queries could be run faster.
  • If you can use ordinary collections with no mappedBy, the data will be stored in the object, and data could be refreshed with no queries.
ObjectDB Support

Reply