1

I have a standard web app with spring 3 and hibernate framework. I have both applicationContext.xml and hibernate.cfg.xml files with database connection data:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.cglib.use_reflection_optimizer">true</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.release_mode">after_transaction</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/webapp</property>
        <property name="hibernate.connection.username">webapp</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    </session-factory>
</hibernate-configuration>

...

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/webapp" />
    <property name="username" value="webapp" />
    <property name="password" value="password" />
</bean>

Now I need to change this app using a dynamic database name, how to set it runtime on for example in ServletContextListener.contextInitialized?

Tobia
  • 9,165
  • 28
  • 114
  • 219

1 Answers1

0

The XML file store the configuration used to build the context. Find the place where the context is build and there apply your logic for dynamic database location. There is no magic.

  • When I'm in ServletContextListener.contextInitialized the context is already built. There is no way to set it runtime? – Tobia Jun 25 '13 at 15:14