5

I am using hibernate 3.0 in spring with Mysql 5. I have configured JNDI datasource in JBOSS and using it in application context.

My Problem is that Hibernate is issuing average 466.4 queries per second to the database with hardly any load on website.

ApplicationContext.xml snippet is

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
    <property name="jndiName" value="java:MyCustomDSName" />
    <property name="resourceRef" value="true" /> 
</bean>

I am using JTA transaction at java level. Any help welcome.

4
  • 1
    What are those queries? How many hits do the web server do you have per second? Commented Mar 26, 2011 at 11:41
  • 466 queries per second. Those queries are HQL queries. But i think I have got the problem. Perhaps it is Lazy initialization which is firing queries . Commented Mar 26, 2011 at 14:28
  • You might want to enable SQL logging to see what's actually going on. Without this, you're just shooting in the dark. Commented Mar 26, 2011 at 22:48
  • @ManishMudgal if you found a solution please put it as an answer and accept Commented Feb 21, 2012 at 6:56

3 Answers 3

3

One of these should be the case

  • You're getting/processing too many requests - unlikely in dev.
  • You're running into an N+1 select condition - very common.

Please post your domain model, and the queries being executed.

Sign up to request clarification or add additional context in comments.

Comments

2

This is typically caused by N+1 query issues. However, you can use a unit test assert mechanism to find all those JPA and Hibernate data access methods that cause N+1 query issues, like this JUnit assert mechanism using datasource-proxy.

Also, you are better off switching all EAGER associations to LAZY because EAGER fetching can easily generate N+1 query issues if you forget to JOIN FETCH the associations in every JPQL or Criteria API query.

LAZY loading can also cause N+1 query issues. This happens when you iterate over a collections of child entities while initializing a parent Proxy on every iteration. Again, this can be easily detected using this JUnit assert mechanism using datasource-proxy.

Comments

0

How are you using JTA transactions? If every Java method is marked to require a new transaction, this could explain part of your problem. Also, your Hibernate object relationships should be reviewed. If you have complex relationships, a lot of eagerly-loaded objects defined in your model relationships, or circular relationships, you could have harder problems to solve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.