0 Replies Latest reply on Jun 6, 2018 6:35 AM by David Tierens

    Getting datasource via entityManager in Wildfly 13

    David Tierens Newbie

      In Wildfly 12 it ,was possible to get the DataSource via :

       

      @ApplicationScoped
      public class ExamplePersistenceContextConfiguration {

       

         @PersistenceContext(unitName = "examplePersistenceUnit")

         @Produces
         private EntityManager entityManager;

       

         @Produces
         public DataSource getDataSource() {

        EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();

        Map<String, Object> properties = entityManagerFactory.getProperties();

         return (DataSource) properties.get("javax.persistence.jtaDataSource");

        }

       

         @Produces
         public SessionFactory getSessionFactory() {

        Session session = entityManager.unwrap(Session.class);

         return session.getSessionFactory();

        }

      }

       

      Currently it's returning null in Wildfly 13, however :

        (DataSource) properties.get("hibernate.connection.datasource");

      does work (instead of (DataSource) properties.get("javax.persistence.jtaDataSource")

       

      Persistence.XML :

       

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
         version="2.1">

         <!-- HBM files in this module will be loaded automatically -->
         <persistence-unit name="examplePersistenceUnit" transaction-type="JTA">

         <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

         <jta-data-source>jdbc/ExampleXADataSource</jta-data-source>

         <properties>

         <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>

         <property name="hibernate.show_sql" value="true"/>

         <property name="hibernate.order_inserts" value="true"/>

         <property name="hibernate.jdbc.wrap_result_sets" value="true"/>

         <property name="hibernate.integration.envers.enabled" value="false"/>

         </properties>

         </persistence-unit>

      </persistence>

       

      Is there any way to get it via the properties.get("javax.persistence.jtaDataSource") and not via Hibernate ?

       

      Thanks in advance,

       

      David