11 Replies Latest reply on Dec 14, 2009 4:03 AM by jwirsig

    Using enviroment variables in persistence.xml

    dyahav

      Hi all,
      I would like to use an environment variable in my persistence.xml for
      determining the database dialect and username dynamically.
      Currently I'm using a simple string but when other wants to run it, he should change it. I want to avoid it by taking it dynamically using an environmet variable.
      Does anybody know how to do this?
      Thanks
      Dyahav

        • 1. Re: Using enviroment variables in persistence.xml
          jaikiran

          Its possible. Here's an example:

          <persistence>
           <persistence-unit name="EJB3Persistence">
          
           <jta-data-source>${myds}</jta-data-source>
          
           </persistence-unit>
          </persistence>


          Then i pass the "myds" value as a JVM parameter while starting JBoss:

          run.bat -Dmyds=java:MyEJB3DS


          • 2. Re: Using enviroment variables in persistence.xml
            dyahav

            Thanks, but I would prefer using an environment variable.
            Do you know how to do that?
            Thanks again
            dyahav

            • 3. Re: Using enviroment variables in persistence.xml
              jaikiran

              You mean Operating System level environment variable? If yes, any specific reason why you want to do that instead of the JVM parameter.

              • 4. Re: Using enviroment variables in persistence.xml
                dyahav

                Yes, I mean OS environmet.

                I have a project that contains a persistence.xml file. this file contains the database configuration including the username and password.
                For development, I would like to deploy this project in other machines using their owning JBoss (that is, each machine has its own AS).

                Instead of modifying the persistence.xml I would like to use an environment variable that defined in each machine.

                Thanks
                dyahav

                • 5. Re: Using enviroment variables in persistence.xml
                  jaikiran

                  Passing the -D java parameter does exactly this :-) When you deploy this persistence.xml, each user on their own machines does not have to change the persistence.xml. Instead all he has to do is set the -D parameter either on the command prompt or in the startup script (run.bat or run.sh).

                  • 6. Re: Using enviroment variables in persistence.xml
                    peterj

                    If you really insist on using an environment variable, then try this (you didn't say which OS you are using, so I'll use Linux syntax). Set the env var:

                    export MYDS="java:MyEJB3DS"

                    then in the run.conf, add:

                    JAVA_OPTS=$JAVA_OPTS -Dmyds=$MYDS

                    and change the persistence.xml file as Jaikiran mentioned.

                    All this does is converts an env var into a system property.

                    • 7. Re: Using enviroment variables in persistence.xml
                      jwirsig

                      I couldn't manage to get it working though i followed the advices posted here.

                      Can anybody tell me in which versions of JBoss it is supposed to work?

                      I have a Jboss 4.2.0 in use.

                      • 8. Re: Using enviroment variables in persistence.xml
                        jaikiran

                        Please post the exact details including what you did and what doesn't work.

                        While posting logs or xml content or code, please remember to wrap it in a code block by using the Code button in the message editor window. Please use the Preview button to ensure that your post is correctly formatted.

                        • 9. Re: Using enviroment variables in persistence.xml
                          jwirsig

                          I added the runtime variable in the run.bat of my jboss by adding -Ddatabasetype=ORACLE.

                          Afterwards i tried to use the variable databasetype in the persistence.xml with the syntax ${databasetype}.

                           

                          The Thing that is not working is the replacement. When the application is deployed there is an error reported, coulnd not find file META-INF/${databasetype}-orm.xml. The File is used as mapping file in the persistence.xml.

                          <mapping-file>META-INF/${databasetype}-orm-work.xml</mapping-file>
                          

                           

                          The Persistence XML is within a jar that is packed into an ear.

                          I already tried to deploy the jar instead but the effect was the same.

                           

                          I wasn't sure whether 4.2.0 supports this feature so i didn't want to spend to much time in investigating the error.

                          • 10. Re: Using enviroment variables in persistence.xml
                            jaikiran

                            I *think* it should work even in that version. Can you post the run.bat file changes? Also please post the initial few lines from the console which shows the startup params, something like this:

                             

                            =========================================================================
                            
                              JBoss Bootstrap Environment
                            
                              JBOSS_HOME: /home/me/jboss-5.1.0.GA
                            
                              JAVA: /home/me/Java/JDK-1.5/jdk1.5.0_17/bin/java
                            
                              JAVA_OPTS: -Dprogram.name=run.sh -server -Xms128m -Xmx512m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djava.net.preferIPv4Stack=true
                            
                              CLASSPATH: /home/me/jboss-5.1.0.GA/bin/run.jar:/home/me/Java/JDK-1.5/jdk1.5.0_17/lib/tools.jar
                            
                            =========================================================================
                            
                            
                            • 11. Re: Using enviroment variables in persistence.xml
                              jwirsig
                              ===============================================================================
                              
                                JBoss Bootstrap Environment
                              
                                JBOSS_HOME: E:\work\jboss-4
                              
                                JAVA: C:\Programme\Java\jdk1.6.0_14\bin\java
                              
                                JAVA_OPTS: -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n  -Dprogram.name=run.bat -server -Dsun
                              .rmi.dgc.client.gcInterval=60000 -Dsun.rmi.dgc.server.gcInterval=60000 -XX:+CMSPermGenSweepingEnabled -XX:MaxPermSize=25
                              6m  -Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl -Djboss.platform.mbeanserver -
                              Dcom.sun.management.jmxremote  -Devergabe.server=jboss
                              
                                AI_OPTS: -Xms256m -Xmx512m -Ddatabasetype=ORACLE
                              
                                CLASSPATH: C:\Programme\Java\jdk1.6.0_14\lib\tools.jar;E:\work\jboss-4\bin\run.jar
                              
                              ===============================================================================
                              

                              The run.bat might have several changes, in fact i dont't know the original version, but the main start command line looks like:

                              set AI_OPTS=-Xms256m -Xmx512m -Ddatabasetype=ORACLE
                              ......
                              echo .....
                              ......
                              :RESTART
                              "%JAVA%" %JAVA_OPTS% %AI_OPTS% "-Djava.endorsed.dirs=%JBOSS_ENDORSED_DIRS%" -classpath "%JBOSS_CLASSPATH%" org.jboss.Main %JBOSS_BIND% %*
                              if ERRORLEVEL 10 goto RESTART