Hibernate - Prod vs Dev

In my application, I use the jvm command line argument to differentiate from production or development. Based on this, I move on to using the appropriate config / properties files to access various databases, etc.

With hibernate we have a file hibernate.cfg.xml

with the property

<property name="hibernate.connection.url">url</property>

      

How can we differentiate between two different urls for this to avoid connecting to production in dev and vice versa.

thank

+3


source to share


3 answers


As mentioned, the correct way is to define data sources inside the application server and expose them via JNDI names.

If the correct path is not an option and you are using Maven, you can get a workaround with maven resource filtering.



Take a look at the spec: here

0


source


I would also advise using datasource (JNDI) or make two different MySQL port numbers to differentiate between Prod / Dev environment.



0


source


There is an example for you:

add filename to WEB-INF whose name is classes.

WEB-INF-classes-configDevelopment

 - -config.properties                        
 - -log4j.xml
 - -theme-html5.properties

      

WEB-INF-classes-configProduction

 - config.properties
 - log4j.xml
 - theme-html5.properties

      

config.properties for testing

connection.jndiName=jdbc/test
user.loginURI=user/login

      

log4j.xml for test

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!-- Appenders -->

    <!-- Console Appender -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%t %-5p: %c - %m%n" />
        </layout>
    </appender> 

    <!-- Application Loggers -->

    <!-- 3rdparty Loggers -->

    <logger name="SPGW">
        <level value="debug" />
    </logger>

    <logger name="org.apache">
        <level value="error" />
    </logger>

    <logger name="org.springframework">
        <level value="error" />
    </logger>

    <logger name="org.hibernate">
        <level value="error" />
    </logger>

    <!-- Root Logger -->
    <root>
        <priority value="debug" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

      

root context.xml

 <!-- APP CONFIGURER -->
        <bean id="appConfigurer" 
            class="YOURPROJECT.PropertyPlaceholderExposer">
            <property name="ignoreResourceNotFound" value="true" />
            <property name="ignoreUnresolvablePlaceholders" value="true" />   
            <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />     
            <property name="locations">
                <list>
                    <value>classpath:configDevelopment/config.properties</value>
                </list>
            </property>
        </bean>

        <!-- LOG4J -->
        <bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <property name="staticMethod">
                <value>org.springframework.util.Log4jConfigurer.initLogging</value>
            </property>
            <property name="arguments">
                <list>
                    <value>classpath:configDevelopment/log4j.xml</value>
                </list>
            </property>
        </bean>

<!-- DATA SOURCE -->
    <bean id="_dataSourceProxy"
        class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="resourceRef" value="true" />
       <property name="jndiName" value="${connection.jndiName}"></property>
       <property name="lookupOnStartup" value="false"></property>
       <property name="cache" value="false"></property>
       <property name="proxyInterface" value="javax.sql.DataSource"></property>
    </bean>

      

PropertyPlaceholderExposer.java

package YOURPROJECT;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.StringValueResolver;

public class PropertyPlaceholderExposer extends PropertyPlaceholderConfigurer implements Map<String, String> {

    Map<String, String> props = new HashMap<String, String>();

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
            throws BeansException {

        this.props.clear();
        for (Entry<Object, Object> e: props.entrySet())
            this.props.put(e.getKey().toString(), e.getValue().toString());

        super.processProperties(beanFactory, props);
    }

    @Override
    protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
            StringValueResolver valueResolver) {

        super.doProcessProperties(beanFactoryToProcess, valueResolver);

        for(Entry<String, String> e: props.entrySet())
            e.setValue(valueResolver.resolveStringValue(e.getValue()));
    }

    // Implement map interface to access stored properties
    @Override public Set<String> keySet() { return props.keySet(); }
    @Override public Set<java.util.Map.Entry<String, String>> entrySet() { return props.entrySet(); }
    @Override public Collection<String> values() { return props.values(); }
    @Override public int size() { return props.size(); }
    @Override public boolean isEmpty() { return props.isEmpty(); }
    @Override public boolean containsValue(Object value) { return props.containsValue(value); }
    @Override public boolean containsKey(Object key) { return props.containsKey(key); }
    @Override public String get(Object key) { return props.get(key); }
    @Override public void clear() { throw new UnsupportedOperationException(); }
    @Override public String put(String key, String value) { throw new UnsupportedOperationException(); }
    @Override public String remove(Object key) { throw new UnsupportedOperationException(); }
    @Override public void putAll(Map<? extends String, ? extends String> t) { throw new UnsupportedOperationException(); }

}

      

0


source







All Articles