title: Wildfly - set datasource password at runtime
body: <p>I am developing a server application using Jboss wildfly 8.1 and JPA Hibernate. The problem is, that the JPA datasource creditials have to be loaded at runtime (password). When the server starts, it connects to an encrypted storage where it retrieves password to real database. After that, it should establish connection to the real database.</p>
  
  <p>I tried several things already:
  Lookup the datasource through JNDI and rebind it with actual DS.
  Lookup the entityManagerFactory through JNDI and rebind it with custom EntityManager.</p>
  
  <p>but none of these work. Do you have idea how to solve it?</p>
  
  <p>my config:</p>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0"&gt;
     &lt;persistence-unit name="PERSISTENCE_UNIT" transaction-type="JTA"&gt;
        &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
  
        &lt;jta-data-source&gt;java:jboss/datasources/Datasource&lt;/jta-data-source&gt;
        ...classes...
  
        &lt;properties&gt;
          &lt;!-- Properties for Hibernate --&gt;               
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
          &lt;property name="hibernate.show_sql" value="false"/&gt;
          &lt;property name="hibernate.format_sql" value="false"&gt;&lt;/property&gt;
          &lt;property name="hibernate.connection.useUnicode" value="true"/&gt;
          &lt;property name="hibernate.connection.characterEncoding" value="UTF-8"/&gt;
          &lt;property name="hibernate.connection.charSet" value="UTF-8"/&gt;
          &lt;property name="org.hibernate.flushMode" value="commit" /&gt; &lt;!-- THIS ONE IS IMPORTANT --&gt;
  
          &lt;property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactory"/&gt;
          &lt;property name="jboss.entity.manager.jndi.name" value="java:/Manager1"/&gt;
        &lt;/properties&gt;      
     &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>datasource (defined in standalone.xml):</p>
  
  <pre><code>            &lt;datasource jndi-name="java:jboss/datasources/Datasource" pool-name="DS" enabled="true" use-java-context="true"&gt;
                  &lt;connection-url&gt;jdbc:mysql://localhost:3306/repository&lt;/connection-url&gt;
                  &lt;connection-property name="useCompression"&gt;
                      false
                  &lt;/connection-property&gt;
                  &lt;connection-property name="logSlowQueries"&gt;
                      false
                  &lt;/connection-property&gt;
                  &lt;connection-property name="zeroDateTimeBehavior"&gt;
                      convertToNull
                  &lt;/connection-property&gt;
                  &lt;connection-property name="characterEncoding"&gt;
                      utf8
                  &lt;/connection-property&gt;
                  &lt;connection-property name="useUnicode"&gt;
                      true
                  &lt;/connection-property&gt;
                  &lt;connection-property name="connectionCollation"&gt;
                      utf8_unicode_ci
                  &lt;/connection-property&gt;
                  &lt;driver&gt;mysql&lt;/driver&gt;
                  &lt;security&gt;
                      &lt;user-name&gt;user&lt;/user-name&gt;
                      &lt;password&gt;TO_BE_DEFINED&lt;/password&gt;
                  &lt;/security&gt;
              &lt;/datasource&gt;
  </code></pre>
  
  <p>accessing entity manager:</p>
  
  <pre><code>@Stateless
  @Local
  public class GenericDataBean {
  
      @PersistenceContext(type=PersistenceContextType.TRANSACTION)
      private EntityManager em;
  
  ...
  }
  </code></pre>