We are experiencing some slow connections with hibernate. Right whenever we call a service, it seems to pause about 5 seconds before making the connection and searching. Here is the code we are making on the first call:
Code:
public AbstractUser login(String username, String password){
//Configuration cfg = new Configuration().addResource("hibernate.cfg.xml");
//SessionFactory sessionFactory= cfg.buildSessionFactory();
SessionFactory sessionFactory;
sessionFactory = new AnnotationConfiguration()
.addAnnotatedClass(AbstractUser.class)
.addAnnotatedClass(CourseSession.class)
.addAnnotatedClass(Course.class)
.addAnnotatedClass(Message.class)
.addAnnotatedClass(Material.class)
.addAnnotatedClass(MaterialSession.class)
.addAnnotatedClass(Step.class)
.addAnnotatedClass(Section.class)
.addAnnotatedClass(Topic.class)
.addAnnotatedClass(Subtopic.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
.buildSessionFactory();
sessionFactory.openSession();
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
EntityManager em = emf.createEntityManager();
Query q = em.createNamedQuery("users.byUnPass");
q.setParameter("username", username);
q.setParameter("password", password);
AbstractUser user;
try{
user = (AbstractUser) q.getSingleResult();
}catch(javax.persistence.NoResultException e){
user = new AbstractUser();
}
return user;
}
We are using annotations, I am not sure if that is a slower method of lookup than hbm. Also I am not sure of the best way of initialization of annotations to our db. Any advice would be helpful. Also the commented code is for an hbm file that used to serve as our connection, but it didn't like adding the annotations for manytoone and onetomany, so I used the addAnnotatedClass tag to get those relationships read in.
TIA!