0

I feel my app runs slow. I use Java for my server, so except java itself,the first reason of slow run, I think, is the account of database access. Hibernate could search all attribute associate with the entity which you are using, so I think it may send a lot of SQL language when finding one entity. If there's a large concurrent happening ,the trouble will happen.

@Component("user")
@Entity
@Table(name="users")
public class User implements Serializable{

    @Id
    @GenericGenerator(strategy="assigned",name="idGenerator")
    @GeneratedValue(generator="idGenerator")
    private String client_id;

    @Column(name="username")
    private String username;

    @Column(name="password")
    private String password;

    @Column(name="email")
    private String email;

    @Column(name="phone")
    private String phone;

    @Column(name="sex")
    private String sex;

    @Column(name="birth")
    private String birth;

    @Column(name="status")
    public String status;

    @Column(name="isonline")
    private String isonline;

    @Column(name="province")
    private String province;

    @Column(name="city")
    private String city;

    @ManyToOne()
    @JoinColumn(name="channel_id")
    private Channel channel;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="created_at",updatable=true)
    private Date created_at = new Date();

    @Column(name="last_login_at")
    private Date last_login_at;

    @ManyToMany()
    @JoinTable(name = "users_labels",
       joinColumns = @JoinColumn(name = "client_id"),
       inverseJoinColumns = @JoinColumn(name = "label_name"))
    @LazyCollection(LazyCollectionOption.FALSE)
    private Set<Label> labellist;

    @ManyToOne()
    @JoinTable(name = "user_topics",
       joinColumns = @JoinColumn(name = "client_id"),
       inverseJoinColumns = @JoinColumn(name = "huanxin_group_id"))
    private Topic topic;

    @ManyToMany()
    @JoinTable(name="friends",
        joinColumns=@JoinColumn(name="client_id"),
        inverseJoinColumns=@JoinColumn(name="friend_id"))
    @LazyCollection(LazyCollectionOption.FALSE)
    private Set<User> friends;

    @OneToMany(mappedBy="user")
    private Set<FeedBack> feedbacks;

    @ManyToMany()
    @JoinTable(name="blacklist",
        joinColumns=@JoinColumn(name="client_id"),
        inverseJoinColumns=@JoinColumn(name="blocker_id"))
    @LazyCollection(LazyCollectionOption.FALSE)
    private Set<User> blacklist;

    public Topic getTopic() {
        return topic;
    }
    public void setTopic(Topic topic) {
        this.topic = topic;
    }
    public Set<Label> getLabellist() {
        return labellist;
    }
    public void setLabellist(Set<Label> labellist) {
        this.labellist = labellist;
    }

    public String getClient_id() {
        return client_id;
    }

    public void setClient_id(String client_id) {
        this.client_id = client_id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getIsonline() {
        return isonline;
    }

    public void setIsonline(String isonline) {
        this.isonline = isonline;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }

    public Channel getChannel() {
        return channel;
    }
    public void setChannel(Channel channel) {
        this.channel = channel;
    }
    public Date getCreated_at() {
        return created_at;
    }

    public void setCreated_at(Date created_at) {
        this.created_at = created_at;
    }

    public Date getLast_login_at() {
        return last_login_at;
    }

    public void setLast_login_at(Date last_login_at) {
        this.last_login_at = last_login_at;
    }
    public Set<User> getFriends() {
        return friends;
    }
    public void setFriends(Set<User> friends) {
        this.friends = friends;
    }
    public Set<FeedBack> getFeedbacks() {
        return feedbacks;
    }
    public void setFeedbacks(Set<FeedBack> feedbacks) {
        this.feedbacks = feedbacks;
    }

    public Set<User> getBlacklist() {
        return blacklist;
    }
    public void setBlacklist(Set<User> blacklist) {
        this.blacklist = blacklist;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((client_id == null) ? 0 : client_id.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (client_id == null) {
            if (other.client_id != null)
                return false;
        } else if (!client_id.equals(other.client_id))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "User [client_id=" + client_id + ", username=" + username
                + ", labellist=" + labellist+ "]";
    }   
}

Just like this, User contains Topic, Label or other Entity in future. So I think that is not possible to remove this association. How could I improve my database access to improve access speed?

Another reason influncing the app speed may be my algorithm. I use bubble sort, selection sort, and quick sort. Each sort seems to cost little time on little data. But what if the concurrent occurs? 300 users may cost only 20 msec but 300 000 I couldn't imagine. Here is my sort code:

private static List<Map<String,Object>> sortInteger(List<Map<String,Object>> list){
    int index = 0;
    int max = 0;
    Map<String,Object> temp = new HashMap<String, Object>();
    for(int i=0;i<list.size();i++){
        for(int j=0;j<list.size()-i-1;j++){
            Map<String,Object> pre = (Map<String, Object>) list.get(j);
            Map<String,Object> next = (Map<String, Object>) list.get(j+1);
            if((Integer)pre.get("sortnum")<(Integer)next.get("sortnum")){
                temp = pre;
                list.set(j, next);
                list.set(j+1,pre);
            }
        }
    }
    return list;
}

So in terms of above, I need some suggestion to solve my question,either theory or code.

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
  • 1
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) and [*What types of questions should I avoid asking?*](/help/dont-ask) It would also be useful to get a friend or colleague to help you with the English in your question. – T.J. Crowder Sep 19 '15 at 06:11
  • Super awkward ! In fact I'm a Chinese with poor English.As I'm learning the ropes around here, I welcome your advice. – rpgmakervx Sep 19 '15 at 06:29

0 Answers0