tags:

views:

34

answers:

1

my code is

static {
 try {
  sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

 } catch (Throwable ex) {
  System.err.println("Initial SessionFactory creation failed." + ex);
  throw new ExceptionInInitializerError(ex);
 }
}

here i created only single instance of SessionFactory

the above code work correctly but why we create only single instance ?

+3  A: 

The process of creating a session factory is expensive, performance wise. The performance gain from using a single static session factory is at least an order of magnitude. You can certainly create a new factory on each request, if you'd like, but it would be incredibly wasteful to do so.

Chris