Hibernate使用HikariCP池失败创建sessionfactory

时间:2014-11-06 21:38:30

标签: hibernate sessionfactory hikaricp

我尝试使用HikariCP池连接,但我无法启动应用程序,因为我得到所有时间,来自create sesion的null对象。

这是我的配置:

系统

  • Hibernate 4.3.6.Final
  • MariaDB v10.0(x64)

HibernateUtil类

public class HibernateUtil {
private static SessionFactory sessionFactory;

private static SessionFactory buildSessionFactory() {
    try {
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        return sessionFactory;
    }
    catch (HibernateException ex) {
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        sessionFactory = buildSessionFactory();
    }
    return sessionFactory;
}
}

hibernate.cfg.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Datos de conexión a la BD -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3307/******?zeroDateTimeBehavior=convertToNull</property>
        <property name="hibernate.connection.username">****</property>
        <property name="hibernate.connection.password">****</property>
        <property name="javax.persistence.validation.mode">none</property>
        <!-- Configuración de sesión -->
        <property name="connection.release_mode">after_transaction</property>
        <property name="current_session_context_class">thread</property>
        <!-- Configuración de la salida SQL y estadísticas -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
        <!-- Pool de conexiones -->
        <property name="hibernate.connection.provider_class">org.mariadb.jdbc.MySQLDataSource</property>
        <!-- Enable Hibernate's current session context -->
        <property name="hibernate.cache.use_second_level_cache">false</property>
        <property name="hibernate.cache.use_query_cache">false</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    </session-factory>
</hibernate-configuration>

LOG

  

准备使用过滤器构建会话工厂:   {} HHH000130:实例化显式连接提供者:org.mariadb.jdbc.MySQLDataSource   应用程序启动方法中的异常

1 个答案:

答案 0 :(得分:2)

正如brettw所说,我只需要将提供者类更改为

<prop key="hibernate.connection.provider_class">com.zaxxer.hikari.hibernate.HikariConnectionProvider</prop>