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>