How to use Hibernate connection settings in persistence.xml

This is a short article to learn how to use Hibernate connection properties (username, JDBC URL etc.) in your persistence.xml to drive your connection to the Database

As you know from the article Java Persistence (JPA) with JBoss and WildFly , the file persistence.xml includes a reference to a Datasource that you need to configure in your WildFly datasource subsystem:

<persistence version="2.1"
             xmlns="" xmlns:xsi=""
    <persistence-unit name="<meta http-equiv="content-type" content="text/html; charset=utf-8">userDatabase">

The datasource needs to be JTA compliant (jta=true) to indicate that it will honor the Java Transaction API to allow better tracking of connections by the JCA implementation.

Implementing a Datasource with Hibernate connection settings

It is possible to use Hibernate Connection settings in your persistence.xml with hibernate properties and even reference an external as hibernate.cfg.xml as follows:

<persistence xmlns=""
    <persistence-unit name="resource1" transaction-type="RESOURCE_LOCAL">

            <property name="hibernate.dialect" value="@db.dialect@"/>
            <property name="hibernate.connection.driver_class" value="@jdbc.driver@"/>
            <property name="hibernate.connection.username" value="@jdbc.user@"/>
            <property name="hibernate.connection.password" value="@jdbc.pass@"/>
            <property name="hibernate.connection.url" value="@jdbc.url@"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.cache.region_prefix" value="hibernate.test"/>
            <property name="hibernate.jdbc.use_streams_for_binary" value="true"/>
            <property name="hibernate.jdbc.batch_size" value="0"/>
            <property name="hibernate.max_fetch_depth" value="3"/>
            <property name="" value="create-drop"/>
            <property name="hibernate.generate_statistics" value="true"/>
			<property name="hibernate.cache.region.factory_class" value="org.hibernate.testing.cache.CachingRegionFactory" />
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.jpa.test.MyNamingStrategy"/>

            <!-- alternatively to <class> and <property> declarations, you can use a regular hibernate.cfg.xml file -->
            <!-- property name="hibernate.ejb.cfgfile" value="/resource-path/to/hibernate.cfg.xml"/ -->

The thing to note here is that by setting the transaction-type as RESOURCE_LOCAL forces Hibernate to explicitly manage the transaction boundaries directly on the JDBC connection.

This means, in turn, that Hibernate will manage the Connection commit/rollback rather than allowing the transaction context to be managed by a JTA provider (such as WildFly). In other words, the application code becomes responsible for preserving data integrity in such cases. A possible outcome is that heuristic transactions (mixed commit/rollback) may occur leaving the backing data stores in an inconsistent state.

Finally, as you are not referring to a JTA datasource, you cannot use WildFly’s cached connection manager debug facility.