• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Hibernate is Show slow in these query?

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am executing this query in my mysql db using hibernate


S is my model class

Then sometimes Hibernate starts stoping and then I have to manually restart my web server to work properly

What could be reason for this?
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt it's anything to do with that query (apart from it not being valid syntax).
You'll need to do some logging to find out what's going on internally to your code.

I will say that that code should be using bind variables, and not concatenating the userId into the query.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dave Tolls

Thanks for reply

What is Happening that if I run this query 7,8 times simultenously then my execuation is Halt after 6,7 record query with db.

Don't know why this is happening

I will say that that code should be using bind variables, and not concatenating the userId into the query.


Will there be any advantage of using Bind Variables Instead of my current approach
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:Will there be any advantage of using Bind Variables Instead of my current approach


Yes, your code won't work if the user ID you concatenate into that SQL query contains a quote, as then you'll have unbalanced quotes. (Well, you already do, because you left one off at the end.)
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, your code won't work if the user ID you concatenate into that SQL query contains a quote, as then you'll have unbalanced quotes. (Well, you already do, because you left one off at the end.)



that was my Incomplete code this is my complete code


query is working fine but sometimes it stop responding and server will stop there.If I restart my server then its work fine

I can share my bean and Hibernate.xml files if requires
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the advantage of not having to handle quotes, binding prevents SQL injection, and also allows the database to reuse execution plans instead of having to recalculate them for every unique query.

You still need to log the details of what is going on.
Without that you run the high risk of making assumptions about your code that are incorrect, leading you to try and fix something that is not the actual problem.

Is this a web app?
What database are you using?

When it locks up can you interrogate the db to find out whether there are any deadlocks going on? Most DBs have tools to check on that sort of thing.

Are there other queries that happen? Updates or Inserts that are part of the same transaction?

This is the sort of thing you need to work through...and it all starts with your logs and basic debugging to narrow down exactly where it is blocking.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I view Hibernate Specific logs?

Although I can view using Systm.out.println()

What else?
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is a web app you should have a proper logging framework running.
You should then have log statements in your code showing you what is going on.
Those are all down to you.
You can stick to Sysout, I suppose, but you really ought to use a proper tool.

You can open Hibernate up to log lots of things as well, but whether this is needed might depend on how your own logging goes.
See here (for 3.3, I expect there's equivalent ones for newer Hibernate version).

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I have this query




will this type of query also make data fetching slower?

if yes what is the solution for above approaches?
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that your query?

The quotes seem to be incorrect, and you're still concatenating your WHERE clause rather than binding the userID parameter.

What are you doing with the data retrieved?

Are those all, or most of, the fields in EmployeeGeneralInfo. If so, then why not simply select the whole object?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is that your query?

The quotes seem to be incorrect, and you're still concatenating your WHERE clause rather than binding the userID parameter.



query is working fine but someties it become slow.


Are those all, or most of, the fields in EmployeeGeneralInfo. If so, then why not simply select the whole object?


Well I don't need whole object I need some Columns of that Object.

Can using Binding Parameter will solve problems?
or selecting whole object rather than indivitual columns will solve problem?

Thanks
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it's the query that's slow?
How have you timed it?

Have you run the Hibernate query against the database?

As for binding, it is something you should do in any case. Except for specific cases (enums and booleans where one value is seriously skewed in the database) you should be binding your variables. Not doing so is a mistake, whether it speeds up your query or not. You are putting an extra load on the database by not doing so.

As for the object, if you are selecting most of the values then you may as well select the whole thing.

What you are doing at the moment is the equivalent of taking a Java object and iterating over its attributes picking out all but a couple that you want and sticking them in a List<Object>.
Hibernate may well have cached that EmployeeGeneralInfo that you are interested in, and rather than simply returning the whole thing it now has to extract the bits you asked for.

Of course, you haven't actually shown that this query is the problem.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are so many things that is going on inside hibernate but I don't what is actual logs there

I have just place sysout that's why I want able to detect that my Program stuck on the above line when I run

Like in case of Spring Web Application when we deploy it on server we get clear logs on console what are beans is there any problem while running these beans

and same is true for hibernate

I want to know how to get system logs of hibernate?

then I will be able to post what is actual problem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic