Memory size of anonymous objects in Hibernate

I am currently working on a project that was built with Struts and Hibernate.

All DAO classes in the project have the following code

Internal constructor:

hibernateSession = HibernateUtil.currentSession();      
tx=hibernateSession.beginTransaction();

      

Inside the final sentence of all methods:

HibernateUtil.closeSession();

      

This effectively means that in my business code, I have to initialize a reference variable or create an anonymous object every time I want to access data from the database, i.e.

If I need to access method1

and method2

from class A

:

A a= new A();

a.method1(); // access method 1 

a = new A();  
a.method2(); //access method 2

I now mostly use anonymous objects to get this done ie

new A().method1(); //access method 1
new A().method2(); //access method 2 

      

Now my questions are:

  • Do anonymous objects collect garbage immediately after use? In my project, since every access to the methods of the DAO class is through an anonymous object, will it negatively affect the amount of memory? if yes any alternative way?

  • Am I doing this right or is there a better way?

  • Is this the best / correct way to implement it using Hibernate?

  • Am I using the term "anonymous object" correctly for new A();

    ? While searching for the same on Google, I noticed many comments saying that this is not called an anonymous object in Java, but also came across some articles explaining it as anonymous objects.

+3


source to share


2 answers


  • There are no such things as anonymous objects. You can have anonymous instances of the classes, but in your case, I think you mean "local variables". GC uses a highly optimized collection strategy for short lived object, so there won't be a memory problem even on commodity hardware.

  • There's a better way. With Spring's support for transaction management , you can remove transaction processing routines from your application code, so your business logic can only focus on business-related features.

  • Temporary objects and individual objects are common practice, so don't worry here.

  • This term is incorrect. Local variable or transient object is a much more descriptive term.



+3


source


1) Yes, they are eligible for garbage collection. In modern JVMs, this will not have a significant impact on the performance of the garbage collector, since such objects will be cleaned up directly from Eden .

2) There is a better way - Dependency Injection (DI, IoC) like Spring DI .



3) No, this is not the best way to implement this, because apart from having a large boilerplate of error code, you will use a different transaction for each DAO method call. There are many use cases where you want to group calls to multiple methods in the same or different DAOs in the same transaction. A much better alternative is to declaratively demarcate transactions at the service level using a structure designed for this purpose. For example, Spring transaction management .

4) There is no official term in Java (to be honest, it makes sense for me to call it that). In the java world, we just call it by calling the no-arg (default) constructor A

.

+3


source







All Articles