2

I have a JavaEE application (WildFly 8.2 application server, JPA 2.1, Hibernate 4.3...)

As I use JSF I'm using the JSF project stage (wich value is Development or SystemTest or Production according to if we are in dev or in the test or prod server)

We configure the JSF project stage via JNDI within WildFly combining this JBoss how to with the WlidFly specific way so the lookup of "java:/env/jsf/ProjectStage" return Development or SystemTest or Production depending on the environment

For instance this allow us to configure the log without changing the source code (we are using SLF4J and logback) and the logback configuration file look like :

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <insertFromJNDI env-entry-name="java:/env/jsf/ProjectStage" as="projectStage" />
    <property name="STAGE" value="${projectStage}"/>
    <if condition='property("STAGE").equals("Production")'>
        <then>
            <property name="LEVEL" value="INFO"/>
        </then>
        <else>
            <if condition='property("STAGE").equals("SystemTest")'>
                <then>
                    <property name="LEVEL" value="INFO"/>
                </then>
                <else>
                    <property name="LEVEL" value="TRACE"/>
                </else>
            </if>
        </else>
    </if>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="DEBUG" level="TRACE"/>

    <root level="${LEVEL}">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

I show the logback conf file because I would like to do such a thing with the JPA persistence.xml conf file

What I would like to configure is the hibernate.show_sql in the persistence.xml

<persistence ...>
     <persistence-unit name="myappPU" transaction-type="JTA">
         <jta-data-source>java:/jdbc/myappDS</jta-data-source>
         <!-- ... -->
         <properties>
             <property name="hibernate.show_sql" value="true"/>
             <property name="hibernate.format_sql" value="true"/>
             <!-- ... -->
         </properties>
     </persistence-unit>
</persistence>

How to configure JPA/Hibernate within the persistence.xml (such as logback.xml)?

If it's not possible how to configure JPA/Hibernate programmatically in a JavaEE way? we use JPA doing:

@PersistenceContext(unitName = "myappPU", type = PersistenceContextType.EXTENDED)
private EntityManager value ;

I think about to configure the EntityManagerFactory which as a Map<String,Object> getProperties() method but the javadoc says : "Changing the contents of the map does not change the configuration" so how to do?


EDIT

Thank you @Nayan Wadekar and @Martijn

without testing the solution of @Nayan Wadekar, I think that calling Persistence.createEntityManagerFactory(unitName, map) return a new EntityManagerFactory but doesn't change the EntityManagerFactory used by WildFly, so that won't impact injected entityManager

The solution of @Martijn Burger is very instructive, I learned a lot about CDI but, if I well understand the solution, it make me edit the beans.xml CDI conf file instead of the persistence.xml JPA conf file and I don't find out how to configure beans.xml with JNDI such as logback.xml in order to deploy my war without editing anything...

But those two responses gave me some clues :

  • maybe I can change programmatically the EntityManagerFactory WildFly is using via JNDI
  • maybe I could write a CDI producer method to inject EntityManager in the app

I will try...

Those responses make me think I was not enough precise: my goal is to deploy the same war in dev, prod or test without editing anything even conf file (but may be it is a not so good idea...)

kwisatz
  • 1,266
  • 3
  • 16
  • 36
  • No, try `Persistence.createEntityManagerFactory(unitName, map)` & provide required property values in map. With `getProperties()` it returns the current configuration which doesn't affect if values changed in map. – Nayan Wadekar Sep 11 '15 at 10:55
  • How about creating different `persistence-unit`s in your `persistence.xml` and inject them as alternatives? – Martijn Burger Sep 11 '15 at 12:16
  • This post might be helpful: http://antoniogoncalves.org/2014/05/25/switch-datasource-with-cdi-alternatives-and-stereotypes/ – Martijn Burger Sep 11 '15 at 12:17

1 Answers1

0

I have found that leveraging project stage doesn't work too well. First it ties you tightly w/ JSF. Since you're using JPA, what does JSF have to do with JPA?

Anyways, it turns out that WildFly supports variables here

<property name="hibernate.show_sql" value="${some.system.property:false}"/>
<property name="hibernate.format_sql" value="true"/>

Then when starting your application, just override some.system.property.

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • thank you, but how to add system properties in WildFly? I try to add a `` after the `` element of the `standalone-full.xml` but it doesn't seem to work – kwisatz Sep 14 '15 at 10:15
  • well, the project stage: dev, test, prod... is more general than JSF and outside of JSF I access the project stage with JNDI not with the JSF `FacesContext` so the two technologies aren't bind it is just that the JNDI project stage value and name are specified by JSF wich seems to be OK for me. (The system works without JSF even if the project stage have "jsf" in its name) – kwisatz Sep 14 '15 at 10:26
  • That would be how you do it. You can also try -D. Are you sure you're using standalone-full to start? Anything starting with javax.faces is JSF specific. – John Ament Sep 14 '15 at 11:38