File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Magic Number 7 ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Magic Number 7 ??" Watch "Magic Number 7 ??" New topic

Magic Number 7 ??

Navin Keswani

Joined: Jun 08, 2004
Posts: 17
Hi I have used the Hibernate Criteria objects for querying the database for sometime now.

Everything works just fine. But now a new module which I am working on requres me to execute 15 to 16 queries inside a method [the main method is broken down into logical groups/methods].

Now the problem is that I can execute 7 Hibernate queries fine buit whne it goes to the 8th one it just stops ... nothing happens at all as if it just dies... no exception nothing...

When I Execute just first 5 queries it works fine and then when I comment out the first 5 and run the next 5 it works fine but when I try to run all 10 together it just stops after the 7th query be it whatever ... I tried shuffling the queries sequence to see if there is a problem with the query itslef but whichever is the 8th query , it doesnt work.

Can anyone please help me out in this as its pretty weird. Are there any cofiguiration settings for Hibernate for memmory or Maximum no. of queries to execute or something which will help me track whats happening....

Navin Keswani
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
no. there is no such setting. can you give some more infos ?

- set log level of hibernate to DEBUG (
- make sure SQL statements are printed
- how many objects end up in the Session ?
- Show some code
- TX / Session management ?
- cache configuration ?
- mappings

Navin Keswani

Joined: Jun 08, 2004
Posts: 17
Hi ,

here is the code snippet

In the above code 2 broad methods namely :
executePolicyStateProcess(policy) and executeLOBProcess(policy) are called.

These methods internally call the Hibernate DAO to execut ethe Hibenate code.

The getFaaDao gets the ref of the Hibernate DAO and the queries to the database are written there...

These are pretty simple staright fwd select stmts queries.....

If I reduce the no. of queries the codce works fine....

the Spring config goes as follows...

[ Edited to use code tags - Paul Sturrock ]
[ November 17, 2006: Message edited by: Paul Sturrock ]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Weel, I have a couple of suggestions. First it looks like you are loading lookup values. Are you using Hibernates 2nd Level Cache so that those values can be cached and you won't have to go to the database for each query?

It also looks like you do not want to change values, but just get all that data and pass it down to some client. So in that case it does not require you to run all the queries in one session. So split it up into running 6 in three different sessions one right after the other.

I think there is a limitation to the number of queries to be able to run in a session.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Navin Keswani

Joined: Jun 08, 2004
Posts: 17
Hi Mark,

Thanks so much for your time but I guess I found a very simple solution to it.

I changed my Spring config file for Hibernate from this

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">


<bean id="dataSource"

Looks like the org.apache.commons.dbcp.BasicDataSource doesnt support many open connections or fails to close open connections or something as when I change it to org.springframework.jdbc.datasource.DriverManagerDataSource it works just fine.

And when we move our code to Staging area where we will be deploying it as a Webservice we would be using the datasource of App server may be Weblogic .

Thanks so much .. once again...
Navin Keswani

Joined: Jun 08, 2004
Posts: 17
I have one more question

It looks like to be a big problem for me

I will start a new thread for it.

I have School and Student table .
There is a one to many relationship. One school can have many students
Now when I wite a SQL query to the School table to get the complete school info.

Now this query returns me a school which has a student which is the eldest age wise in the entire studdnt table.

so when I run the sql it would give me 1 School record.

In that record I want to display student name also

Now when I execute the same in Hibernate it works just fine it gives me just one School object which is what I want but when I need to retrieve the student information from there which has One to many mapping means School would have getStudents() which returns a list of students when I do that it doesnt return me just the student which has the max age but it returns all the students which belong to that school.

so If the school has 3 rows with school id 1 2 3 and student has 150 rows with 50 students belonging to school 1 and other 50 belonging to school 2 and the rest 50 to school 3 so school_1 getStudents will return me 50 students and not just the studnet which has the max age as was specified in the criteria although it returns me the correct and only 1 school object but not the case with the linked classes

can you please provide me some insight on the same

Some one suggested using Hibernate Report queries
I agree. Here's the link:
subject: Magic Number 7 ??
It's not a secret anymore!