There is a small chance you can get in trouble when your code is accessed from multiple threads. When two threads access getSessionFactory()
one might create a SessionFactory
and the other one might reach if(sessionFactory == null)
before the first one is assigned to the variable.
The easiest way to fix this is to make getSessionFactory()
synchronized
. This might cause some locking-overhead and have an impact on performance.
For more sophisticated methods see for example here or do some googling on "Singleton pattern java".