You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.5 KiB

  1. package com.sothr.imagetools.engine.dao
  2. import com.sothr.imagetools.engine.util.{PropertiesService, PropertyEnum}
  3. import grizzled.slf4j.Logging
  4. import org.hibernate.SessionFactory
  5. import org.hibernate.boot.registry.StandardServiceRegistryBuilder
  6. import org.hibernate.cfg.Configuration
  7. import org.hibernate.service.ServiceRegistry
  8. /**
  9. * Utility class to interface with hibernate
  10. *
  11. * Created by drew on 2/8/14.
  12. */
  13. object HibernateUtil extends Logging {
  14. private val sessionFactory:SessionFactory = buildSessionFactory()
  15. private var serviceRegistry:ServiceRegistry = null
  16. private def buildSessionFactory():SessionFactory = {
  17. try {
  18. // Create the SessionFactory from hibernate.cfg.xml
  19. val configuration = new Configuration().configure("hibernate.cfg.xml")
  20. //set the database location
  21. info(s"Connecting to database at: \'${PropertiesService.get(PropertyEnum.DatabaseConnectionURL.toString)}\'")
  22. configuration.setProperty("hibernate.connection.url", PropertiesService.get(PropertyEnum.DatabaseConnectionURL.toString))
  23. serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties).build
  24. configuration.buildSessionFactory(serviceRegistry)
  25. } catch {
  26. case ex:Throwable =>
  27. // Make sure you log the exception, as it might be swallowed
  28. error("Initial SessionFactory creation failed.", ex)
  29. throw new ExceptionInInitializerError(ex)
  30. }
  31. }
  32. def getSessionFactory:SessionFactory = {
  33. sessionFactory
  34. }
  35. }