3

I am current working on a project which was built with Struts and Hibernate .

All DAO classes in the project has the following code

Inside constructor:

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

Inside finally clause of all methods:

HibernateUtil.closeSession();

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

If I have to access method1 and method2 of 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:

  1. Are anonymous objects garbage collected just after usage? In my project, since every access to methods in DAO class is via anonymous object, will it adversely affect the memory footprint ? if yes any alternative way?

  2. Am I doing it correctly or is there a better way?

  3. Is this the best/correct way of implementation using Hibernate?

  4. Is my usage of the term "anonymous object" for new A(); correct? While searching for the same in Google , I noticed many comments saying this is not called anonymous object in Java, but also came across some articles explaining this as anonymous objects.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Raj
  • 1,945
  • 20
  • 40

2 Answers2

3
  1. There's no such thing as anonymous objects. You can have anonymous classes instances but in your case I think you mean "local variables". The GC uses a highly optimized collecting strategy for short lived object, so there isn't going to be a memory problem even on commodity hardware.

  2. There's a better way. Using Spring transaction management support you can remove the transaction handling routines out of the application code, so your business logic can focus only on business related features.

  3. Transient entities and detached objects are common practice, so no worries here.

  4. The term is not correct. Local variable or transient object is a much more descriptive term.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
3

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

2) There is a better way - dependency injection (DI, IoC), for example Spring DI.

3) No, this is not the best way to implement that, because, besides having a lot of error-prone boilerplate code, you will use a different transaction for each DAO method invocation. There are many use cases in which you want to group multiple method invocations on the same or different DAOs in a single transaction. A much better alternative is to declaratively demarcate transactions at the service layer using a framework designed for that purpose. An example is Spring transaction management.

4) There is no official term for this in Java (honestly, to me it makes sense to name it like that). In java world we simply call it the invocation of the no-arg (default) A's constructor.

Community
  • 1
  • 1
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110