0

EDIT: Its not the problem that i create Session Object everytime, its bad but in this case not really the reason.

I just built an application with Hibernate, which use an MySql database.

I have a snippet of code like below :

List<FitArticle> articleList = new ArrayList<>();
PerformanceTest.setStarttime();
articleList = db.getArticleByFilter(Filter.STANDARD);
PerformanceTest.printTime("Getting Articles");

PerformanceTest.setStarttime();
List<FitArticle> articleNewList = new ArrayList<>();
articleNewList.add(articleList.get(0));

System.out.println(articleList.size());
controller.renderPage(articleList, page,firstCall);                     
PerformanceTest.printTime("Rendering");

By Using PerformanceTest which just mesures the time between, i got a result that Fetching a list of objects (300 items) take only 200ms or something like that. But working with these new fetched objects take way way longer than it used to be with JDBC.

For clarity, by working with objects i meant render the objects to a html web view.

Further more, i tried to troubleshooting the problem, by using YourKit Java Profiler to figure out exactly which Methods took so long and on top of the table is (which with distance longer than any other methods):

java.lang.ClassLoader.loadClass(String)

And this

long.org.hibernate.internal.SessionImpl.initializeCollection(PersistentCollection, boolean)

This one bothers me the most because its invoked many times in my programm.

The Methode i used to fetch objects :

List<FitArticle> list = new ArrayList<FitArticle>();
Session session = factory.openSession();
Transaction tx = null;
Criteria crit = session.createCriteria(FitArticle.class);
// Here some lines to modify Criteria.
list= crit.list();
return list;

Does anyone have some idea why ? Thanks in advance!

Tuấn Phạm
  • 688
  • 6
  • 20
  • 1
    You are doing it wrong, you should only open one session object at a time and share it –  Apr 04 '17 at 15:39
  • I will try, thank you! – Tuấn Phạm Apr 04 '17 at 15:40
  • @JarrodRoberson You might be right, but still the methode renderPage() takes way way longer than it used to be ( when we fetch the objects list with JDBC). – Tuấn Phạm Apr 04 '17 at 15:43
  • Yes that is expected. You seriously need to read the docs on tools you use; what the are for, and how to use them correctly before complaining about them –  Apr 04 '17 at 15:45
  • why its expected ? i fixed the problem with SessionFactory, but still not anyway faster. Mean while with JDBC, AFTER i got the list of objects, the render() works way way faster, its like 1 sec comparing with 4secs. The problem is rendering the list of objects which i obtained with hibernate is way slower than rendering the same one obtaining with JDBC – Tuấn Phạm Apr 04 '17 at 15:47
  • because hibernate is extremely heavy weight, it is expected to be orders of magnitudes less performant in space and time than straight JDBC. That is what I meant about understanding what tools you are using and what they are good for and what they are not good for. –  Apr 05 '17 at 08:33

0 Answers0