-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Question about Hibernate performance using in a Tomcat env.
PostPosted: Wed Feb 23, 2011 7:23 am 
Newbie

Joined: Thu Aug 27, 2009 10:35 am
Posts: 6
Hi,

we are running a tomcat application using Hibernate to access to an mysql database and have the problem about the performance using it.

When enabling the hibernate_statistics it shows that database queries in average takes about 20 till 30 ms using hibernate. But the mysql-log shows that they only takes about 1 ms. Are 20 ms slow for hibernate; what are normal values?

And especially the method getRoles() (in Code 'Entitiy-Bean') takes about 50 ms when tracking the time in the code programmatly and hibernate do not log any database access for it. So what's wrong? The XML-Configuration for hibernate is attached too.

And maybe have someone any tips for improving the hibernate performance with tuning the hibernate properties shown below (code 'Hibernate and c3p0 (datasource) configurations')?

We are happy about any idea. Thanks.

Entity-Bean:
Code:
@Entity
public class User {

   @Id
   @GeneratedValue
   private Integer id;
   private String username;
   private String password;
   @Column(columnDefinition="MEDIUMBLOB")
   private HashMap<String, String> userstore;
   
   @ManyToMany(fetch=FetchType.EAGER)
   @JoinTable(name="user_role",
         joinColumns={@JoinColumn(name="user_id")},
         inverseJoinColumns={@JoinColumn(name="role_id")}
   )
   private Set<Role> roles;
   
   @ManyToMany(fetch=FetchType.EAGER)
   @JoinTable(name="user_usergroup",
         joinColumns={@JoinColumn(name="user_id")},
         inverseJoinColumns={@JoinColumn(name="usergroup_id")}
   )
   private Set<UserGroup> usergroups;
   
   public Set<Role> getRoles() {
      return roles;
   }

   public void setRoles(Set<Role> roles) {
      this.roles = roles;
   }

   public User() {
      userstore = new HashMap<String, String>();
   }
   
   public Integer getId() {
      return id;
   }
   
   public void setId(Integer id) {
      this.id = id;
   }
   
   public String getUsername() {
      return username;
   }
   
   public void setUsername(String username) {
      this.username = username;
   }
   
   public String getPassword() {
      return password;
   }
   
   /**
    * This method sets the user's password, md5-hashing it on the fly.
    * @param password - unhashed password string, will be hashed
    */
   public void setPassword(String password) {
      password = UtilityFunctions.md5hash(password);
      this.password = password;
   }
   
   public HashMap<String,String> getUserstore() {
      return userstore;
   }

   public void setUserstore(HashMap<String, String> userstore) {
      this.userstore = userstore;
   }

   public Set<UserGroup> getUsergroups() {
      return usergroups;
   }

   public void setUsergroups(Set<UserGroup> usergroups) {
      this.usergroups = usergroups;
   }

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result
            + ((password == null) ? 0 : password.hashCode());
      result = prime * result
            + ((username == null) ? 0 : username.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 (password == null) {
         if (other.password != null)
            return false;
      } else if (!password.equals(other.password))
         return false;
      if (username == null) {
         if (other.username != null)
            return false;
      } else if (!username.equals(other.username))
         return false;
      return true;
   }
}


Hibernate and c3p0 (datasource) configurations:
Code:
   <!-- database connection -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      scope="singleton">
      <property name="driverClass" value="com.mysql.jdbc.Driver" />
      <property name="jdbcUrl" value="jdbc:mysql://${db.server}/${db.database}" />
      <property name="user" value="${db.username}" />
      <property name="password" value="${db.password}" />
      <property name="acquireRetryAttempts" value="10"></property>
      <property name="preferredTestQuery" value="SELECT 1"></property>
      <property name="testConnectionOnCheckin" value="true"></property>
      <property name="testConnectionOnCheckout" value="false"></property>
      <property name="maxIdleTime" value="50"></property>
      <property name="idleConnectionTestPeriod" value="300"></property>
   </bean>

   <!-- Hibernate session factory -->
   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      scope="singleton">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="de.upb.mature.widgetserver.beans" />
      <property name="hibernateProperties">
         <props>
            <!-- recreate database on startup -->
            <!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
            <!-- connection pooling -->
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider
            </prop>
            <prop key="hibernate.c3p0.max_size">10</prop>
            <prop key="hibernate.c3p0.min_size">0</prop>
            <prop key="hibernate.c3p0.timeout">5000</prop>
            <prop key="hibernate.c3p0.max_statements">0</prop>
            <prop key="hibernate.c3p0.idle_test_period">300</prop>
            <prop key="hibernate.c3p0.acquire_increment">1</prop>
         </props>
      </property>
   </bean>


Top
 Profile  
 
 Post subject: Re: Question about Hibernate performance using in a Tomcat env.
PostPosted: Wed Feb 23, 2011 10:50 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

I suggest to set
Code:
hibernate.c3p0.max_statements

to a value > 0.
This enables the PreparedStatement-caching in c3po and can fasten up the thing a little bit,
as the mysql-database then will not be forced to compile each new query (-> prepared statement) you issue via hibernate.


Furthermore I believe that hibernate_statistics includes in his output also the time it toke to
transfer the whole result set from database server to your hibernate application.
The mysql-log shows indeed most probably only the time the engine took to build up the result-set.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.