DamnYouStark

HibernateUtil.java

Jun 7th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | Source Code | 0 0
  1. package com.phantom.db.util;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.cfg.Configuration;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10.  
  11. @Component
  12. public class HibernateUtil {
  13.  
  14.     private static SessionFactory sessionFactory;
  15.     private static Configuration configuration;
  16.  
  17.     public HibernateUtil() {
  18.         if (sessionFactory == null) {
  19.             sessionFactory = buildSessionFactory();
  20.         }
  21.     }
  22.  
  23.     private SessionFactory buildSessionFactory() {
  24.         try {
  25.             // Load configuration
  26.             configuration = new Configuration().configure();
  27.  
  28.             // Test connection before building session factory
  29.             testJDBCConnection();
  30.  
  31.             System.out.println("βœ… JDBC connection test successful!");
  32.             return configuration.buildSessionFactory();
  33.         } catch (Throwable ex) {
  34.             System.err.println("❌ Initial SessionFactory creation failed: " + ex.getMessage());
  35.             ex.printStackTrace();
  36.             throw new ExceptionInInitializerError(ex);
  37.         }
  38.     }
  39.  
  40.     private void testJDBCConnection() throws Exception {
  41.         // Extract connection parameters from Hibernate config
  42.         String driver = configuration.getProperty("hibernate.connection.driver_class");
  43.         String url = configuration.getProperty("hibernate.connection.url");
  44.         String username = configuration.getProperty("hibernate.connection.username");
  45.         String password = configuration.getProperty("hibernate.connection.password");
  46.  
  47.         System.out.println("Testing JDBC connection with parameters:");
  48.         System.out.println("Driver: " + driver);
  49.         System.out.println("URL: " + url);
  50.         System.out.println("Username: " + username);
  51.  
  52.         // Load driver class
  53.         Class.forName(driver);
  54.  
  55.         try (Connection conn = DriverManager.getConnection(url, username, password)) {
  56.             if (conn == null) {
  57.                 throw new Exception("DriverManager returned null connection");
  58.             }
  59.  
  60.             System.out.println("Connection valid: " + conn.isValid(2));
  61.         } catch (Exception e) {
  62.             throw new Exception("JDBC connection test failed: " + e.getMessage(), e);
  63.         }
  64.     }
  65.  
  66.     public SessionFactory getSessionFactory() {
  67.         return sessionFactory;
  68.     }
  69.  
  70.     public Session getOpenSession() {
  71.         return sessionFactory.openSession();
  72.     }
  73.  
  74.     public void shutdown() {
  75.         if (sessionFactory != null) {
  76.             sessionFactory.close();
  77.             System.out.println("Session factory closed");
  78.         }
  79.     }
  80. }
  81.  
  82. /**
  83. Output:
  84. Testing JDBC connection with parameters:
  85. Driver: com.mysql.cj.jdbc.Driver
  86. URL: jdbc:mysql://localhost:3306/company_test?allowPublicKeyRetrieval=true
  87. Username: root
  88. Connection valid: true
  89. ? JDBC connection test successful!
  90. com.phantom.configuration.PhantomStaticConfiguration@7e06eefb
  91. Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
  92. Initial SessionFactory creation failed.org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
  93.  
  94. Followed by
  95. "Beans not being able to be created" errors.
  96. */
Advertisement
Add Comment
Please, Sign In to add comment