Hibernate 5 Core + FlexyPool

103 Views Asked by At

I'm trying to develop an Hibernate 5 Core application and I'm using hibernate.cfg.xml to build the SessionFactory. For DB connection, I'm using HikariCP connection pooling but I wanted to retrieve some metrics using FlexyPool.

My current hibernate.cfg.xml is as follows:

<?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>


        <!-- HikariCP database connection properties and pool configuration -->
        <property name="hibernate.hikari.dataSourceClassName">org.postgresql.ds.PGSimpleDataSource</property>
        <property name="hikari.dataSource.serverName">192.168.1.1</property>
        <property name="hikari.dataSource.portNumber">5432</property>
        <property name="hikari.dataSource.databaseName">dbtest</property>
        <property name="hikari.dataSource.user">dbuser</property>
        <property name="hikari.dataSource.password">dbpass</property>
        <property name="hikari.dataSource.reWriteBatchedInserts">true</property>

        <!-- Maximum number of actual connection in the pool, including both idle and in-use connections -->
        <property name="hibernate.hikari.maximumPoolSize">10</property>
        <!-- Minimum number of idle connections in the pool -->
        <property name="hibernate.hikari.minimumIdle">2</property>
        <!-- Maximum time that a connection is allowed to sit idle in the pool (in milliseconds). Default: 600000 (10 minutes) -->
        <property name="hibernate.hikari.idleTimeout">300000</property>
        <!-- Maximum waiting time for a connection from the pool (in milliseconds). Default: 30000 (30 seconds) -->
        <property name="hibernate.hikari.connectionTimeout">20000</property>
        <!-- Maximum lifetime of a connection in the pool (0 indicates infinite lifetime), subject of course to the idleTimeout setting
        (in milliseconds). Default: 1800000 (30 minutes) -->
        <property name="hibernate.hikari.maxLifetime">600000</property>
        <!-- Controls the amount of time that a connection can be out of the pool before a message is logged indicating a possible
        connection leak (in milliseconds). Default: 0 -->
        <property name="hibernate.hikari.leakDetectionThreshold">30000</property>

        <!-- Select our SQL dialect -->
        <!-- Dialect is required to let Hibernate know the Database Type, MySQL, Oracle, etc.
             Hibernate 4 automatically figure out Dialect from Database Connection Metadata -->
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL10Dialect</property>

        <!-- Specifies when Hibernate should release JDBC connections -->
        <!-- Values: auto (default) | on_close | after_transaction | after_statement -->
        <property name="hibernate.connection.release_mode">auto</property>

        <!-- Set the current session context -->
        <!-- This option is only needed if using getCurrentSession()
             org.hibernate.HibernateException: No CurrentSessionContext configured! -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- Set the default Flush Mode -->
        <!-- This option will set the flush mode on Hibernate
             Values: AUTO (default) | COMMIT | ALWAYS | MANUAL -->
        <property name="hibernate.flushMode">AUTO</property>

        <!-- Set JDBC batch size -->
        <property name="hibernate.jdbc.batch_size">20</property>

        <!-- Hibernate orders the SQL insert statements by entity type -->
        <property name="hibernate.order_inserts">true</property>

        <!-- Hibernate orders the SQL update statements by entity type -->
        <property name="hibernate.order_updates">true</property>

        <!-- Set this property to true if your JDBC driver returns correct row counts from executeBatch().
             It is usually safe to turn this option on. Hibernate will then use batched DML for automatically
             versioned data. Defaults to false. -->
        <property name="hibernate.batch_versioned_data">true</property>

        <!-- Echo the SQL to stdout -->
        <!-- Outputs the SQL queries to stdout, should be disabled in Production
             (there are better ways to output SQL statements) -->
        <property name="hibernate.show_sql">false</property>

        <!-- Show SQL formatted -->
        <property name="hibernate.format_sql">true</property>

        <!-- Database schema handle on startup -->
        <!-- Values: create | validate | update | create-drop
             create
             validate - Validate and export schema DDL to the database
             update
             create-drop -->
        <property name="hibernate.hbm2ddl.auto">validate</property>

        <!-- Tell Hibernate to generate statistics -->
        <!-- (will write one multi-line log statement with summarized statistical information at the end of the session).
             This will be written to logfile through logger org.hibernate.stat = DEBUG -->
        <property name="hibernate.generate_statistics">true</property>

        <!-- Log slow DB queries that take longer than a specific duration (ms) -->
        <!-- This will be written to logfile through logger org.hibernate.SQL_SLOW = INFO -->
        <property name="hibernate.session.events.log.LOG_QUERIES_SLOWER_THAN_MS">1000</property>

    </session-factory>
</hibernate-configuration>

I've read the FlexyPool documentation on how to initialize it for HikariCP but don't know where to start. Any help?

1

There are 1 best solutions below

0
On

You can take a look into the configuration that is used for tests in Hibernate: https://github.com/hibernate/hibernate-orm/blob/main/hibernate-hikaricp/src/test/resources/hibernate.properties