title: How do synchronized static methods work in Java and can I use it for loading Hibernate entities?
body: <p>If I have a util class with static methods that will call Hibernate functions to accomplish basic data access. I am wondering if making the method <code>synchronized</code> is the right approach to ensure thread-safety.</p>
  
  <p>I want this to prevent access of info to the same DB instance. However, I'm now sure if the following code are preventing <code>getObjectById</code> being called for all Classes when it is called by a particular class. </p>
  
  <pre><code>public class Utils {
       public static synchronized Object getObjectById (Class objclass, Long id) {
             // call hibernate class
           Session session = new Configuration().configure().buildSessionFactory().openSession();
           Object obj = session.load(objclass, id);
           session.close();
           return obj;
       }
  
       // other static methods
  }
  </code></pre>